Vectors -Must- be the same length

조회 수: 1 (최근 30일)
Gregory Mendes
Gregory Mendes 2021년 5월 24일
댓글: Star Strider 2021년 5월 24일
So the problem here is line 38, where it would say
"Error using plot
Vectors must be the same length.
Error in x (line 37)
plot(t,I)"
And I know if it is in line 37, then it would likely be in line 47 as well. But I cannot see what is proccing the error message.
% Equations : dS/dt = -r S I
% dR/dt = a I
% dI/dt = -dS/dt - dR/dt - b I
% time measured in days
%S=susceptibles , R=recovereds, I=infecteds.
% L=(S+R+I)(0)- (S+I+R)(t)
dt= 1.3 %time change
%r=1e-7
r=5e-8
a=0.05
%b=a*0.01
b=a*0.04
S0=8419000.
R0=0
I0=100
tmax=120 %number of days
popscale=8419000 %population scale for graphing
t=0:dt:tmax ;
S(1)=S0;
R(1)=R0;
I(1)=I0;
itmax=ceil(tmax/dt)
for it=2:itmax+1
dSodt(it)=-r*S(it-1)*I(it-1);
S(it)=S(it-1)+dSodt(it)*dt;
dRodt(it)=a*I(it-1);
R(it)=R(it-1)+dRodt(it)*dt;
dIodt(it)=-dSodt(it)-dRodt(it)-b*I(it-1);
I(it)=I(it-1)+dIodt(it)*dt;
end
L=(S0+I0+R0)-(S+I+R);
%plot(t,S)
%axis([0 tmax 0 popscale])
%xlabel('time in days')
%ylabel('Number Susceptible')
%title('Epidemic simulation of NYC from near start')
plot(t,I)
axis([0 tmax, 0 popscale])
xlabel('time in days')
ylabel('Number Infected')
title('Epidemic simulation of NYC from near start')
%plot(t,R)
%axis([0 tmax 0 popscale])
%xlabel('time in days')
%ylabel('Number Recovered')
%title('Epidemic simulation of NYC from near start')
popscale=500000
plot(t,L)
axis([0 tmax, 0 popscale])
xlabel('time in days')
ylabel('Number Lost')
title('Epidemic simulation of NYC from near start')

답변 (1개)

Star Strider
Star Strider 2021년 5월 24일
Change the for loop to:
for it=2:itmax
dSodt(it)=-r*S(it-1)*I(it-1);
S(it)=S(it-1)+dSodt(it)*dt;
dRodt(it)=a*I(it-1);
R(it)=R(it-1)+dRodt(it)*dt;
dIodt(it)=-dSodt(it)-dRodt(it)-b*I(it-1);
I(it)=I(it-1)+dIodt(it)*dt;
end
and it works!
The problem was the '+1'.
.
  댓글 수: 5
Walter Roberson
Walter Roberson 2021년 5월 24일
you must have initialized t or I to a different size.
Star Strider
Star Strider 2021년 5월 24일
@Walter Roberson — Thank you!
@Gregory Mendes — The code you are having problems with is obviously not the code you posted.
.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by