MATLAB doesn't let me plot 3 graphs in the same plot
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I am using the hold on command but matlab still wont let me have 3 graphs in the same plot. I am solving ode with eulers step size 1 and 5 and also with ode45.
------------------------------------------
h=1; %step size of 1
tm=(0:h:20);%t0=0 and tEnd=20
ve=zeros(size(tm)); %v modified in terms of t
ve(1)=500; %v0=500L
n=numel(ve); %amount of v values
for i=1:n-1
f=10*exp(-tm(i)/20)-11;
ve(i+1)=ve(i)+h*f; %applying Eulers method
end
htwo=5; %step size of 5
tn=(0:h:20);%t0=0 and tEnd=20
vi=zeros(size(tm)); %v modified in terms of t
vi(1)=500; %v0=500L
ne=numel(vi); %amount of v values
for i=1:ne-1
f=10*exp(-tn(i)/20)-11;
ve(i+1)=vi(i)+htwo*f; %applying Eulers method
end
tspan = [0 20]; %t0=0 and tEnd=20
v0 = 500; %initial volume
[t,v] = ode45(@(t,v) 10*exp(-t/20)-11, tspan, y0) %applying ode45
figure(1)
plot(t,v); grid on
hold on
plot(tm,ve(i+1),'green')
hold on
plot(tn,vi(i+1))
hold off
ylim([400 500])
xlim([0 20])
title(' Volume Change on Reservoir over 20 Days')
xlabel('time (days)')
ylabel('volume(L)')
0 Kommentare
Antworten (3)
Guillaume
am 5 Sep. 2019
Matlab plots exactly what you ask it.
plot(tm,ve(i+1),'green')
will plot the scalar value ve(21) (since after the loop i is 20) at each tm value as a single tiny green dot. Green is not the most visible colour, so you probably can't see it.
plot(tm, ve(i+1), 'g*')
would let you see the points better, but probably you meant to plot
plot(tm, ve)
I suggest you let matlab choose the colour as the defaults a lot better than 'green'. Also note that you only need one hold on. So your code could be:
plot(t, v);
hold on;
plot(tm, ve);
plot(tn, vi);
or you could everything with just one plot call and no need for hold on:
plot(t, v, tm, ve, tn, vi);
1 Kommentar
Guillaume
am 6 Sep. 2019
Note that if the above doesn't result in the expected plot, then you've made a mistake with your equations, it's nothing to do with the plot functions. As we don't know what your equations are meant to do, it's not really something we can help with at present.
Fabio Freschi
am 5 Sep. 2019
The problem is here
plot(tm,ve(i+1),'green')
...
plot(tn,vi(i+1))
ve(i+1) is a number, as well as vi(i+1), while tm and tn are vectors
Fabio Freschi
am 5 Sep. 2019
As Guillame pointed out, you just have to plot them (without the indexing):
h = figure;
hold on, grid on
plot(t,v);
plot(tm,ve);
plot(tn,vi);
ylim([400 500])
xlim([0 20])
title(' Volume Change on Reservoir over 20 Days')
xlabel('time (days)')
ylabel('volume(L)')
4 Kommentare
Fabio Freschi
am 5 Sep. 2019
It is the plot of your solution. I don't know the problem so I can't comment on the results.
Guillaume
am 6 Sep. 2019
Rather than postive massive (!) screenshots, export your figure as an image (png) and insert that.
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations 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!
