MATLAB: ‘Matrix dimensions must agree error’

agreeanonymousdimensionsfunctionMATLABmatrix

bbfun = @(x1,x2) (4-2.1.*x1.^2+x1.^4/3).*x1.^2 + x1.*x2 + (-4+4*x2.^2) .*x2.^2;
x1 = -2:1/100:2;
x2 = -1.5:1/100:1.5;
pltvar = bbfun(x1,x2);
contour(x1,x2,pltvar,20)
Error:
Matrix dimensions must agree.
Error in q2_a>@(x1,x2)(4-2.1*x1.^2+x1.^4/3).*x1.^2+x1.*x2+(-4+4*x2.^2).*x2.^2
Error in q2_a (line 5)
pltvar = bbfun(x1,x2);
Why am I getting this error? What am I doing wrong?

Best Answer

You need a mesh to create contour plot
bbfun = @(x1,x2) (4-2.1.*x1.^2+x1.^4/3).*x1.^2 + x1.*x2 + (-4+4*x2.^2) .*x2.^2;
x1 = -2:1/100:2;
x2 = -1.5:1/100:1.5;
[X1, X2] = meshgrid(x1, x2);
pltvar = bbfun(X1, X2);
contour(X1, X2, pltvar,20)