MATLAB: Z is not matrix for surf(X,Y,Z)

matrixmeshgridsurf

I am trying to plot surf(X,Y,Z) but my Z is not a matrix.
TH = [0:0.05*pi:2*pi ];
R = [0:0.5:20];
[X,Y] =pol2cart(TH,R);
[X,Y] = meshgrid(X,Y)
Z = (abs((sqrt(2)/(81*sqrt(pi))).*(6.*R-R.^2).*exp(-R./3).*cos(TH))).^2
Im hoping someone can look over my code and help me out. Thanks

Best Answer

TH = [0:0.05*pi:2*pi ];
R = [0:0.5:20];
[X,Y] =pol2cart(TH,R);
[X,Y] = meshgrid(X,Y) ;
[R,TH] = meshgrid(R,TH) ;
Z = (abs((sqrt(2)/(81*sqrt(pi))).*(6.*R-R.^2).*exp(-R./3).*cos(TH))).^2 ;
figure
surf(R,TH,Z)
figure
surf(X,Y,Z)