Unable to plot exponential graphs properly

6 views (last 30 days)
I want to plot multiple simple exponential graphs in a single graph. At one time, I don't know how I got the correct plot, but later when I have copied the same plot multiple times and still unable tot get the desired results.
t = linspace(-10,10,400); y1 = exp(t); plot(t,y1) hold on t = linspace(-10,10,400); y2 = exp(-t); plot(t,y2) hold on t = linspace(-10,10,400); y3 = exp(2*t); plot(t,y3) legend('y1','y2','y3'). I have attached the unexpected results that I am getting. How can I make it work?

Accepted Answer

John D'Errico
John D'Errico on 12 Sep 2021
Edited: John D'Errico on 12 Sep 2021
What do you reasonably expect to happen?
t varies over the interval -10 and 400. What is exp(-10)? If you don't know, then try it.
exp(-10)
ans = 4.5400e-05
At the other end of that interval, what is exp(400)?
exp(400)
ans = 5.2215e+173
So on the y axis, you are hoping to plaot a curve that varies over many hundreds of powers of 10. What will that curve look like when you plot it? Do you want to guess?
At the same time, you want to plot the function exp(-t). So what does that curve do at the end points?
t = [-10,400];
format long g
exp(-t)
ans = 1×2
22026.4657948067 1.91516959671401e-174
So this curve varies also over many hundreds of powers of 10, but it never grows lareger than roughly 22000.
How about exp(2*t)?
exp(2*t)
ans = 1×2
2.06115362243856e-09 Inf
That one is even more extreme, since at the high end, it exceeds that largest number MATLAB can represent in double precision, a little larger the 10^300.
realmax
ans =
1.79769313486232e+308
Now look at the y axis in the plot you generated. At the top left corner of that plot, do you see the x10^307? That tells you the y axis is scaled by that power of 10.
So seriously, what do you expect to see in that figure? MATLAB does its best to plot what you told it to plot, all in one figure. But remember the exponential function grows very rapidly. In fact, it grows exponentially rapidly.
Instead, you might have chosen a very much narrower range for t.
For example...
t = linspace(-2,2);
plot(t,exp(t),'r-',t,exp(-t),'g--',t,exp(2*t),'b:')
legend('exp(t)','exp(-t)','exp(2*t')

More Answers (1)

Alan Stevens
Alan Stevens on 12 Sep 2021
One possibility
t = linspace(-10,10,400);
y1 = exp(t);
y2 = exp(-t);
y3 = exp(2*t);
plot(t,y1,t,y2,t,y3)
legend('y1','y2','y3')
though you would be better off plotting them using separate graphs (see subplot) because of the scaling differences.

Categories

Find more on Line Plots 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!