Introduction to Matlab

Mathematics University of Queensland Mathematics

8  3D Graphics

8.1  3D Basics

We have already seen that Matlab is able spectacular 3 dimensional plots by just typing in the formula for the surface. To undertake more complicated graphing tasks we have to be explicit obout the coordinates of our surface. This means we have to use the component-wise operators and set up matrices for the x and y coordinates This is only a little more complicated than the 2D case.

To draw the surface of f(x,y) = xyexp(-(x^2+y^2)) for -2 £ x £ 2, 0 £ y £ 2, use the commands.

    x=(-2:.2:2)' ;  y = (0:.2:2)' ;
    [ X, Y ] =  meshgrid(x,y) ; 
    Z  =  X .* Y .* exp( -( X.^2 + Y.^2 ) ) ;
    surf(X,Y,Z)
    xlabel('X')
    ylabel('Y')          % Label axes to avoid confusion!
    zlabel('Z')

To let us to `walk' around the plot to see things from different angles give the command

    rotate3d

(or press the rotation button on the figure's toolbar). Once this command has been given, click anywhere on the plot without releasing the mouse button. A box appears. Dragging the mouse rotates the box. When the button is released, the graph is redrawn from the new angle. Experiment a little!

The following commands draw slightly different mesh plots of the same function.

     surfl(X,Y,Z) , surfc(X,Y,Z)

Instead of showing a complete surface, a`wire frame' is often faster to compute. This is the purpose of the three mesh commands:-

   mesh(X,Y,Z) meshz(X,Y,Z) meshc(X,Y,Z)

Exercise 25

 

(a.) Draw the above mesh and surface plots with a finer grid. (Use an 80×40 grid rather than the 20×10 grid above.) Experiment with the three variants of the mesh commands and the three variants of the surf commands.

(b.) Matlab can vary the colours . Look for a way to draw the surface in cool colors. How can one spin the map?

(c.) Use the help command or the help window to find out how to draw a curve in 3-space. (More help will be found under the topic graph3d). Plot the curve (r(t)sin(t), r(t)cos(t), t), r(t) = 1/(1+(t/p)2 for 0 £ t £ 10p. Try not to use ezplot3.

8.2  Advanced Options

In three dimensions it is more difficult to draw a plot that is pleasing to the eye and shows all the important features of a function.

Exercise 26

Use the graf3d interactive demonstration to investigate the function of two variables calculated by the Matlab peaks command. (Hint: By typing the command rotate3d into the command box you can rotate the surface interactively.) From appropriate plots

(a.) Find a plot which shows clearly there are 3 local maxima. Choose a color scheme that can be used to determine the value of the maxima as accurately as possible.

(b.) Find another graph that shows as clearly as possible the locations of all local maxima and minima. (If you are submitting answers to this exercise, show the tutor your graphs on the screen, and hand in the commands you use to generate the plots.)