# This Matlab code is saying Index in position 1 is invalid. Array indices must be positive integers or logical values.

1 visualización (últimos 30 días)
CONNOR el 30 de Nov. de 2023
Respondida: Image Analyst el 30 de Nov. de 2023
when I try to run this code it says "Index in position 1 is invalid. Array indices must be positive integers or logical values.", but both i and n are integers so I am assuming that it has a problem with how I use the cell arrays.
close all, clear, clc
Bmax = 1;
uL_min = 6;
uI = 10;
e = 0.001;
Ap = 5000;
Pi = 930.27249;
Si = Pi/Ap;
Li = 0.01*Si;
Ii = 0;
Ri = uI*Ii;
Bi = 1;
Pb = 1;
for i = 1:length(T)
if T(i)>0 && T(i)<35
Tb(i) = (0.000241*(T(i)^2.06737))*((35-T(i))^0.72859);
end
end
j = 1;
for i=1:length(t)
uL(i) = sum(Tb(j:i));
while uL(i) > uL_min
j = j+1;
uL(i) = sum(Tb(j:i));
end
end
B = Bmax*Tb;
p = [B, uL, uI, e, Te, Pb];
y0 = [Pi, Si, Li, Ii, Ri, Bi];
odeFunc = SLIRmodel(tspan, y0, p);
[t,y] = rk4(odeFunc, tspan, y0);
%% Functions
function [dydt] = SLIRmodel(t,y0,p)
%assign parameters
beta = p(1);
uL = p(2);
uI = p(3);
e = p(4);
Te = p(5);
Pb = p(6);
%assign variables
P = y0(1);
S = y0(2);
L = y0(3);
I = y0(4);
R = y0(5);
dPbdt = (0.1724*Pb - 0.0000212*Pb^2)*Te;
dPldt = (1.33*t)*Te;
dPdt = dPbdt + dPldt;
dSdt = (-beta*S*I)+dPdt;
dLdt = (beta*S*I)-((uL^-1)*L)+e;
dIdt = ((uL^-1)*L)-((uI^-1)*I);
dRdt = (uI^-1)*I;
dydt = {dPbdt, dPldt, dPdt, dSdt, dLdt, dIdt, dRdt};
end
function [t, y] = rk4(odeFunc, tspan, y0)
N = length(tspan);
t = tspan;
y = y0;
for n=2:N
for i=1:length(odeFunc)
k1 = odeFunc{i}(t(n), y(n));
k2 = odeFunc{i}(t(n) + 0.5*h, y(n) + 0.5*h*k1);
k3 = odeFunc{i}(t(n) + 0.5*h, y(n) + 0.5*h*k2);
k4 = odeFunc{i}(t(n) + h, y(n) + h*k3);
for j = 1:q
y(j, n+1) = y(j, n) + h*(k1(j) + 2*k2(j) + 2*k3(j) + k4(j))/6;
end
t(n+1) = t(n) + h;
end
end
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (2)

Angelo Yeo el 30 de Nov. de 2023
Editada: Angelo Yeo el 30 de Nov. de 2023
Let's look at the code in line 63.
k1 = odeFunc{i}(t(n), y(n));
where odeFunc, t, y are defined like below.
odeFunc =
1×7 cell array
{[0.0121]} {1×1465 double} {1×1465 double} {1×1465 double} {[0.3445]} {[0.0045]} {[0]}
t(1:10) (size: 1x1465)
ans =
0 0.0417 0.0833 0.1157 0.1667 0.2083 0.2500 0.2917 0.3333 0.3750
y =
930.2725 0.1861 0.0019 0 0 1.0000
In your first iteration where n = 2 and i = 1, this may be something MATLAB is commanded to do.
[0.0121](0, 930.2725)
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Image Analyst el 30 de Nov. de 2023
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by