# Homework Help: Really need help with MATLAB, anyone.

1. Jan 21, 2012

### animboy

1. The problem statement, all variables and given/known data

Hi, I really need help on this MATLAB assignment, I appreciate it. The Assignment requires us to plot a function that has two local maxima and no other critical points. I will present all my work here so you can see for yourself.

2. Relevant equations

The function is as you see on the sheet.

http://desmond.imageshack.us/Himg338/scaled.php?server=338&filename=71437941.png&res=medium [Broken]

3. The attempt at a solution

Firstly we are supposed to plot contours for this function, I've done that already. Next we are supposed to plot the level curve on which the two maxima lie, we do this by finding the equation for the line (in the xy plane) that joins the two maxima and using that as the restriction for the y-variable (in this case it is y = x + 1). Anyway, i've also done that. Here is that plot from two angles:

http://desmond.imageshack.us/Himg32/scaled.php?server=32&filename=graphtop.png&res=medium [Broken]

http://desmond.imageshack.us/Himg193/scaled.php?server=193&filename=graphslant.png&res=medium [Broken]

However my problem is with the last part. Where we are supposed to just "plot the straight line equation that joins the two maxima", on the same plot. How would I do that on Matlab? Here is my code for the first two parts.

[x,y]=meshgrid(-2:0.02:2, -1:0.02:3);
z = -(x.^2 -1).^2 -(x.^2.*y -x -1).^2;
w = x + 1;
q = -(x.^2 -1).^2 -(x.^2.*w -x -1).^2;
contour3(x, y, z, [-2 -0.5 -0.1], 'b')
hold on
mesh(x, w, q)

Another problem is, as you can see, I used 'x', 'y' and 'z' to plot the contour. But defined two new variables 'q' and 'w' to plot the level curve. I have a feeling this method is wrong as my assignment warns against it. It says that the extra code to plot the level curve cannot just be added on top of the code required to plot the contour. However the code I wrote works and you could write it in just that way! as this:

[x,y]=meshgrid(-2:0.02:2, -1:0.02:3);
z = -(x.^2 -1).^2 -(x.^2.*y -x -1).^2;
contour3(x, y, z, [-2 -0.5 -0.1], 'b')
hold on
w = x + 1;
q = -(x.^2 -1).^2 -(x.^2.*w -x -1).^2;
mesh(x, w, q)

Unless it's talking about the third part about plotting the straight line.

Here is the original assignment:

http://img714.imageshack.us/img714/8382/60081545.png [Broken]

Last edited by a moderator: May 5, 2017