Vectors -Must- be the same length

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')

Answers (1)

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 Comments

I tried that. I got the same error.
I tried it and it worked.
Try this —
% 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
dt = 1.3000
%r=1e-7
r=5e-8
r = 5.0000e-08
a=0.05
a = 0.0500
%b=a*0.01
b=a*0.04
b = 0.0020
S0=8419000.
S0 = 8419000
R0=0
R0 = 0
I0=100
I0 = 100
tmax=120 %number of days
tmax = 120
popscale=8419000 %population scale for graphing
popscale = 8419000
t=0:dt:tmax ;
S(1)=S0;
R(1)=R0;
I(1)=I0;
itmax=ceil(tmax/dt)
itmax = 93
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
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
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')
.
I did all of the edits, tested it out myself. Removed code and re-added it to see if there was an issue outside of it.
It still ends up giving me the same error:
Error using plot
Vectors must be the same length.
Error in epidemic2 (line 38)
plot(t,I)
you must have initialized t or I to a different size.
@Walter Roberson — Thank you!
@Gregory Mendes — The code you are having problems with is obviously not the code you posted.
.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Asked:

on 24 May 2021

Commented:

on 24 May 2021

Community Treasure Hunt

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

Start Hunting!