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

on 6 Nov 2019
Latest activity Commented on by 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 (view profile)

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

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 (view profile)

on 7 Nov 2019

Damian, is this the task You set? 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 (view profile)

on 8 Nov 2019

Sincerely Yours, Pavel Osipov.

#### 1 Comment

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 (view profile)

on 9 Nov 2019  ### Pavel Osipov (view profile)

on 9 Nov 2019

Damian, hello! See attachment.Bye!