- 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 ?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
irfan naufan
il 25 Giu 2019
Commentato: irfan naufan
il 25 Giu 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 Commenti
Risposta accettata
Pullak Barik
il 25 Giu 2019
Modificato: Pullak Barik
il 25 Giu 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Discrete Math in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!