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!