28 plotting
(1,7) (2,7) (3,7) (4,7)
(1,6) (2,6) (3,6) (4,6)
(1,5) (2,5) (3,5) (4,5)
Figure 9: Operation ofmeshgridfunction
Comments:
- xxis an array consisting of rows of the vectorx.
- yyis an array consisting of columns of vectory.
- xxandyyare then used in the calculation ofz, and the plotting of the
surface.
Listing 2.7 and Figures 10–11 demonstrate usingmeshgridin combination
with the surface plotting functionssurf(creates a colour-filled surface) and
mesh(creates a colored mesh) to plot the function:
z=c·sin
(
2πa
√
x^2 +y^2
)
,
wherea= 3 ,c=0.5,− 16 x 61 , and− 16 y 61.
Listing 2.7: Plotting a surface
1 >> x = linspace(−1,1,50);
2 >> y = x;
3 >> a = 3;
4 >> c = 0.5;
5 >> [xx, yy] = meshgrid(x,y);
6 >> z = c*sin(2*pi*a*sqrt(xx.^2+yy.^2));
7 >> surf(xx,yy,z), colorbar, xlabel('x'), ylabel('y'), zlabel('z'), ...
8 >> title('f(x,y)=csin(2\pia\surd(x^2+y^2))')
9 >> figure;
10 >> mesh(xx,yy,z), colorbar, xlabel('x'), ylabel('y'), zlabel('z'), ...
11 >> title('f(x,y)=csin(2\pia\surd(x^2+y^2))')