Optimal Control Matlab
Optimal Control Matlab
Optimal Control Matlab
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
16.323 Lecture 7
tf
1
2
10
10
10
10
10
Spr 2008
Simple Problem
Performance
tf
J=
16.323 71
(u x)2dt
t0
Form Hamiltonian
H = (u x)2 + pu
Necessary conditions become:
x = u
p = 2(u x)(1)
0 = 2(u x) + p
(7.25)
(7.26)
(7.27)
(7.28)
(7.29)
(7.30)
Spr 2008
16.323 72
(7.31)
(7.32)
with BC x0 = y0 = 0, xf = 1, yf = 0
Final time is unspecied.
p 1 = c1
(7.33)
p 2 = c2
(7.34)
Spr 2008
16.323 73
(7.35)
(7.36)
(7.37)
(7.38)
1
V cos
Rearrange to get
cos =
sin =
w
V
V 2 w2
V
= arcsin
w
V
Spr 2008
Numerical Solutions
16.323 74
Most of the problems considered so far have been simple. Things get
more complicated by the need to solve a two-point boundary value
problem when the dynamics are nonlinear.
Numerous solution techniques exist, including shooting methods13
and collocation
Will discuss the details on these later, but for now, let us look at
how to solve these use existing codes
Matlab code called BVP4C exists that is part of the standard package 14
Solves problems of a standard form:
y = f (y, t, p)
atb
reference
help and BVP4C Tutorial
Spr 2008
16.323 75
Figure 7.1: Results suggest a good comparison with the dynamic LQR result
Spr 2008
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function m = TPBVPlqr(p1,p2,p3)
t_f=10;x0=[1 1];
Rxx=p1;Ruu=p2;Ptf=p3;
solinit = bvpinit(linspace(0,t_f),@TPBVPlqrinit);
sol = bvp4c(@TPBVPlqrode,@TPBVPlqrbc,solinit);
time = sol.x;
%-------------------------------------------------------------------------
function dydt=TPBVPlqrode(t,y)
%-------------------------------------------------------------------------
function res=TPBVPlqrbc(ya,yb)
res=[ya(1) - x0(1);ya(2)-x0(2);yb(3:4)-Ptf*yb(1:2)];
%-------------------------------------------------------------------------
function v=TPBVPlqrinit(t)
global A B x0 b alp
v=[x0;1;0];
return
25
26
27
28
29
30
31
32
33
34
35
36
% Jonathan How
global A B
tf=10;dt=.01;time=[0:dt:tf];
37
38
39
40
41
42
43
44
45
46
P=zeros(2,2,length(time));K=zeros(1,2,length(time));
Pcurr=Ptf;
for kk=0:length(time)-1
P(:,:,length(time)-kk)=Pcurr;
K(:,:,length(time)-kk)=inv(Ruu)*B*Pcurr;
Pdot=-Pcurr*A-A*Pcurr-Rxx+Pcurr*B*inv(Ruu)*B*Pcurr;
Pcurr=Pcurr-dt*Pdot;
end
47
48
49
50
51
52
53
54
xdot1=(A-B*K(:,:,kk))*x1(:,:,kk);
xcurr1=xcurr1+xdot1*dt;
end
55
56
57
58
59
60
61
figure(3);clf
plot(time,squeeze(x1(1,1,:)),time,squeeze(x1(2,1,:)),--,LineWidth,2),
16.323 76
Spr 2008
Conversion
16.323 77
BVP4C sounds good, but this standard form doesnt match many of
the problems that we care about
In particular, free end time problems are excluded, because the time
period is dened to be xed t [a, b]
Can convert our problems of interest into this standard form though
using some pretty handy tricks.
U. Ascher and R. D. Russell, Reformulation of Boundary Value
Problems into Standard Form, SIAM Review, Vol. 23, No. 2,
238-254. Apr., 1981.
Spr 2008
16.323 78
Recall that our basic set of necessary conditions are, for t [t0, tf ]
x = a(x, u, t)
p = HxT
Hu = 0
Then
x = a(x, u, t) x = tf a(x, u, )
and
p = HxT
p = tf HxT
Spr 2008
Example: 71
16.323 79
0 = bu + 0 1 p
with state conditions
x1(0)
x2(0)
x1(tf )
x2(tf )
0.5bu2(tf ) + tf
=
=
=
=
=
10
0
0
0
0
A B 0 1 /b 0
= z5 0
AT
0 z
0
0
0
z = f (z) which is nonlinear
with BC:
z1(0)
z2(0)
z1(1)
z2(1)
=
=
=
=
10
0
0
0
0.5 2
z (1) + z5(1) = 0
b 4
June 18, 2008
Spr 2008
16.323 710
tf
1
2
10
10
10
10
10
Figure 7.2: Comparison of the predicted completion times for the maneuver
Spr 2008
16.323 711
3
2
u(t)
1
0
1
2
3
0.5
1.5
2.5
3.5
4.5
Time
8
uAnalytic(t)UNumerical
x 10
Analytic
3.5
3
2.5
2
1.5
1
0.5
0.5
1.5
2.5
3.5
4.5
Time
10
1
Analytic
Numerical
dX(t)/dt
X(t)
8
6
4
2
0
Analytic
Numerical
1
2
3
Time
8
Time
7
x 10
x 10
0.8
Error
Error
0.6
0.4
2
0.2
1
0
Time
Time
Spr 2008
TPBVP
1
2
3
4
5
function m = TPBVP(p1,p2)
% 16.323 Spring 2007
% Jonathan How
%
global A B x0 b alp
6
7
8
9
10
11
12
13
14
solinit = bvpinit(linspace(0,1),@TPBVPinit);
sol = bvp4c(@TPBVPode,@TPBVPbc,solinit);
15
16
17
18
19
20
21
22
23
time = sol.y(5)*sol.x;
state = sol.y([1 2],:);
adjoint = sol.y([3 4],:);
control = -(1/b)*sol.y(4,:);
m(1,:) = time;
m([2 3],:) = state;
m([4 5],:) = adjoint;
m(6,:) = control;
24
25
26
27
28
%-------------------------------------------------------------------------
function dydt=TPBVPode(t,y)
global A B x0 b alp
dydt=y(5)*[ A -B*[0 1]/b zeros(2,1); zeros(2,2) -A zeros(2,1);zeros(1,5)]*y;
29
30
31
32
33
%-------------------------------------------------------------------------
function res=TPBVPbc(ya,yb)
global A B x0 b alp
res=[ya(1) - x0(1);ya(2)-x0(2);yb(1);yb(2);-0.5*yb(4)^2/b+ alp*yb(5)];
34
35
36
37
38
%-------------------------------------------------------------------------
function v=TPBVPinit(t)
global A B x0 b alp
v=[x0;1;0;1];
39
40
return
41
16.323 712
Spr 2008
TPBVP Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
figure(1);clf
semilogx(alp,(1800*b./alp).^0.2,-,Linewidth,2)
xlabel(\alpha,FontSize,12);ylabel(t_f,FontSize,12)
legend(Analytic,Numerical)
21
22
23
24
25
26
27
28
29
30
b=0.1;alpha=0.1;
m=TPBVP(b,alpha);
tf=(1800*b/alpha)^0.2;
c1=120*b/tf^3;
c2=60*b/tf^2;
u=(-c2+c1*m(1,:))/b;
[y3,t3]=lsim(G,u,m(1,:),X0);
31
32
33
34
35
36
37
38
39
40
41
42
figure(2);clf
subplot(211)
plot(m(1,:),u,g-,LineWidth,2);
xlabel(Time,FontSize,12);ylabel(u(t),FontSize,12)
subplot(212)
plot(m(1,:),abs(u-m(6,:)),-)
xlabel(Time,FontSize,12)
ylabel(u_{Analytic}(t)-U_{Numerical},FontSize,12)
legend(Analytic,Numerical)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
figure(3);clf
subplot(221)
plot(m(1,:),y3(:,1),c-,LineWidth,2);
xlabel(Time,FontSize,12);ylabel(X(t),FontSize,12)
legend(Analytic,Numerical)
subplot(222)
plot(m(1,:),y3(:,2),c-,LineWidth,2);
xlabel(Time,FontSize,12);ylabel(dX(t)/dt,FontSize,12)
legend(Analytic,Numerical)
subplot(223)
plot(m(1,:),abs(y3(:,1)-m(2,:)),k-)
xlabel(Time,FontSize,12);ylabel(Error,FontSize,12)
subplot(224)
plot(m(1,:),abs(y3(:,2)-m(3,:)),k-)
xlabel(Time,FontSize,12);ylabel(Error,FontSize,12)
16.323 713
Spr 2008
Zermelos Problem
16.323 714
where tf is free.
Initial conditions are:
x(0) = x0
y(0) = y0
y(tf ) = y1
Spr 2008
16.323 715
Spr 2008
TPBVPZermelo
1
2
function m = TPBVPzermelo(p1,p2)
global x0 x1 V w
3
4
5
solinit = bvpinit(linspace(0,1),@TPBVPinit);
sol = bvp6c(@TPBVPode,@TPBVPbc,solinit);
6
7
8
9
10
time = sol.y(5)*sol.x;
control = atan2(-sol.y([4],:),-sol.y([3],:));
11
12
13
14
15
16
m(1,:) = time;
m(6,:) = control;
return
17
18
19
20
%-------------------------------------------------------------------------
function dydt=TPBVPode(t,y)
global x0 x1 V w
21
22
23
24
25
% x y p1 p2 t
% minimizing form
sinth=-y(4)/sqrt(y(3)^2+y(4)^2);
costh=-y(3)/sqrt(y(3)^2+y(4)^2);
26
27
28
29
30
31
32
33
34
dydt=y(5)*[V*costh ; V*sinth+w;0;0;0];
%-------------------------------------------------------------------------
function res=TPBVPbc(ya,yb)
global x0 x1 V w
% x y p1 p2 t
% minimizing form
costhb=-yb(3)/sqrt(yb(3)^2+yb(4)^2);
sinthb=-yb(4)/sqrt(yb(3)^2+yb(4)^2);
35
36
37
38
res=[ya(1) - x0(1);ya(2)-x0(2);
yb(1) - x1(1);yb(2)-x1(2);
1+V*costhb*yb(3)+V*(sinthb+w)*yb(4)];
39
40
41
42
43
44
45
%-------------------------------------------------------------------------
function v=TPBVPinit(t)
global x0 x1 V w
%v=[x0;-1;-1;norm(x1-x0)/(V-w)];
v=[x0;1;1;norm(x1-x0)/(V-w)];
return
46
47
48
49
50
51
clear all
global x0 x1 V w
w=1/sqrt(2);
mm=TPBVPzermelo;
52
53
54
55
56
57
58
59
60
61
figure(1);clf
plot(mm(2,:),mm([3],:),LineWidth,2);axis(square);grid on
axis([-2 5 -2 1.5 ])
xlabel(x,FontSize,12);ylabel(y,FontSize,12);
hold on;
plot(x0(1),x0(2),rs);plot(x1(1),x1(2),bs);
text(x0(1),x0(2),Start,FontSize,12)
text(x1(1),x1(2),End,FontSize,12)
hold off
62
63
64
65
figure(2);clf
plot(mm(1,:),180/pi*mm([6],:),LineWidth,2);grid on;axis(square)
xlabel(t,FontSize,12);ylabel(u,FontSize,12);
66
67
16.323 716
Spr 2008
68
69
70
71
72
73
74
clear all
global x0 x1 V w
w=1/sqrt(2);
x0=[0 1];x1=[0 0];V = 1;
mm=TPBVPzermelo;
75
76
77
78
79
80
81
82
83
84
figure(1);clf
plot(mm(2,:),mm([3],:),LineWidth,2);axis(square);grid on
axis([-2 5 -2 1.5 ])
xlabel(x,FontSize,12);ylabel(y,FontSize,12);
hold on;
plot(x0(1),x0(2),rs);plot(x1(1),x1(2),bs);
text(x0(1),x0(2),Start,FontSize,12)
text(x1(1),x1(2),End,FontSize,12)
hold off
85
86
87
88
figure(2);clf
plot(mm(1,:),180/pi*mm([6],:),LineWidth,2);grid on;axis(square)
xlabel(t,FontSize,12);ylabel(u,FontSize,12);
89
90
91
92
16.323 717
Spr 2008
16.323 718
Goal: (Bryson page 66) determine the maximum radius orbit transfer
in a given time tf assuming a constant thrust rocket (thrust T ).15
Must nd the thrust direction angle (t)
Assume a circular orbit for the initial and nal times
Nomenclature:
r radial distance from attracting center, with gravitational con
stant
v, u tangential, radial components of the velocity
m mass of s/c, and m
is the fuel consumption rate (constant)
T sin
u =
2+
r
r
m0 |m|t
uv
T cos
v = +
r
m0 |m|t
Dynamics :
u(0) = 0
v(0) =
r0
v(tf )
=0
r(tf )
u
2
T sin
H
= p
T
v
r
r
2 + m0|m |t
T cos
uv
r + m0 |m
|t
15 Thanks
to Geo Huntington
Spr 2008
16.323 719
T cos
T sin
p2
+ p3
=0
m0 |m|t
m0 |m|t
u(tf
)
m=
=0
v(tf ) r(t )
f
which gives
w = r + 1u(tf ) + 2 v(tf )
r(tf )
Since the rst state r is not specied at the nal time, must have that
w
2
p1(tf ) =
(tf ) = 1 +
r
2 r(tf )3
And note that
w
(tf ) = 2
v
p3(tf ) =
Spr 2008
16.323 720
Spr 2008
16.323 721
Spr 2008
Orbit Raising
1
2
3
4
5
6
7
8
9
10
11
Tf = 4;
12
13
14
15
16
%Constants
global mu m0 m1 T
17
18
19
20
21
22
23
24
25
26
27
28
29
30
n=100;
y = [ones(1,n); %r
zeros(1,n); %vr
ones(1,n); %vt
-ones(1,n); %lambda_r
-ones(1,n); %lambda_vr
-ones(1,n)]; %lambda_vt
x = linspace(0,Tf,n); %time
solinit.x = x;solinit.y = y;
tol = 1E-10;
31
32
33
34
35
36
37
38
39
%Solve
if four
sol = bvp4c(@orbit_ivp,@orbit_bound,solinit,options);
Nstep=40;
else
sol = bvp6c(@orbit_ivp,@orbit_bound,solinit,options);
Nstep=30;
end
40
41
42
43
44
45
46
47
48
%Plot results
figure(1);clf
plot(sol.x,sol.y(1:3,:),LineWidth,2)
legend(r,v_r,v_t,Location,NorthWest)
grid on;
axis([0 4 0 2])
title(HBVP Solution)
xlabel(Time);ylabel(States)
49
50
51
52
53
54
55
56
figure(2);clf
plot(sol.x,sol.y(4:6,:),LineWidth,2)
legend(p_1(t),p_2(t),p_3(t),Location,NorthWest)
grid on;
axis([0 4 -3 2])
title(HBVP Solution)
xlabel(Time);ylabel(Costates)
57
58
59
60
61
62
63
64
65
ang2=atan2(sol.y([5],:),sol.y([6],:))+pi;
figure(3);clf
plot(sol.x,180/pi*ang2,LineWidth,2)
grid on;
axis([0 4 0 360])
title(HBVP Solution)
norm([tan(ang2)-(sol.y([5],:)./sol.y([6],:))])
66
67
68
69
70
71
16.323 722
Spr 2008
72
73
74
75
76
dt=diff(sol.x);
dth=(sol.y(3,1:end-1)./sol.y(1,1:end-1)).*dt; % \dot \theta = v_t/r
th=0+cumsum(dth);
pathloc=[sol.y(1,1:end-1).*cos(th) sol.y(1,1:end-1).*sin(th)];
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
figure(4);clf
plot(pathloc(:,1),pathloc(:,2),k-,LineWidth,2)
hold on
zz=exp(sqrt(-1)*[0:.01:pi]);
r0=sol.y(1,1);rf=sol.y(1,end);
plot(r0*real(zz),r0*imag(zz),r--,LineWidth,2)
plot(rf*real(zz),rf*imag(zz),b--,LineWidth,2)
plot(r0,0,ro,MarkerFace,r)
plot(rf*cos(th(end)),rf*sin(th(end)),bo,MarkerFace,b)
fact=0.2;ep=ones(size(th,1),1)*pi/2+th-ang2(1:end-1);
xt=pathloc(:,1)+fact*cos(ep); yt=pathloc(:,2)+fact*sin(ep);
for i=1:Nstep:size(th,1),
pltarrow([pathloc(i,1);xt(i)],[pathloc(i,2);yt(i)],.05,m,-);
end;
%axis([-1.6 1.6 -.1 1.8]);
axis([-2 2 -.1 1.8]);
axis(equal)
hold off
96
97
1
2
global mu m0 m1 T
4
5
6
%State
8
9
10
sinphi = -lamu./sqrt(lamu.^2+lamv.^2);
cosphi = -lamv./sqrt(lamu.^2+lamv.^2);
11
12
13
14
15
%Dynamic Equations
dr = u;
du = v^2/r - mu/r^2 + T*sinphi/(m0 + m1*t);
dv = -u*v/r + T*cosphi/(m0 + m1*t);
16
17
18
19
20
21
1
2
3
4
5
6
%Initial State
r = x(1);u = x(2);v = x(3);
lamr = x(4);lamu = x(5);lamv = x(6);
7
8
9
10
%Final State
r2 = x2(1);u2 = x2(2);v2 = x2(3);
lamr2 = x2(4);lamu2 = x2(5);lamv2 = x2(6);
11
12
13
14
15
16
17
18
%Boundary Constraints
b1 = r - 1;
b2 = u;
b3 = v - sqrt(mu/r);
b4 = u2;
b5 = v2 - sqrt(mu/r2);
b6 = lamr2 + 1 - lamv2*sqrt(mu)/2/r2^(3/2);
19
20
21
%Residual
res = [b1;b2;b3;b4;b5;b6];
16.323 723