Sie sind auf Seite 1von 4

MATLAB assignment

Math 222, Fall 2011

GRAPHING PROBLEMS!!!

Part I: A simple graph

Assignment! Problem 1: First simply plot the function

y = x 3 x 2 + 6x sin(5x) 9x ,

on the interval 4 x 4. (If you cannot do this, then you need to review the intro packet that I created!)

Part II: A curve in 3-D

Time for some three dimensional graphics. Here is some MATLAB code I wrote which draws a curve in three dimensional space:

% A curve in three dimensions t = [0:0.01:10*pi];

plot3(cos(t),sin(t),4*t);

First save this program into an m-file and run it. Next, in the figure window above the figure click on the button with the counter-clockwise arrow. It is just to the right of the button with a hand on it. Rotate the figure into a few different positions so that you can see it well.

Assignment! Problem 2: Change the code to give you a look at the curve:

⃗r(t) := (t cos(2t), t sin(2t), 5t),

for

0 t 12π .

Put comments into your code explaining what each line does. Print the curve from a couple of different angles. Submit these pictures and your code.

Part III: Surfaces

The next bit of fun. Here is some MATLAB code I wrote which draws a surface in three dimensional space:

% A surface in three dimensions [x,y] = meshgrid(-3:0.05:3); z = -0.1*y + y.*exp(-x.2-y.2); mesh(x,y,z);

If you cannot guess what the command meshgrid is doing (and probably even if you can), then I recommend typing:

[u,v] = meshgrid(-2:0.5:2) (No semicolon here!)

in the main MATLAB window. Hopefully the definition of z now makes clear the fact that I am working with the function

z = .1y + ye x 2 y 2 .

The last line which uses the command mesh is the line that draws this thing.

In fact there are a zillion options when it comes to how to graph this type of function, and I recommend messing around and replacing the line where I called the mesh command with

contour3(x,y,z,40);

or

surf(x,y,z);

or

surfc(x,y,z);

or even all of the following

surf(x,y,z,’FaceColor’,’green’,’EdgeColor’,’none’); camlight left; lighting phong;

You can learn a lot more about your options by sticking any of these com- mands into the search feature of the help menu. (You may certainly want to

look up meshgrid in particular so that you learn the syntax to make the x bounds different from the y bounds.)

Assignment! Problem 3: Change the code to give you a look at any of the interesting functions in the textbook of the form z = f (x, y). (“Interesting” in this context means that at least two critical points should fit in your pic- ture. Also, don’t choose a quadric surface.) Do this in a domain where the bounds for x are different from the bounds for y. (In other words, you will need to use the help menu to figure out how to use the command meshgrid.) Print your surface from at least two different angles, and use at least two of the commands mentioned above. Submit these pictures and your code. Write down (in pen on what you submit or in a comment within your code) which function you are graphing and on what domain you graphed it.

Now sometimes it is more convenient to plot things by using polar coordi- nates. Here is some code that I found online and adapted to my purposes to create graphs for the first test:

%z = x2 + y2

r=linspace(0,2,20);

theta=linspace(0,2*pi,40);

[r,theta]=meshgrid(r,theta); x=r.*cos(theta); y=r.*sin(theta);

z=r.2;

mesh(x,y,z)

xlabel(’x-axis’)

ylabel(’y-axis’)

zlabel(’z-axis’)

In order to understand this code one should look up “linspace” in the help menu, and there one will find (amongst many other things):

“y = linspace(a,b,n) generates a row vector y of n points linearly spaced between and including a and b.”

It seems to me as if one could simply use instead: y = [a:(b-a)/(n-1):b], but I think that the linspace command is less confusing.

Of course, many of the graphs on the first test were not graphs of functions (they failed the vertical line test), and so I needed to use more complicated code which I also found online. For example, to graph my sphere, I used the code:

% x2 + y2 + z2 = 9

r=linspace(0,3,20);

theta=linspace(0,2*pi,40);

[r,theta]=meshgrid(r,theta); x=r.*cos(theta); y=r.*sin(theta); z1=sqrt(9 - r.2); z2=-sqrt(9 - r.2);

mesh(x,y,z1)

hold on

mesh(x,y,z2)

xlabel(’x-axis’)

ylabel(’y-axis’)

zlabel(’z-axis’)

Assignment! Problem 4: Change this last piece of code to graph the hyperboloid of two sheets determined by:

z 2 = x 2 + y 2 + 4 .

Print it out from a few different angles. Submit your pictures and your code.

Assignment!

the hyperboloid of one sheet determined by:

Bonus Problem: Change this last piece of code to graph

z 2 + 4 = x 2 + y 2 .

Print it out from a few different angles. Submit your pictures and your code. Explain your calls to linspace by putting comments into your code.