- 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 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Pullak Barik
2019-6-25
编辑:Pullak Barik
2019-6-25
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Math 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!