Unable to plot exponential graphs properly
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Shweta Kiran
am 12 Sep. 2021
Bearbeitet: John D'Errico
am 12 Sep. 2021
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?
0 Kommentare
Akzeptierte Antwort
John D'Errico
am 12 Sep. 2021
Bearbeitet: John D'Errico
am 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)
At the other end of that interval, what is exp(400)?
exp(400)
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)
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)
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
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')
0 Kommentare
Weitere Antworten (1)
Alan Stevens
am 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!