MATLAB Answers

Symbolic math Tool box "diff" and "dsolve" for optimal control

8 views (last 30 days)
This is a simple optimal control problem where I have to differentialte the hamiltonian w.r.t "u" and substitute into the state equation . the confusion is with "diff" dunction which wants me to declare the symbolic variables as "syms x1 x2 p1 p2 u " etc where as "dsolve" wants me to declare as "syms x1(t) x2(t) p1(t) p2(t) u(t)" .
What I want to do is to find the partial derivative of the Hamiltonian w.r.t "u" and substitute back in the state equation and obtain the ode , then use "dsolve" and solve the state and costate . But I see dsolve wants me to declare as said above and hence I am unable to supply BC's as mentioned in the website https://www.mathworks.com/help/symbolic/solve-a-system-of-differential-equations.html. Any work around for this ? the initial conditions are x1(0) = x2(0) = 0; final x1(2)=5 and x2(2) =2 . I have shown here till the sustitution which works fine . But I want to use dsolve for the state and costate odes and supply the conditions and solve symbolically . i dont want to use the old "string" method whose is said will be removed in future
If I declare as "syms x1(t) x2(t) p1(t) p2(t) u(t)" "diff" function wont work with this error
Error using sym/diff (line 26)
Arguments, except for the first, must not be symbolic functions.
clear all ;close all
syms x1 x2 p1 p2 u
% Write the Hamiltonian
H = (1/2)*u^2 + p1*x2 - p2*x2 + p2*u;
% --------------------- Necessary COnditions -------------------------
%State Equations (I have substituted directly)
x1dot = x2;
x2dot = -x2 + u;
% Costate Equations (I wanted to experiment with diff ) - for my other large problem
p1dot = -diff(H,x1);
p2dot = -diff(H,x2);
% COntrol Equations
dHdu = diff(H,u);
% -----------------------------------------------------------------------
sol_u = solve(dHdu,u);
% Substitute u in second state equation
x2dot = subs(x2dot,u,sol_u);
% here I have to collect x1dot;x2dot;p1dot;p2dot as four odes and use dsolve
  1 Comment
Prateek Tiwari
Prateek Tiwari on 19 May 2020
Even I have a similar problem in solving the hamiltonian function.

Sign in to comment.

Accepted Answer

Divija Aleti
Divija Aleti on 29 Oct 2020
Hi Karthi,
The issue you have stated has been fixed in MATLAB R2020b, i.e., from this version onwards, the 'diff' function can take a symbolic function as its second input argument. So, ideally this should work:
syms p1(t) p2(t) x1(t) x2(t) u(t) H
% Write the Hamiltonian (Given)
H = (1/2)*u(t)^2 + p1(t)*x2(t) - p2(t)*x2(t) + p2*u(t);
% --------------------- Necessary COnditions -------------------------
%State Equations (Given)
x1dot = x2(t);
x2dot = -x2(t)+ u(t);
% Costate Equations (Using diff, so that the same method can be used for
% larger problems with complicated Hamiltonians)
p1dot = -diff(H,x1(t));
p2dot = -diff(H,x2(t));
% COntrol Equations
dHdu = diff(H,u(t));
% -----------------------------------------------------------------------
sol_u = solve(dHdu,u(t))
% Substitute u in second state equation
x2dot = subs(x2dot,u(t),sol_u);
% Solve for x1(t), x2(t), p1(t), p2(t)
eqns = [diff(x1,t)==x1dot, diff(x2,t)==x2dot, diff(p1,t)==p1dot, diff(p2,t)==p2dot];
conds = [x1(0)==0, x2(0)==0, x1(2)==5, x2(2)==2];
S=dsolve(eqns,conds);
However, 'solve' function cannot take symbolic functions as it's second input argument. This issue has been brought to the notice of our developers. They will investigate the matter further.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!