MATLAB doesn't let me plot 3 graphs in the same plot

2 次查看(过去 30 天)
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)')

回答(3 个)

Guillaume
Guillaume 2019-9-5
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 个评论
Guillaume
Guillaume 2019-9-6
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
Fabio Freschi 2019-9-5
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
  1 个评论
Camille Maradiaga Ponce
Thanks you. How can I take all the answers for ve and vi and make them vectors in order to plot them?

请先登录,再进行评论。


Fabio Freschi
Fabio Freschi 2019-9-5
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 个评论
Fabio Freschi
Fabio Freschi 2019-9-5
It is the plot of your solution. I don't know the problem so I can't comment on the results.
Guillaume
Guillaume 2019-9-6
Rather than postive massive (!) screenshots, export your figure as an image (png) and insert that.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by