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

4 ビュー (過去 30 日間)
Camille Maradiaga Ponce
Camille Maradiaga Ponce 2019 年 9 月 5 日
コメント済み: Guillaume 2019 年 9 月 6 日
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
Camille Maradiaga Ponce 2019 年 9 月 5 日
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 Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by