MATLAB Answers

Vectors -Must- be the same length

1 view (last 30 days)
Gregory Mendes
Gregory Mendes on 24 May 2021
Commented: Star Strider on 24 May 2021
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 (2)

Star Strider
Star Strider on 24 May 2021
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
Star Strider
Star Strider on 24 May 2021
@Walter Roberson — Thank you!
@Gregory Mendes — The code you are having problems with is obviously not the code you posted.
.

Sign in to comment.


VBBV
VBBV on 24 May 2021
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+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(1:itmax))
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(1:itmax))
axis([0 tmax, 0 popscale])
xlabel('time in days')
ylabel('Number Lost')
title('Epidemic simulation of NYC from near start')
you can try this way

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by