ERROR: Array indices must be positive integers or logical values.
조회 수: 6 (최근 30일)
이전 댓글 표시
"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
Dyuman Joshi
2023년 5월 19일
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
답변 (1개)
Rik
2023년 5월 19일
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
Rik
2023년 5월 19일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!