4 views (last 30 days)
Show older comments
Guillermo Pourreau Pons on 19 May 2023
Commented: Rik on 19 May 2023
Open in MATLAB Online
"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 secondsDiscrete-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 Comments Show NoneHide None
Show NoneHide None
Dyuman Joshi on 19 May 2023
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752439
Open in MATLAB Online
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 on 19 May 2023
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752479
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?
Sign in to comment.
Sign in to answer this question.
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 Show NoneHide None
Show NoneHide None
Guillermo Pourreau Pons on 19 May 2023
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752474
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 on 19 May 2023
Direct link to this comment
https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752729
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.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Mathematics and OptimizationSymbolic Math ToolboxMathematicsNumber Theory
Find more on Number Theory 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office