Introduction to Matlab

Mathematics University of Queensland Mathematics

4  Simple Plots of Functions

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.

4.1  2D Plots

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)' )

Exercise 5

  1. A nice example of a function that would be difficult to graph without a computer or calculator is
    f(x) = x(xx)-(xx)x.
    Plot f(x). (Hint:- By itself, ezplot uses the range -2p £ x £ 2p. This is inappropriate here: a better choice is to try the range 0 £ x £ 2.)

  2. A Lissajous curve is a curve of the form
    x = sin(nt+c),    y = sin(t)
    (1)
    Play with small integer values of n = 1,2,... and then find values of n and c so the Lissajous curve becomes the ABC symbol.

  3. And finally graph the implicitly defined function
    (y2-x2)(x-1)(2x-3) = 4(x2+y2-2x)2.

The web site
http://mathworld.wolfram.com/topics/GeneralPlaneCurves.html
has a rich collection of interesting curves to draw.

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
(x,y) = ( r(q)cos(q), r(q)sin(q))
for some simple function r(q). For example, the cochleoid (meaning `snail like') uses the formula r(q) = sin(q)/q. The resulting curve is plotted in Matlab as

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])

Exercise 6

  1. From the help for the fplot command, find out how to change the range of the y axis on the previous plot. For example, a range of -1.1 £ y £ 1.1 looks better.

  2. Use fplot to do the following.

    1. Plot the function ex between -1 and 1.

    2. On the same graph plot ex and the polynomial 1+x between -1 and 1.

    3. Finally on the same graph plot ex, 1+x, and 1+x+x2/2+x3/6 between -1 and 1. Why are these last two polynomials good approximations to ex?

    Now add a title to the graph. Finally find out how to use the command legend to label the three curves.

4.2  3D Plots.

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
( x,y,z) = ( sin(2pt),cos(2pt),t)        t ³ 0.
To show the flight path up to the height z = 4, use the command

ezplot3('cos(2*pi*t)','sin(2*pi*t)','t',[0,4])

Exercise 7

  1. Use help ezplot3 to findout how to animate this curve.

  2. What is the angle the flight path makes with the horizontal? (This is a thinking question; there is no simple Matlab command to help!)

  3. How would we plot the path if we gained 2 units of height for each circuit?

Plotting surfaces in 3 dimensions is more interesting. To plot the surface z = f(x,y) where
f(x,y) = xyexp(-(x2+y2))
use the Matlab command

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.

Exercise 8

Plot the function xyexp(-(x2+y2)) using the following commands instead of easysurf.

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?

Exercise 9

(For students of multivariate calculus only.) Produce a nice graph which demonstrates as clearly as possible the behaviour of the function
xy2/(x2+y4)
near the point (x,y) = (0,0). This is surprisingly difficult. Remember, by default, Matlab plots surfaces on a 60×60 grid, and many of the finer features of a function are lost. If your computer has enough memory it is possible to increase the number of grid points for a smoother graph.

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
(x,y,z) = ( x(s,t),y(s,t),z(s,t))
The three coordinates are functions of 2 variables. For example a spiral ramp is described by
{ ( scos(t),ssin(t),t)  : .4 £ s £ 1,0 £ t £ 6p}
This is graphed by

ezsurf( 's*cos(t)', 's*sin(t)', 't',[.4,1],[0,6*pi] )

Exercise 10

Use the floor command to plot a spiral staircase

ezsurf('s*cos(t)','s*sin(t)','floor(t)',[.4,1],[0,6*pi])

Exercise 11

Explain how the Lissajous curve in the previous section, is related to the curve (x,y,z)  =  (cos(nt),sin(nt),sin(t)). (Hint: Look at the 3D curve from different angles.)

Exercise 12

Use the help surf command and look at the very last example. (It is spread over two lines). Cut and paste these two lines from the help text to the current command line (i.e. after the last > > ) . Now press return and look at the graph. (You could of course type in the whole command; but a cut and paste is more reliable.)

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

Exercise 13

Plot a sphere. (Hint: Use a parametric 3D plot. At height t the sphere is a circle of radius ... ?)

Exercise 14

Plot a swirl. This is a surface whose height above the point (x,y) = (rcosq,rsinq) is sin(6cos(r)-nq).