0% found this document useful (0 votes)
86 views3 pages

Plotting Graphs, Surfaces and Curves in Matlab: Plotting A Graph Z F (X, Y) or Level Curves F (X, Y) C

The document discusses various ways to plot graphs and curves in Matlab, including: 1) Plotting graphs of the form z=f(x,y) by generating a grid of x and y values, calculating the z values, and using commands like mesh() or surf(); 2) Plotting level curves f(x,y)=c using contour(); 3) Plotting parametric curves and surfaces by generating parameter values, calculating the x, y, and z coordinates, and using plot(), plot3(), or mesh(); 4) Plotting vector fields using a grid, calculating components, and quiver() or quiver3(). Examples are provided for each type of plot.

Uploaded by

Shweta Sridhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views3 pages

Plotting Graphs, Surfaces and Curves in Matlab: Plotting A Graph Z F (X, Y) or Level Curves F (X, Y) C

The document discusses various ways to plot graphs and curves in Matlab, including: 1) Plotting graphs of the form z=f(x,y) by generating a grid of x and y values, calculating the z values, and using commands like mesh() or surf(); 2) Plotting level curves f(x,y)=c using contour(); 3) Plotting parametric curves and surfaces by generating parameter values, calculating the x, y, and z coordinates, and using plot(), plot3(), or mesh(); 4) Plotting vector fields using a grid, calculating components, and quiver() or quiver3(). Examples are provided for each type of plot.

Uploaded by

Shweta Sridhar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Plotting graphs, surfaces and curves in Matlab

October 10, 2012

Plotting a graph z = f (x, y ) or level curves f (x, y ) = c


A graph z = f (x, y ) is most easily plotted in Google, but should you want to do it in Matlab, we can achieve it this way. Example 1. Lets say we want to plot the graph z = x2 y 2 for x [2, 2], y = [2, 2]. We rst create two sequences x and y with tightly spaced values from 2 to 2. >> x=linspace(-2,2,30); >> y=linspace(-2,2,30); The number 30 above indicates the number of points we create. You can experiment with bigger or smaller values. Next, using these two vectors, we create a grid of points [x,y] in the rectangle [2, 2] [2, 2]. >> [x,y]=meshgrid(x,y); The above command overwrites our old x and y. We also calculate the z coordinate for each point in the grid as the function value f (x, y ). >> z=x.^2-y.^2; We can now plot the points weve created. You can try either >> mesh(x,y,z) or >> surf(x,y,z) If instead of the graph z = f (x, y ) we wish to plot level curves f (x, y ) = c, the command >> contour(x,y,z) draws such level curves for a few dierent values of c. If youre not happy with the result, you can specify the number of level curves drawn, for example >> contour(x,y,z,10) draws 10 level curves. You can also specify exactly which level curves you want to draw. For example 1

>> 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.

Plotting a parametric curve or parametric surface


Parametric curves
Plotting a 2D parametric curve x = x(t), or a 3d parametric curve x = x(t), is straightforward. Example 2. Lets say we want to plot the curve, x = sin3 t, y = 13 cos t 5 cos 2t 2 cos 3t cos 4t, t [0, 2 ]. y = y (t), t [a, b], y = y (t), t [a, b],

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.

Plotting a vector eld


We can also plot vector elds. To plot a 2D vector eld, F(x, y ) = P (x, y )i + Q(x, y )j, we create a grid of (x, y ) values, evaluate the corresponding function values P and Q, and plot the vector eld with the quiver command. Example 5. To plot the vector eld F(x, y ) = y i + xj, for (x, y ) [5, 5] [5, 5], we use the commands >> >> >> >> >> >> x = linspace(-5,5,30); y = linspace(-5,5,30); [x,y] = meshgrid(x,y); P = -y; Q = x; quiver(x,y,P,Q) There is also a quiver3 command for plotting 3D vector elds. 3

You might also like