An interactive introduction to MATLAB

(Jeff_L) #1
2.33d plotting using plot3 and surf 27

ï 1

ï0.5

0

0.5

1

ï 1

ï0.5

0

0.5

1

0

5

10

15

20

25

30

35

x

3D helix

y

z

Figure 8: Usingplot3to plot points on a helix

For plotting surfaces and contours two commonly used functions aresurf(x,y,z)
andmesh(x,y,z)wherex,y, andzare coordinates of points on a surface.
Before you use either of these functions you must use themeshgridfunction
to define a grid of points which the surface will be plotted onto. Listing 2.6
demonstrates the typical use ofmeshgrid. In this example, assumez=f(x,y)
wherexis a vector of values(1,2,3,4)andyis a vector of values(5,6,7).
meshgridtakes the vectorsxandyand returns two matrices, in this case
calledxxandyy, which contain the coordinates of the grid that the surface
zwill be plotted onto. Figure 9 shows the coordinates of the points in the
matrices returned by themeshgridfunction.
Listing 2.6: Usingmeshgrid
1 >> x = [1 2 3 4];
2 >> y = [5 6 7];
3 >> [xx, yy] = meshgrid(x,y)
4 xx =
5 1 2 3 4
6 1 2 3 4
7 1 2 3 4
8 yy =
9 5 5 5 5
10 6 6 6 6
11 7 7 7 7

Free download pdf