Graphing level curves for an e^x function
6 views (last 30 days)
Show older comments
Malena Vasquez
on 1 May 2020
Commented: Malena Vasquez
on 1 May 2020
Hi everyone!
For a school project I'm trying to graph the function
and its level curves. Using matlab is not required but I'm trying to learn it so as to improve the presentation of my work. I've attempted to do this but for some reason the plot isn't looking as it should so I'm guessing there's a problem in my code. Any help would be appreciated.
my code:
[x,y]=meshgrid(-5:.1:5);
z=exp(x.*y);
z=surfc(z);
output:

1 Comment
Accepted Answer
John D'Errico
on 1 May 2020
Edited: John D'Errico
on 1 May 2020
You did just fine. What you don't recognize is the function you are trying to plot varies by so many order of magnitudes.
That is, what is exp(5*5)=exp(25)? TRY IT!
exp(25)
ans =
7.2005e+10
The answer is, a beeeg number. In fact, in the opposite corner, when both x and y are -5, you get the same thing.
However, most of that surface you plotted is composed of far more reasonably small numbers. In fact, much of its is downright tiny. Still always non-negative, but really small.
exp(-5*5)
ans =
1.3888e-11
So in two corners of that square region, you expect a huge number. In the other two corners, a really tiny number. Right in the midlle, what would you expect? exp(0) is....?
But what happens when you try to plot that as a surface? All you see are two huge peaks, and the rest looks pretty flat in comparison. After all, what is the difference between an elevation of 1e-11 feet and 1 foot, when you are looking down from a satelite in geostationary orbit? The actual case you are plotting is worse than that, in fact.
You used surfc. It tries to plot contours on the floor, underneath the surface. EXCEPT that here, almost the entire surface is colored a dark blue, and you cannot see the floor underneath. Will you even see any contour lines? Why? Think about the shape of that surface again.
How can you fix this? You might restrict the region to a somewhat smaller one. That would help. Or, you might use a log transformation on the z axis.
[x,y]=meshgrid(-5:.1:5);
z=exp(x.*y);
Hsurf = surfc(x,y,log10(z));
Hint: don't reuse z there as the output of surf.

3 Comments
John D'Errico
on 1 May 2020
As I created the surface, you do see the level curves, even though I logged the value of z. After all, z being constant is the same thing as log(z) being constant. The respective constants will not be the same.
The reason you don't see those level curves is simple. You hid them. Try this instead:
Hsurf = meshc(x,y,z);
view(-45,80)

I've made the plot fairly large here, and then oriented it so we can see the level lines as drawn.
Look down at the VERY BOTTOM of that plot, in the corner where x and y are both very near -5. Do you see some horizontal lines that are essentially underneath the surface? Those are level lines of the surface as you want to create it, in the un-logged form. Again, when you stand on top of immense mountains, the rest of the world looks flat by comparison. But there are a few lines drawn that can be seen.
Another issue is, the contour plot routine will generate contours that are LINEARLY spaced in Z.
Hsurf(2)
ans =
Contour with properties:
LineColor: 'flat'
LineStyle: '-'
LineWidth: 0.5
Fill: 'off'
LevelList: [1e+10 2e+10 3e+10 4e+10 5e+10 6e+10 7e+10]
XData: [101×101 double]
YData: [101×101 double]
ZData: [101×101 double]
So 7 countours were generated, at Z == [1e+10 2e+10 3e+10 4e+10 5e+10 6e+10 7e+10] respectively. But what happens is all of those contours are tucked tightly into two corners. Here, I'll just create a simple contour plot of z:
Hcont = contour(x,y,z);

Again, everything that is happening lives right in those corners, because the contour levels chosen are linearly spaced, essentially between 0 and 7e10.
Now, look at what you see of the contours when you plot the log of that surface.
contour(x,y,log(z))

This is always an issue to beware of when you try to visualize or do any work with something that varies by so many orders of magnitude. The log transformation can make stuff that is otherwise ill-behaved suddenly work nicely.
More Answers (1)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!