This gives an error that says, "Index in position 2 is invalid. Array indices must be positive integers or logical values."
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I cant seem to figure out what is causeing the error saying the indices must be positive integers or logical values. All help is appreciated but I dont understand most feedback when it is super heavy in matlab lingo
close all, clear, clc
load('EnvironmentalForcing.mat')
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, p);
%% 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);
dPdt = ((1.33*t)*Te)+((0.1724*Pb - 0.0000212*Pb^2)*Te);
dSdt = (-beta*S*I)+(((1.33*t)*Te)+((0.1724*Pb - 0.0000212*Pb^2)*Te));
dLdt = (beta*S*I)-((uL^-1)*L)+e;
dIdt = ((uL^-1)*L)-((uI^-1)*I);
dRdt = (uI^-1)*I;
dydt = [dPdt, dSdt, dLdt, dIdt, dRdt];
end
function [t, y] = rk4(odeFunc, tspan, y0, p)
% N = length(tspan);
% q = length(y0);
% t0 = tspan(2);
% h = tspan(2) - tspan(1);
% t = zeros(N, 1);
% y = zeros(q, N);
% t(1) = t0;
% y(:, 1) = y0;
N = length(tspan);
t = tspan;
y = zeros(length(y0),N);
y(:,1) = y0(:);
for n=1:N-1
h = tspan(n+1)-tspan(n);
k1 = h*odeFunc(n, y(:,n), p);
k2 = h*odeFunc(n + 0.5, y(:,n) + 0.5*k1, p);
k3 = h*odeFunc(n + 0.5, y(:,n) + 0.5*k2, p);
k4 = h*odeFunc(n + h, y(:,n) + k3, p);
y(:, n+1) = y(:,n)+((k1+(2*k2)+(2*k3)+k4))/6;
t(n+1) = t(n) + h;
end
end
3 commentaires
Walter Roberson
le 1 Déc 2023
whos -file 'EnvironmentalForcing.mat'
t is not in the file. T is in the file.
Réponses (1)
sai charan sampara
le 5 Déc 2023
Hello Connor,
I understand that you are trying to find out why the error “Index in position 2 is invalid.Array indices must be positive integers or logical values.” is popping up.
Firstly, there are some errors that need to be fixed. In line number 21 the variable “t” is unrecognized. Since the variable “i” is used to index through array “Tb” the line should be changed as
for i=1:length(Tb)
Also, the variable “Te” is not defined anywhere in the script or in the mat file. Some value should be given to the variable “Te”. After resolving these two there is still the error that "Index in position 2 is invalid. Array indices must be positive integers or logical values.". This is because in line 71(shown below) a part of the function rk4 there is an error.
k1 = h*odeFunc(n, y(:,n), p);
As per the script “odeFunc” is a 2-Dimensional array and using parenthesis to index through it means that the variables “n”, “y(:,n)”, “p” are treated as indexes of the array “odeFunc”. “y(:,n)” returns an array of size 4x1 of non-integer values and this cannot be given as an index to array. This is the cause for the error saying index at position 2 is invalid, since “y(:,n)” is at position 2 and is not an integer nor a logical variable. This needs to be fixed. Check if “odeFunc” being a 2-Dimensional array is correct or if it should be a function. Also passing three arguments as index inputs for a 2-Dimensional array “odeFunc” will also give an error. Fixing these two will resolve the error.
You can refer to the below resources to learn more:
0 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!