Plotting Graphs, Surfaces and Curves in Matlab: Plotting A Graph Z F (X, Y) or Level Curves F (X, Y) C
Plotting Graphs, Surfaces and Curves in Matlab: Plotting A Graph Z F (X, Y) or Level Curves F (X, Y) C
>> contour(x,y,z,[-1 0 1 2]) draws the level curves f (x, y ) = 1, f (x, y ) = 0, f (x, y ) = 1 and f (x, y ) = 2. If you want to label the level curves, you can do it like this >> [c,h]=contour(x,y,z); >> clabel(c,h) I dont know of any built in function in Matlab to plot level surfaces f (x, y, z ) = c in the same way as the contour command plots level curves. What you can try to do if you want to plot a level surface f (x, y, z ) = c is to solve for one variable in terms of the others (say x = g (y, z )) and then you can plot a graph as described above. Alternatively, you can try to describe the surface as a parametric surface and proceed as described below.
We create a sequence t of tightly spaced points from 0 to 2 , we calculate the corresponding x(t) and y (t) and nally plot all points (x(t), y (t)) like this: >> >> >> >> t = linspace(0,2*pi,100); x=(sin(t)).^3; y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t); plot(x,y)
Go ahead and try the above example. Its nice :) Example 3. For a 3d curve, everything is the same, except we use the Matlab function plot3 instead of plot. Lets plot the curve t t , y = sin t sin , 10 10 The following commands do the job. x = sin t cos >> >> >> >> >> t = linspace(0,10*pi,1000); x = sin(t).*cos(t./10); y = sin(t).*sin(t./10); z= cos(t); plot3(x,y,z) z = cos t, t [0, 10 ].
In general, it might take some trial and error to nd a suitable number of points to create with the linspace command. If you choose to few, your plot will be a bit jagged. 2
Parametric surfaces
A parametric surface x = x(r, s), y = y (r, s), z = z (r, s), (r, s) [a, b] [c, d],
is plotted very much like we plotted a graph z = f (x, y ) above. In fact, the graph z = f (x, y ) is just a special case of a parametric surface where x and y are used as parameters. So, in general when plotting a parametric surface, instead of making a grid of (x, y )-values and calculating the z -values like we did above, we make a grid of (r, s)-values instead and calculate the corresponding x- y - and z -values. Example 4. We can create a M obius strip (an example of a non orientable surface) as a parametric surface like this: x = (3s cos t) cos 2t, y = (3s cos t) sin 2t, z = s sin t, s [1, 1], t [0, ]
and to plot it we use the commands >> >> >> >> >> >> >> s = linspace(-1,1,30); t = linspace(0,pi,30); [s, t]=meshgrid(s,t); x = (3-s.*cos(t)).*cos(2.*t); y = (3-s.*cos(t)).*sin(2.*t); z = s.*sin(t); mesh(x,y,z)
Try it :) You might have to rotate the surface to get a good view.