MATLAB Answers

Unable to plot exponential graphs properly

9 ビュー (過去 30 日間)
Shweta K Totla
Shweta K Totla 2021 年 9 月 12 日
編集済み: John D'Errico 2021 年 9 月 12 日
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?

採用された回答

John D'Errico
John D'Errico 2021 年 9 月 12 日
編集済み: John D'Errico 2021 年 9 月 12 日
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')

その他の回答 (1 件)

Alan Stevens
Alan Stevens 2021 年 9 月 12 日
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.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by