ERROR: Array indices must be positive integers or logical values.
5 views (last 30 days)
Show older comments
"Why am I getting the error 'Array indices must be positive integers or logical values' in MATLAB when using a for loop?"
% grupo 20
Km=1.5;
Ra=8;
La=0.03;
J=0.08;
Kg=10;
Kt=0.20;
Kv=40;
T=0.02;
syms k;
%F2 y F3
A=[0 1 0; 0 0 1; -Kg*Km^2 -Ra*J -La*J];
B=[0; 0; Km];
C=[1 0 0];
sys=ss(A,B,C,0)
sys1=c2d(sys,T,'zoh')
[V,D]=eig(sys1.A)
V1=inv(V)
Dk=D^k;
Af=V*Dk*V1
X = zeros(3, 1);
y = zeros(25, 1);
for k=1:25
for j=1:K_sym
X(k)=Af(k)*X(0)+j*Af(K_sym)*B
y(k)=C*X(k)
end
end
2 Comments
Dyuman Joshi
on 19 May 2023
I am not able to reproduce the same error you obtained.
However, there is another error, as you can see below, which occurs because you are trying to use a variable you have not defined.
% grupo 20
Km=1.5;
Ra=8;
La=0.03;
J=0.08;
Kg=10;
Kt=0.20;
Kv=40;
T=0.02;
syms k;
%F2 y F3
A=[0 1 0; 0 0 1; -Kg*Km^2 -Ra*J -La*J];
B=[0; 0; Km];
C=[1 0 0];
sys=ss(A,B,C,0);
sys1=c2d(sys,T,'zoh');
[V,D]=eig(sys1.A);
V1=inv(V);
Dk=D^k;
Af=V*Dk*V1;
X = zeros(3, 1);
y = zeros(25, 1);
for k=1:25
for j=1:K_sym
X(k)=Af(k)*X(0)+j*Af(K_sym)*B;
y(k)=C*X(k)
end
end
Answers (1)
Rik
on 19 May 2023
Actually, the result of your code is a missing variable. Once you fix that, you will have to edit your code to avoid X(0).
2 Comments
Rik
on 19 May 2023
You can find an explanation for the layout tools here. It is the toolbar you see when you edit your question.
Since you didn't write any comments and only use single/two letter variable names, I have no clue what you want to do. So I have no idea what you should change. Currently you are trying to retrieve the zeroth element of X. Since that is before the first element, it doesn't exist.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!