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

조회 수: 2 (최근 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 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