# Help! I'm having trouble plotting hamiltonian and LQR.

36 views (last 30 days)
Damian Prashad on 6 Nov 2019
Commented: Damian Prashad on 10 Nov 2019
Hi,
I'm attempting to land a rocket using state space equations. I'm using optimal controls LQR and Hamiltonian. I have an example of a smaller matrix attached but I can't seem to get Matlab code enough to expand it to the dimensions of my Matrix.
clc, clear all, close all
syms u(t) x(t) x1 x2 x3 x4 x5 x6 u1 u2 u3 lambda1 lambda2 lambda3 lambda4 lambda5 lambda6 lambda Q R
Q = eye(6);
R = 5;
x0 = [241500 0].';
t0 = 0;
tf = 5;
l1=35; % meters
l2=35;% meters
ln=1; % 1 meter nozzle length
m=30000; %kg, mass of the rocket
J=m*(l1+l2)^2/12; %moment of inertia
g=9.5; %m/s
theta = pi;
phi = pi/12;
Fe=64860; %Newtons
Fs=130;%Newtons
N=0;
F=eye();
u=[Fe theta phi].';
x=[x1 x2 x3 x4 x5 x6].';
A = [0 0 0 1 0 0; 0 0 0 0 1 0; 0 0 0 0 0 1; 0 0 g 0 0 0; 0 0 0 0 0 0; 0 0 0 0 0 0]
B = [0 0 0; 0 0 0; 0 0 0; theta/m 1 m*g; theta/m -m -g*theta; 1-phi*(l1+ln)/J l2/J -Fe*(l1+ln)/J]
% A_transpose = A.';
% B_transpose = B.';
% %ode1 = diff(x_dot) == A_transpose*x + B_transpose*u;
H=0.5*x.'*Q*x+.5*u.'*R*u+lambda1.'*A*x+ lambda2.'*B*u; %Hamiltonian Function
[K,S,P] = lqr(A,B,Q,R,N)

Pavel Osipov on 7 Nov 2019
Damian Prashad, good afternoon. Looked up Your code. Everything works well. What matrices Would you like to change to larger sizes? Where do difficulties arise? You are probably hampered by the loss of precision in matrix transformations whose elements differ by 10^11 times (B). This may well lead to a loss of computational stability.
All the best, Paul.

#### 1 Comment

Damian Prashad on 7 Nov 2019
Pavel Osipov, thanks for the response. I'm with my A,B,Q, and R matrices. I'm not sure how to get the code to run when I have a 6X6 A, 3X3 B, 6X6 Q, and 1X1 R.
Hope to hear from you soon.
Thanks,
Damian

Pavel Osipov on 7 Nov 2019
Damian, is this the task You set?

Damian Prashad on 7 Nov 2019
Pavel,
Thanks for the question. Yes that's the form that I have however, my understanding is that I don't need the y=C*X+D*U because I'm looking for the LQR which normally doens't have that form.
Please see the code. I'm able to run the code now with the expanded matrices, however the data isn't coming out correctly.
Thank you,
-Damian

Pavel Osipov on 8 Nov 2019
Damian, Hello! Putting matrices C =diag(1,...,1) and D=zeros(.,.) get Your system of equations. Simulink State-Space block allows you to solve linear control problems without much hassle. I can send a training file where the problem of oscillations of a double spring pendulum is solved by Simulink. Matrix A: Size [4x4]. I hope You solve Your problem by analogy. Simulink also has nonlinear blocks x'=f (x, u, t) to solve your control. While there is little free time to learn more about your script. Don't take offense at me. Advice is much easier to give than to program.
Sincerely Yours, Pavel Osipov.

#### 1 Comment

Damian Prashad on 8 Nov 2019
Pavel,
Yes, please send me the link, I haven't completly solved my problem yet. I got the matlab code to run, but my data is not good.
Please post the information when you havea moment.
Thank you,
-Damian

Pavel Osipov on 9 Nov 2019

Pavel Osipov on 9 Nov 2019
Damian, hello! See attachment.Bye!

#### 1 Comment

Damian Prashad on 10 Nov 2019
Pavel, Thank you! I translated the PDF and I'm reviewing it. Have a nice day!