I try to draw the Mexican hat function using Matlab.

x=-20:20;
y=-20:20;
z=(1 - x.^2 -y.^2) * exp(-1/2 * (x.^2 + y.^2))
But I got error:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
How should do to I fix it? Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are two bugs here. First, you want to create your
xandyusing something likemeshgrid, so that they are arrays rather than vectors (and while we’re at it, let’s look at a more edifying range for the function…)Second, you want element-wise multiplication instead of matrix multiplication (
.*instead of*).That should do the trick: