Coding problem for optimal control

4 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2016-5-31
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?
Tarekegn Dinku
Tarekegn Dinku 2024-1-20
I want to your intial guess to control u

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Nonlinear Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by