Error ODE45 "Array indices must be positive integers or logical values."

I wrote the following code in Matlab and keep receiving an error i can't explain:
I0 = 10^(-2);
del = 1/4;
gam = 1/7;
tLim = [0, 100];
% SEIR-Modell
% y(1) = S
% y(2) = E
% y(3) = I
% y(4) = R
SEIRModell = @(t,y) [-R0(t)*gam*y(1)*y(3); gam*R0(t)*y(1)*y(3) - del*y(2); del*y(2) - gam*y(3); gam*y(3)];
[t, y] = ode45(SEIRModell,tLim, [1-I0; 0; I0; 0]);
% SIR-Funktionen in %
S = 100*y(:,1);
E = 100*y(:,2);
I = 100*y(:,3);
R = 100*y(:,4);
figure(4)
plot(t,S, t,E, t,I, t,R);
ylabel('Anteil in %');
xlabel('Zeit in Tagen');
legend('S', 'E', 'I', 'R', 'Location', 'West');
title(sprintf('SIR-Modell mit I0=%.4f', I0));
grid on;
I took a working code and added the R0(t) function to change the value over time.
function [out] = R0(t)
if (0<t) && (t<10)
out = 2.5;
elseif (10<t) && (t<20)
out = -0.17*t+4.2;
elseif (20<t) && (t<50)
out = 0.8;
elseif (50<t) && (t<60)
out = 0.12*t-5.2;
else
out=2;
end
end
Just some lines to specify the value over time. And i keep receiving the following Error:
Array indices must be positive integers or logical values.
Error in
SEIR>@(t,y)[-R0(t)*gam*y(1)*y(3);gam*R0(t)*y(1)*y(3)-del*y(2);del*y(2)-gam*y(3);gam*y(3)]
(line 133)
SEIRModell = @(t,y) [-R0(t)*gam*y(1)*y(3); gam*R0(t)*y(1)*y(3) -
del*y(2); del*y(2) - gam*y(3); gam*y(3)];
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options,
varargin);
Error in SEIR (line 134)
[t, y] = ode45(SEIRModell,tLim, [1-I0; 0; I0; 0]);

 采纳的回答

I ran your code in a function I use for such purposes, and it ran for me without error, producing an appropriate plot.
function RunSEIR
I0 = 10^(-2);
del = 1/4;
gam = 1/7;
tLim = [0, 100];
% SEIR-Modell
% y(1) = S
% y(2) = E
% y(3) = I
% y(4) = R
SEIRModell = @(t,y) [-R0(t)*gam*y(1)*y(3); gam*R0(t)*y(1)*y(3) - del*y(2); del*y(2) - gam*y(3); gam*y(3)];
[t, y] = ode45(SEIRModell,tLim, [1-I0; 0; I0; 0]);
% SIR-Funktionen in %
S = 100*y(:,1);
E = 100*y(:,2);
I = 100*y(:,3);
R = 100*y(:,4);
figure(4)
plot(t,S, t,E, t,I, t,R);
ylabel('Anteil in %');
xlabel('Zeit in Tagen');
legend('S', 'E', 'I', 'R', 'Location', 'West');
title(sprintf('SIR-Modell mit I0=%.4f', I0));
grid on;
function [out] = R0(t)
if (0<t) && (t<10)
out = 2.5;
elseif (10<t) && (t<20)
out = -0.17*t+4.2;
elseif (20<t) && (t<50)
out = 0.8;
elseif (50<t) && (t<60)
out = 0.12*t-5.2;
else
out=2;
end
end
end
Then run it from your Command Window as:
RunSEIR
and see if it works correctly.

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by