% IN THE NAME OF GOD
% HAMED GHAFFARI
% page 198 - 202
% Book : Kirk
% exp 5.1-1
clcclear % state equation
syms x1 x2 u p1 p2 ;DX1 = x2 ;DX2 = -x2 + u ;% cost function inside the integral
syms j j = 1/2 * u^2 ;% Hamiltonian
syms p1 p2 HH = j + p1*DX1 +p2*DX2 ;% costate equations
DP1 = -diff(H,x1) DP2 = -diff(H,x2)% solve for u control
du = diff(H,u)sol_u = solve(du,u)% substitute u to state eq DX2
DX2 = subs(DX2 , u , sol_u)% Cocatenate String horizontal (الحاق کردن عبارات به صورت رشته ای)
% convert symbolic adjective to string for using 'dsolve'
eq1 = strcat('DX1 = ' , char(DX1))eq2 = strcat('DX2 = ' , char(DX2))eq3 = strcat('DP1 = ' , char(DP1))eq4 = strcat('DP2 = ' , char(DP2))% solve Hamiltonian
sol_h = dsolve(eq1,eq2,eq3,eq4)sol_Dx1 = sol_h.X2sol_Dx2 = sol_h.X1sol_DP1 = sol_h.P1sol_DP2 = sol_h.P2% case Boundry condition x1(0) = x2(0) = 0 ; x1(2) = 5 , x2(2) = 2
cond1 = 'X1(0)= 0' ;cond2 = 'X2(0)= 0' ;cond3 = 'X1(2)= 5' ;cond4 = 'X2(2)= 2' ;sol_Hnew = dsolve(eq1,eq2,eq3,eq4,cond1,cond2,cond3,cond4)sol_eq1 = sol_Hnew.X1sol_eq2 = sol_Hnew.X2sol_eq3 = sol_Hnew.P1sol_eq4 = sol_Hnew.P2
MATLAB: HOW I plotted the x1 and x2 and u in optimal control poblem
optimal control - structure - plot
Best Answer