Coding problem for optimal control

Dear Matlab experts,
I have a coding for optimal control problem, and this is not working. I would like to know how to solve this problem. Thank you very much in advance.
Sincerely yours,
J1
function Bloc1
% State equations
syms x1 x2 p1 p2 u;
Dx1 = x2^2;
Dx2 = -x2 + u;
% Cost function inside the integral
syms g;
g = 0.5*u^0.5*x2^0.5;
% Hamiltonian
syms p1 p2 H;
H = g + p1*Dx1 + p2*Dx2;
% Costate equations
Dp1 = -diff(H,x1);
Dp2 = -diff(H,x2);
% solve for control u
du = diff(H,u);
sol_u = solve(du, 'u');
% Substitute u to state equations
Dx2 = subs(Dx2, u, sol_u);
% convert symbolic objects to strings for using 'dsolve'
eq1 = strcat('Dx1=',char(Dx1));
eq2 = strcat('Dx2=',char(Dx2));
eq3 = strcat('Dp1=',char(Dp1));
eq4 = strcat('Dp2=',char(Dp2));
sol_h = dsolve(eq1,eq2,eq3,eq4);
%%use boundary conditions to determine the coefficients
% case a: (a) x1(0)=x2(0)=0; x1(2) = 5; x2(2) = 2;
conA1 = 'x1(0) = 0';
conA2 = 'x2(0) = 0';
conA3 = 'x1(2) = 5';
conA4 = 'x2(2) = 2';
sol_a = dsolve(eq1,eq2,eq3,eq4,conA1,conA2,conA3,conA4);
% plot both solutions
figure(1);
ezplot(sol_a.x1,[0 3]); hold on;
ezplot(sol_a.x2,[0 3]);
ezplot(-sol_a.p2,[0 3]); % plot the control: u=-p2
axis([0 3 -1.6 7]);
text(0.6,0.5,'x_1(t)');
text(0.4,2.5,'x_2(t)');
text(1.6,0.5,'u(t)');
xlabel('time');
ylabel('states');
title('Solutions comparison (case a)');

2 Comments

Please give more details about the problems you are observing. Are you getting an error message? If not then what is the first output that does not match what you expect?
I want to your intial guess to control u

Sign in to comment.

Answers (0)

Categories

Asked:

on 31 May 2016

Commented:

on 20 Jan 2024

Community Treasure Hunt

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

Start Hunting!