32 Matla Brick

3
Math 32 - Plotting Graphs in 3-D with MatLab A computer can be useful in helping us visualize graphs of functions from R 2 to R. As you go through this, use as an example the function f (x, t) = cos(t)sin(x) which describes a vibra ting guita r string. Here, x describes the position along the string (we assume that the string vibrates in one direction only), and t denotes ti me. In our (twisted) units, the length of the string, from the nut to the bridgte, is π. We will graph this function and some contours using the software package called “Matlab” which is inst alled on the computers in our lab on the second oor of Millik an. Once you’ ve found a free computer in the lab, log on (use your normal Pomona account) and click on the Matlab icon on the desktop. You sho uld see wha t’s called the ”Command Window”, and you sho uld see the “ >>prompt. After this prompt type: x = 0 : pi/20 : pi; follo wed by a carriage return . This giv es the range of the x-variable and the step size: x will be incremented by π/20 from 0 to π. To make a mor e detail ed graph, decr eas e the step size. Now type: t = 0 : pi/20 : 2 pi; The “*” means multiply. You must put this in: “ 2 pi” doesn’t work. The semi-colon tells the program not to print out all the v alues on the screen: you can check out what happens if you leave it o!! If you make a mistake while typing, the UP-ARROW repeats your last line - just x your mistake and type “return”. Now you want to make matrices - or arrays - out of these lists of values to create a grid of points. Thi s is the form of data that Matl ab needs in order to dra w the grap h for you - it is similar to generating the tabular representation of a function of two variables as in table 12.1 in your text. Type: [X, T] = meshgrid (x, t); Then we need to compute the function values. Type: 1

Transcript of 32 Matla Brick

Page 1: 32 Matla Brick

8/7/2019 32 Matla Brick

http://slidepdf.com/reader/full/32-matla-brick 1/3

Math 32 - Plotting Graphs in 3-D with MatLab

A computer can be useful in helping us visualize graphs of functions from R2 to R. Asyou go through this, use as an example the function

f (x, t) = cos(t)sin(x)

which describes a vibrating guitar string. Here, x describes the position along the string(we assume that the string vibrates in one direction only), and t denotes time. In our(twisted) units, the length of the string, from the nut to the bridgte, is π.

We will graph this function and some contours using the software package called “Matlab”which is installed on the computers in our lab on the second floor of Millikan. Once you’vefound a free computer in the lab, log on (use your normal Pomona account) and click on

the Matlab icon on the desktop.

You should see what’s called the ”Command Window”, and you should see the “>>”prompt. After this prompt type:

x = 0 : pi/20 : pi;

followed by a carriage return. This gives the range of the x-variable and the step size: xwill be incremented by π/20 from 0 to π. To make a more detailed graph, decrease thestep size. Now type:

t = 0 : pi/20 : 2 ∗ pi;

The “*” means multiply. You must put this in: “ 2 pi” doesn’t work. The semi-colon tellsthe program not to print out all the values on the screen: you can check out what happensif you leave it off!! If you make a mistake while typing, the UP-ARROW repeats your lastline - just fix your mistake and type “return”.

Now you want to make matrices - or arrays - out of these lists of values to create a gridof points. This is the form of data that Matlab needs in order to draw the graph for you

- it is similar to generating the tabular representation of a function of two variables as intable 12.1 in your text. Type:

[X,T] = meshgrid (x, t);

Then we need to compute the function values. Type:

1

Page 2: 32 Matla Brick

8/7/2019 32 Matla Brick

http://slidepdf.com/reader/full/32-matla-brick 2/3

F = sin(X). ∗ cos(T);

Be sure to use the upper case letters, because they give you the whole grid, rather than asingle list. The “.*’ stands for “element-by-element” multiplication: each element in the X grid is multiplied by each element in the T  grid - after taking sines and cosines. If you justtype “*”, the normal symbol for multiplication, you get MATRIX  multiplication which isnot what you want! More about matrices in Math 60. For future reference, “./” and “.ˆ ” give element-by-element division or powers. Addition and subtraction don’t need aperiod in front of them.

Almost done. To draw the graph, type:

mesh(x, t,F);

The graph is drawn in a special graphing window. Maybe you can see it right away, butsometimes it is hidden. To get that window in front (so you can see it) select “Figure 1” inthe pull-down “Windows” menu. You’ll then need to get back to the Command Windowby clicking on it, or by selecting it from the Windows menu in turn.

Experiment with different grid sizes by changing the step size in your first two lines.(Remember, you can avoid retyping by pressing the UP ARROW until you get the lineyou wanted. Fix it, if necessary, and type “return” to enter it. You still need to enter allthe commands in the right order!

You can view your graph from different angles by using the ’rotate’ feature (the buttonwith the circular arrow icon). After clicking on the icon, put your cursor on the figure androtate it.

Check out this feature!

You can plot the graph as a filled in surface by using the “surf” command instead of the“mesh” command:

surf (x, t,F);

You can also label the axes by using the commands:

xlabel(’X-Axis’);

or

ylabel(’TIME’);

2

Page 3: 32 Matla Brick

8/7/2019 32 Matla Brick

http://slidepdf.com/reader/full/32-matla-brick 3/3

etc. If you have an account, you can print out a copy of your graph by selecting “Print”in the pull-down File menu. Please use the printer judiciously - i.e. only when it’s reallyuseful.

To get a contour plot in MATLAB, you create the values of the function F  in the same

way as before. You already have those values, so you’re in good shape!! Instead of typing“mesh”, however, you type:

contour(x,t,F);

You will see a contour graph of F  with x on the horizontal axis and t on the vertical axis.If you want more contour lines, type:

contour(x,t,F,20);

which gives you 20 contour lines, or enter as many lines as you want in the last spot above.

For something weirder, try:

contour3(x,t,F,20);

which gives a 3-D contour plot. You can also specify which contours to draw. For example,to draw contour lines f (x, t) = 0,−1/2, 1/2,−1, 1 in the xt-plane, type:

contour(x,t,F, [0,−1/2, 1/2,−1, 1]);

One last detail: if you want the computer to label the contours, type the following:

[c,h]=contour(x,t,F, [0,−1/2, 1/2,−1, 1]);

and then

clabel(c,h);

If at any time you want to see more details, there is on-line help. Just type help andyou will see a list of topics from which to choose, or type help contour to get help on aparticular command such as contour. Have fun, but don’t get hooked on 3D graphing!!

3