- Computing Q0, Q1, ..., Q5 values in a for-loop using a single array where the values of X(i) are replaced, say Q.
- Use a for-loop to call lqr on each element of Q and store the result in each element of an array K.
- Directly negate the array K at the end instead of negating each element and storing in a new variable.
How to input result looping/iteret (for) to a existing matrix ?
3 views (last 30 days)
Show older comments
irfan naufan
on 25 Jun 2019
Commented: irfan naufan
on 25 Jun 2019
how to simple the code below with looping logic(for)? if I have Matrix A(4x4) Bu(4x1)
R=[510.5];
N=[0; 0; 0; 0];
for n = 1:6
x(n)=1650+(1100*(n-1))
end
for nn=1:6
X0=x(1,1)
X1=x(1,2)
X2=x(1,3)
X3=x(1,4)
X4=x(1,5)
X5=x(1,6)
%% Matrix Q
Q0=[760 0 0 0; 0 70 0 0; 0 0 X0 0; 0 0 0 0.001];
Q1=[760 0 0 0; 0 70 0 0; 0 0 X1 0; 0 0 0 0.001];
Q2=[760 0 0 0; 0 70 0 0; 0 0 X2 0; 0 0 0 0.001];
Q3=[760 0 0 0; 0 70 0 0; 0 0 X3 0; 0 0 0 0.001];
Q4=[760 0 0 0; 0 70 0 0; 0 0 X4 0; 0 0 0 0.001];
Q5=[760 0 0 0; 0 70 0 0; 0 0 X5 0; 0 0 0 0.001];
%% LQR calculate
[K0,S0,e0] = lqr(A,Bu,Q0,R,N);
[K1,S1,e1] = lqr(A,Bu,Q1,R,N);
[K2,S2,e2] = lqr(A,Bu,Q2,R,N);
[K3,S3,e3] = lqr(A,Bu,Q3,R,N);
[K4,S4,e4] = lqr(A,Bu,Q4,R,N);
[K5,S5,e5] = lqr(A,Bu,Q5,R,N);
%% LQR result gain
Klqr0=-K0;
Klqr1=-K1;
Klqr2=-K2;
Klqr3=-K3;
Klqr4=-K4;
Klqr5=-K5;
0 Comments
Accepted Answer
Pullak Barik
on 25 Jun 2019
Edited: Pullak Barik
on 25 Jun 2019
I can suggest the following ways-
By the way, your for-loop with 'nn' as the iterating variable seems incomplete, as there is no corresponding 'end' keyword for it. Moreover, you have not used the variable 'nn' anywhere below the declaration of the for-loop, so I suggest following the 3 suggestions above and properly encapsulating your code in for-loops.
3 Comments
More Answers (0)
See Also
Categories
Find more on Discrete Math 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!