How to simulate a non linear system
古いコメントを表示
Hello guys,i have a question…I m simulating an LQR of a double inverted pendulum on a cart using matlab.I did the linearization,i tried different values for Q and R and i did the plot of states.Now the problem is that i want to compare the Linear Lqr Model in the form:
Dx/dt = (Ax+Bu) with u=-Kx i want to compare this with the LQR applied to the original non linear system.I know that LQR is for linear system but it was Asked to simulate the LQR u=-Kx in the non linear model The non linear model is in the form: Dx/dt= A(x)*x +B(x)*u + H(x) so using U=-Kx my sistem became dx/dt = (A(x)-B(x)K)x + H(x) .Now how can i simulate this system?for the linear one i used the space state form,how can i Now use the non linear Matrix to simulate and represent my sistem?Thanks
8 件のコメント
Sam Chak
2024 年 3 月 6 日
To simulate the nonlinear state-space of the pendulum system, you can generally utilize the "ode45()" function in MATLAB or the Second-Order Integrator blocks in Simulink. I recommend referring to the documentation and examples for further guidance. In cases where the nonlinear systems have known analytical solutions, you can also employ the "dsolve()" function from the Symbolic Math Toolbox. It's worth noting that the analytical solution for a single undamped pendulum system is expressed using Jacobi elliptic functions, which may not be extensively covered in engineering schools.
If you require a quick result, you can click on the Indentation icon
to write or copy/paste the code that describes the nonlinear dynamics of the double pendulum system. By doing so, you will be guided on what steps to take next.
In your linear simulation, change the function that defined the ODE as
u = -K*x;
dxdt = (Ax+Bu);
to
Afun = @(x) ... % what ever A(x) equation you have
Bfun = @(x) ... % what ever B(x) equation you have
Hfun = @(x) ... % what ever H(x) equation you have
u = -K*x;
dxdt = (Afun(x)*x+Bfun(x)*u)+Hfun(x);
If you get stuck, then provide the A(x) B(x) H(x) and show what you have tried, and more help can come your way.
Sam Chak
2024 年 3 月 6 日
@Gianluca Di pietro, It would be interesting to learn whether your designed linear control law,
, can provide sufficient torque to successfully swing up the double pendulum from its hanging position (rest) and stabilize it in the upright position.
Gianluca Di pietro
2024 年 3 月 6 日
編集済み: Walter Roberson
2024 年 3 月 6 日
Walter Roberson
2024 年 3 月 6 日
Anl, Bnl, Hnl are symfun . You are multiplying Anl and Bnl by something, rather than applying them to something.
Anl(x_)= [zeros(3) eye(3);zeros(3) -inv(D)*C];
Bnl(x_) = [zeros(3,1);inv(D)*H];
Hnl(x_) = [zeros(3,1);-inv(D)*G];
You are not applying D and C and G to arguments.
Note:
The result of applying a symfun is sym, but the result of myfunction needs to be numeric, so you are going to need to double()
Gianluca Di pietro
2024 年 3 月 6 日
%%Tesina Doppio Pendolo Invertito su un Carrellino
%Definiamo le matrici e i parametri del sistema
m0 = 1.5;
m1= 0.5;
m2=0.75;
L1=0.5;
L2=0.75;
g =9.8;
d1 = m0 + m1 + m2;
d2 = (0.5*m1 + m2)*L1;
d3 = 0.5*m2*L2;
d4 = (1/3 * m1 + m2)*L1^2;
d5= 0.5*m2*L1*L2;
d6= 1/3*m2*L2^2;
f1 = (0.5*m1 + m2)*L1*g;
f2 = 0.5*m2*L2*g;
syms Theta0 Theta1 Theta2;
syms DTheta0 DTheta1 DTheta2;
x_ = transpose([Theta0 Theta1 Theta2 DTheta0 DTheta1 DTheta2]);
D(x_)= [d1 d2*cos(x_(2)) d3*cos(x_(3));
d2*cos(x_(2)) d4 d5*cos(x_(2)-x_(3));
d3*cos(x_(3)) d5*cos(x_(2)-x_(3)),d6];
G(x_)= transpose([0 -f1*sin(x_(2)) -f2*sin(x_(3))]);
C(x_)=[0 -d2*sin(x_(2))*x_(5) -d3*sin(x_(3))*x_(6);
0 0 d5*sin(x_(2)-x_(3))*x_(6);0 -d5*sin(x_(2)-x_(3))*x_(5) 0];
H = transpose([1 0 0]);
%matrici A e B sistema linearizzato
deriveG(x_) = transpose(jacobian(G(x_(1),x_(2),x_(3),x_(4),x_(5),x_(6)),[x_(1),x_(2),x_(3)]));
A = [zeros(3) eye(3);-inv(D(0,0,0,0,0,0))*deriveG(0,0,0,0,0,0) zeros(3)];
Anumeric = double(A);
B = [zeros(3,1);inv(D(0,0,0,0,0,0))*H];
Bnumeric = double(B);
%%matrici sistema non lineare
Anl(x_)= [zeros(3) eye(3);zeros(3) -inv(D(x_(1),x_(2),x_(3),x_(4),x_(5),x_(6)))*C];
Bnl(x_) = [zeros(3,1);inv(D(x_(1),x_(2),x_(3),x_(4),x_(5),x_(6)))*H];
Hnl(x_) = [zeros(3,1);-inv(D(x_(1),x_(2),x_(3),x_(4),x_(5),x_(6)))*G];
%Progettazione controllore LQR
%%Bryson rule: Bryson's method [17] was introduced to adjust Q and R weighting
% matricesas an attempt to overcome the drawbacks of using the trial and error method.
% According to this rule, Q and R can be calculated by finding the reciprocals
% of squares of the maximum acceptable values of the state and the input control variables.
Q = diag([50 50 50 700 700 700]);
R = 1;
sys = ss(Anumeric,Bnumeric,eye(6),0);
Co = ctrb(sys);
res = rank(Co); %rank is equal to number of state 6,system is controllable
Ob=obsv(sys);
res2= rank(Ob); %%rank is equal to number of state 6,system is observable
[K,S,P]=lqr(sys,Q,R); %p are closed-loop poles system
newsys = ss((Anumeric-Bnumeric*K),Bnumeric,eye(6),0);
%Pole Representation
reale = real(P);
immaginario = imag(P);
% Plot dei poli nel piano complesso (cartesiano)
figure;
plot(reale, immaginario, 'rx', 'MarkerSize', 10); % 'rx' indica crocette rosse
xlabel('Parte Reale');
ylabel('Parte Immaginaria');
title('Diagramma dei Poli (Cartesiano)');
grid on;
axis equal;
%plot states
% Simulate the system for initial conditions
[y,t,~]=initial(newsys,[0;deg2rad(10);-deg2rad(10);0;0;0],20);
%[y,t,x]=initial(sysnew,[0;deg2rad(20);deg2rad(20);0;0;0],10);
% Convert from rad to deg
y(:,2:3)=rad2deg(y(:,2:3));
y(:,5:6)=rad2deg(y(:,5:6));
figure
for i=1:1:6
subplot(2,3,i)
plot(t,y(:,i))
grid
end
%%ode45 for non linear model
x0 = [0,0,0,0,0,0]; %initial condition
t_a = linspace(0,1,20); %time vector
[t,x] = ode45(@(t,x)myfunction(t,x,K,Anl,Bnl,Hnl),t_a,x0);
figure
plot(t_a,x.','LineWidth',1.5);
function dxdt = myfunction(t,x,K,Anl,Bnl,Hnl)
u = -K*x;
dxdt_ = Anl(x(1),x(2),x(3),x(4),x(5),x(6))*x + Bnl(x(1),x(2),x(3),x(4),x(5),x(6))*u + Hnl(x(1),x(2),x(3),x(4),x(5),x(6));
dxdt = double(dxdt_(:));
end
Gianluca Di pietro
2024 年 3 月 7 日
採用された回答
その他の回答 (1 件)
You have the option to explore and experiment with it on your own. In this case, I used a straightforward 2nd-order system as an example and calculated the LQR gains for three scenarios: Baseline, High QR, and Low QR.
Case 1: Baseline
%% Case 1: Baseline
P = 1;
Q = P*eye(2) % state-weighted factor
R = P; % input-weighted factor
A = [0, 1; -3, -2];
B = [0; 5];
C = [1, 0];
D = 0;
sys = ss(A, B, C, D); % state-space system
K = lqr(A, B, Q, R)
pxy = ss(A-B*K, B, C, D); % proxy model
N = 1/dcgain(pxy);
cls = ss(A-B*K, B*N, C, D); % closed-loop system
S = stepinfo(cls)
step(sys), hold on
step(cls), grid on, hold off
legend('open-loop sys', 'closed-loop sys')
title('Case 1: Baseline with norm(Q) = norm(R) = 1')
Case 2: When both Q and R are high
%% Case 2: When both Q and R are high
P = 10000;
Q = P*eye(2) % state-weighted factor
R = P; % input-weighted factor
A = [0, 1; -3, -2];
B = [0; 5];
C = [1, 0];
D = 0;
sys = ss(A, B, C, D); % state-space system
K = lqr(A, B, Q, R)
pxy = ss(A-B*K, B, C, D); % proxy model
N = 1/dcgain(pxy);
cls = ss(A-B*K, B*N, C, D); % closed-loop system
S = stepinfo(cls)
step(sys), hold on
step(cls), grid on, hold off
legend('open-loop sys', 'closed-loop sys')
title('Case 2: When both norm(Q) and norm(R) are 10000')
Case 3: When both Q and R are low
%% Case 3: When both Q and R are low
P = 0.0001;
Q = P*eye(2) % state-weighted factor
R = P; % input-weighted factor
A = [0, 1; -3, -2];
B = [0; 5];
C = [1, 0];
D = 0;
sys = ss(A, B, C, D); % state-space system
K = lqr(A, B, Q, R)
pxy = ss(A-B*K, B, C, D); % proxy model
N = 1/dcgain(pxy);
cls = ss(A-B*K, B*N, C, D); % closed-loop system
S = stepinfo(cls)
step(sys), hold on
step(cls), grid on, hold off
legend('open-loop sys', 'closed-loop sys')
title('Case 3: When both norm(Q) and norm(R) are 0.0001')
カテゴリ
ヘルプ センター および File Exchange で PID Controller Tuning についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!










