ERROR: Array indices must be positive integers or logical values.

5 visualizaciones (últimos 30 días)
Guillermo Pourreau Pons
Guillermo Pourreau Pons el 19 de Mayo de 2023
Comentada: Rik el 19 de Mayo de 2023
"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)
sys = A = x1 x2 x3 x1 0 1 0 x2 0 0 1 x3 -22.5 -0.64 -0.0024 B = u1 x1 0 x2 0 x3 1.5 C = x1 x2 x3 y1 1 0 0 D = u1 y1 0 Continuous-time state-space model.
sys1=c2d(sys,T,'zoh')
sys1 = A = x1 x2 x3 x1 1 0.02 0.0002 x2 -0.0045 0.9998 0.02 x3 -0.45 -0.0173 0.9998 B = u1 x1 2e-06 x2 0.0003 x3 0.03 C = x1 x2 x3 y1 1 0 0 D = u1 y1 0 Sample time: 0.02 seconds Discrete-time state-space model.
[V,D]=eig(sys1.A)
V =
-0.0618 - 0.0965i -0.0618 + 0.0965i -0.1235 + 0.0000i 0.1573 - 0.2876i 0.1573 + 0.2876i 0.3393 + 0.0000i 0.9378 + 0.0000i 0.9378 + 0.0000i -0.9325 + 0.0000i
D =
1.0265 + 0.0516i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.0265 - 0.0516i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.9465 + 0.0000i
V1=inv(V)
V1 =
-1.4158 + 2.4544i 0.4749 + 0.9156i 0.3602 + 0.0082i -1.4158 - 2.4544i 0.4749 - 0.9156i 0.3602 - 0.0082i -2.8476 + 0.0000i 0.9551 + 0.0000i -0.3478 + 0.0000i
Dk=D^k;
Af=V*Dk*V1
Af = 
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
Unrecognized function or variable 'K_sym'.
  2 comentarios
Dyuman Joshi
Dyuman Joshi el 19 de Mayo de 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
Unrecognized function or variable 'K_sym'.
Guillermo Pourreau Pons
Guillermo Pourreau Pons el 19 de Mayo de 2023
I'm sorry, the correct code is this one:
for k=1:25
for j=1:k-1
X(k)=Af(k)*X(0)+j*Af(K-1)*B
y(k)=C*X(k)
end
end
I continue getting the same error, and I dont know how to avoid X(0). Have you any idea?

Iniciar sesión para comentar.

Respuestas (1)

Rik
Rik el 19 de Mayo de 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 comentarios
Guillermo Pourreau Pons
Guillermo Pourreau Pons el 19 de Mayo de 2023
True, I'm sorry
The correct code is this one:
for k=1:25
for j=1:k-1
X(k)=Af(k)*X(0)+j*Af(K-1)*B
y(k)=C*X(k)
end
end
I continue getting the same error, and I dont know how to avoid X(0). Have you any idea?
Rik
Rik el 19 de Mayo de 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.

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by