MATLAB: Graphing level curves for an e^x function

homeworkMATLABplotting

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:

Best Answer

  • 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.