How to input result looping/iteret (for) to a existing matrix ?

1 回表示 (過去 30 日間)
irfan naufan
irfan naufan 2019 年 6 月 25 日
コメント済み: irfan naufan 2019 年 6 月 25 日
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;

採用された回答

Pullak Barik
Pullak Barik 2019 年 6 月 25 日
編集済み: Pullak Barik 2019 年 6 月 25 日
I can suggest the following ways-
  1. Computing Q0, Q1, ..., Q5 values in a for-loop using a single array where the values of X(i) are replaced, say Q.
  2. Use a for-loop to call lqr on each element of Q and store the result in each element of an array K.
  3. Directly negate the array K at the end instead of negating each element and storing in a new variable.
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 件のコメント
irfan naufan
irfan naufan 2019 年 6 月 25 日
still errorCapture.PNG
%% Body
mb = 5306.5443*(10^-3); %%kg
db = 152.54*(10^-3); %%m
ibx = 24868087.958591*(10^-9); %%kg m^2
iby = 260278008.180508*(10^-9); %%kg m^2
ibz = 261399900.711023*(10^-9); %%kg m^2
%% Gimbal
mg1 = 1106.66*(10^-3); %%kg
mg2 = 1106.66*(10^-3); %%kg
dg1 = 165.765*(10^-3); %%m masih blom pas
dg2 = 165.765*(10^-3); %%m masih blom pas
ig1x = 1983819.2943816*(10^-9); %%kg m^2
ig2x = 1983819.2943816*(10^-9); %%kg m^2
ig1y = 1723249.0635427*(10^-9); %%kg m^2
ig2y = 1723249.0635427*(10^-9); %%kg m^2
ig1z = 3321937.8766810*(10^-9); %%kg m^2
ig2z = 3321937.8766810*(10^-9); %%kg m^2
%% Flywheel
mf1 = 710*(10^-3); %%kg
mf2 = 710*(10^-3); %%kg
df1 = 171.56*(10^-3); %%m
df2 = 171.56*(10^-3); %%m
if1x = 877398.360504*(10^-9); %%kg m^2
if2x = 877398.360504*(10^-9); %%kg m^2
if1y = 877398.360504*(10^-9); %%kg m^2
if2y = 877398.360504*(10^-9); %%kg m^2
if1z = 1642705.048734*(10^-9); %%kg m^2
if2z = 1642705.048734*(10^-9); %%kg m^2
%% Transmisi
j1 = 253.67*(10^-9); %%kg m^2 78.44 atau 53.84
j2 = 5396.7883322*(10^-9); %%kg m^2 3409.22 atau 1782.97
j3 = 5396.7883322*(10^-9); %%kg m^2 3409.22 atau 1782.97
j4 = 2660.87924043*(10^-9); %%kg m^2 2543.3 atau 1734.11
j5 = 2660.87924043*(10^-9); %%kg m^2 2543.3 atau 1734.11
j6 = 1108.69968351*(10^-9); %%kg m^2 2543.3 atau 1734.11
j7 = 1108.69968351*(10^-9); %%kg m^2 2543.3 atau 1734.11
n1 = 2.36;
%% Parameter
wf1 = 628.3185307179587; %% 5000 rpm 523.598775 rad/s
wf2 = -628.3185307179587; %% -5000 rpm 628.3185307179587 785.3981633974483
g = 9.81; %%m/s^2
%% Persamaan State Space
Mx = ((j2 + j3 + j4 + j5)/ n1) + n1*j1;
Mn = (n1 - (1.218*j6 / (if1y + ig1y)) + (1.218*j7 / (if2y + ig2y))) / Mx;
Mq = -((if1z*j6*wf1 /(if1y + ig1y)) + (if2z*j7*wf2 / (if2y + ig2y))) / Mx;
Mp = mb*(db^2) + ibx + if1x + if2x + ig1x + ig2x + mg1*(dg1^2) + mg2*(dg2^2) + mf1*(df1^2) + mf2*(df2^2);
Mv = mb*g*db + mg1*g*dg1 + mg2*g*dg2 + mf1*g*df1 + mf2*g*df2;
a21 = (Mv / Mp); % from rad/s to rpm
a24 = -(0.8125*wf1*if1z / Mp)+(0.8125*wf2*if2z / Mp);
a42 = Mq;
b41 = Mn;
%% State Space Mechanics Model
A = [0 1 0 0;
a21 0 0 a24;
0 0 0 1;
0 a42 0 0];
Bu = [0; 0; 0; b41];
Bd = [1; 0; 0; 0]; %% Tanyakan Pak Unggul dan yang di simulink
C = eye(4,4);
Du = zeros(4,1)
R = 510.5;
N=[0; 0; 0; 0];
nn = 6; %use this value to set the appropriate length of vectors/iterations for the whole code
%Preallocating K,S,e,x to aid in performance while running the code
K = zeros(1, nn);
S = zeros(1, nn);
e = zeros(1, nn);
x = zeros(1, nn);
for n = 1:nn
x(n) = 1650+(1100*(n-1));
end
for i = 1:nn
Q = [760 0 0 0; 0 70 0 0; 0 0 x(i) 0; 0 0 0 0.001];
[K(i),S(i),e(i)] = lqr(A,Bu,Q,R,N);
end
Klqr = -1.*K;
irfan naufan
irfan naufan 2019 年 6 月 25 日
how to loop in simulink ? i want to loop for any Klqr as gain and result is phi,alpha, of each gain

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by