![]() Introduction to Matlab |
The ezplot commands (ezplot, ezsurf, ezmesh, ...) and fplot allow the user to plot functions in 2 and 3 dimensions by simply typing in the formula. Matlab allows the user much more control over colour, style, lighting, and surface texture. These more complicated commands are introduced later.
The ez commands are best shown by example. Suppose we wish to plot the function sin(x)/(1+x^2). This may be done simply using
ezplot( 'sin(x)/(1+x^2)' )
If we want to change the range to [0,5], add a second argument to our command.
ezplot( 'sin(x)/(1+x^2)', [0,5] )
Occasionally a function is given implicitly. For example, the circle of radius 2 is defined by the equation x2+y2-4 = 0. This is plotted by
ezplot( 'x^2+y^2-4' )
A better graph is produced when the range is restricted to [-2.5,2.5] × [-2.5,2.5], that is with the command
ezplot('x^2+y^2-4', [-2.5,2.5],[-2.5,2.5])
Alternatively we may remember the circle is defined parametrically as the set of points {2 (cos(t),sin(t)) : 0 £ t £ 2p}. Using this form the circle is plotted as
ezplot( '2*cos(t)', '2*sin(t)' )
|
| (1) |
|
The web site
|
Many interesting curves are expressed simply in polar coordinates, with the
radius given as a simple function of the angle. That is the curve is
obtained parametrically as the set of points
|
ezpolar( 'sin(t)/t' ), [-6*pi,6*pi] )
Of course we could get the same curve by writing out the x and y coordinates explicitly. The cochleoid could have been graphed by
ezplot('(sin(t)/t)*cos(t)','(sin(t)/t)*sin(t)',[-6*pi,6*pi])
The command fplot can also plot functions quickly. For example
fplot('sin',[0,pi]) % Plot sin(x) on [0,pi] fplot('x-x^3/6',[0,2]) % Plot a polynomial.
Although parametric curves or polar coordinates may not be used, fplot has the advantage that two or more functions may appear on the same plot
fplot('[ cos(x), 1-x^2/2, 1-x^2/2+x^4/24]', [-pi,pi])
Now add a title to the graph. Finally find out how to use the command legend to label the three curves.
Curves may exist three dimensions as well as two. If we fly around in a
circle of radius 1 and gain 1 unit of height each circuit we would trace out
the curve
|
ezplot3('cos(2*pi*t)','sin(2*pi*t)','t',[0,4])
Plotting surfaces in 3 dimensions is more interesting. To plot the surface z = f(x,y) where
|
ezsurf( 'x*y*exp(-(x^2+y^2))' )
Some folk prefer to do away with the ugly black lines on the surface. It is also useful to add a color bar, so that it is possible to determine the height of the surface from its colour. To make these modifications, add two extra commands after the ezsurf command.
ezsurf( 'x*y*exp(-(x^2+y^2))' ) % Plot the surface shading interp % Remove the black lines colorbar % Add a colorbar
To see a 3D surface clearly it is necessary to look at it from different angles. Otherwise key features may be hidden. We can change our viewpoint by clicking on the surface with the mouse (a blue outline of a box should appear). Then, without releasing the mouse button, drag the blue outline until the required view has been obtained. Release the mouse button and the surface will be redrawn from the new view. Be warned though, it is easy to get lost.
ezsurfc ezmesh ezmeshc ezcontour ezcontourf
(The syntax of these new commands is the same as ezsurf. But a complete description of the new commands is found using help commands; help ezcontour, help ezmeshc, ... ). Which command and which viewpoint would be best to determine the height of the peaks?
|
Remember a one dimensional curve in 2D may be described parametrically in
the form (x,y) = (x(s),y(s)); that is its two coordinates are a function of
the one variable s. Surfaces may also be described parametrically in the
form
|
|
ezsurf( 's*cos(t)', 's*sin(t)', 't',[.4,1],[0,6*pi] )
ezsurf('s*cos(t)','s*sin(t)','floor(t)',[.4,1],[0,6*pi])
This formula is very complicated to understand; but the graph is interesting as a work of Matlab art. You may need to rotate a little to see what is happening at the origin. I prefer to use the more aesthetic variation. (Note the ... mean the command is continued on the next line. The ... would be left out if the command we typed on one long line.)
ezsurf( '(1-s)*(3+cos(t))*cos(4*pi*s)', ... '(1-s)*(3+cos(t))*sin(4*pi*s)', ... '3*s + (1 - s)*sin(t)', ... [0,2*pi/3,0,12],120 ), shading interp