A MATLAB Script That Demonstrates Aerospace Trajectory Optimization Using Direct Transcription and Collocation
A MATLAB Script That Demonstrates Aerospace Trajectory Optimization Using Direct Transcription and Collocation
In the language of optimal control theory, this problem is a “continuous system with functions of
the state variables prescribed at a fixed terminal time”. The numerical example given in the text is
a continuous, low-thrust coplanar orbit transfer from Earth to Mars. The orbits of the planets are
assumed to be circular and coplanar, and the total transfer time is about 193 days.
Mathematical Background
A typical optimal control trajectory problem can be described by a system of dynamic variables
y ( t )
z=
u ( t )
consisting of the state variables y and the control variables u for any time t. In this discussion
vectors are denoted in bold.
The system dynamics are defined by a vector system of ordinary differential equations called the
state equations that can be represented as follows:
dy
y= = f y ( t ) , u ( t ) , p, t
dt
The initial dynamic variables at time t0 are defined by ψ0 ≡ ψ y ( t0 ) , u ( t0 ) , t0 and the terminal
conditions at the final time t f are defined by ψ f ≡ ψ y ( t f ) , u ( t f ) , t f . These conditions are
called the boundary values of the trajectory problem.
page 1
For any mission time t there are also simple bounds on the state variables
yl ≤ y (t ) ≤ yu
and the control variables
ul ≤ u ( t ) ≤ uu
The basic optimal control problem is to determine the control vector history that minimizes the
scalar performance index or objective function given by
J = φ y ( t0 ) , t0 , y ( t f ) , t f , p
Direct Transcription
The basic idea behind direct transcription involves discretizing the state and control representation
of a continuous aerospace trajectory. This technique allows the Optimal Control Problem (OCP)
to be “transcribed” into a Nonlinear Programming Problem (NLP). The OCP can be thought of as
an NLP with an infinite number of controls and constraints.
Each phase of an aerospace trajectory can be divided into segments or intervals such that
where tI is the initial time and tF is the final mission time. The individual time points are called
node, mesh or grid points. The value of the state vector at a grid point or node is y k = y ( tk ) and
the control vector is u k = u ( tk ) .
In the direct transcription method we treat the values of the states and controls at the nodes as a set
of NLP variables. Furthermore, the differential equations of the problem are represented by a
system of defect equality constraints that are enforced at each discretization node. In the direct
transcription method the state and control variable bounds become simple bounds on the NLP
variables. The defect constraints and variable bounds are imposed at all the grid points. If we
represent the state defects by a Hermite-Simpson discretization method, these constraints and
bounds are also imposed at the midpoints of each trajectory segment.
The following diagram illustrates the geometry of trajectory and control discretization. For this
simple example we have divided the trajectory into 8 segments. These 8 segments can be
represented by 9 discrete nodes with their corresponding times ti , states y i and controls ui . A
typical segment midpoint is also illustrated.
page 2
y 2
y 3
y 1
y 4
u 2 u 3
s e g m e n t
u u m id p o in t
1 4 y 5
y 5 u
u 5
5
y 6
u 6
y 9
y 7
y
u 7
8
u 9
u 8
t1 t2 t3 t4 t5 t6 t7 t8 t9
Collocation
T
x = y 0 , u0 , y1 , u1 ,… , y f , u f , p, t0 , t f
hk
ζ k = y k − y k −1 − [fk + fk −1 ]
2
to zero for k = 1,… , ns . The step size is denoted by hk ≡ tk − tk −1 , and the right hand side of the
differential equations are given by fk ≡ f y ( tk ) , u ( tk ) , p, tk .
For the compressed Hermite-Simpson discretization or collocation method, the NLP variables are
T
x = ( y, u, u m )i , ( y, u, u m )i +1 ,… , ( y , u ) f , t0 , t f
where u m1 , u m2 , etc. are values of the controls at the midpoints of the discretization segments.
page 3
The defects for this discretization algorithm are given by
hk
ζ k = y k +1 − y k −
6
( )
f ( y k +1 , u k +1 ) + 4f y mk , u mk + f ( y k , u k )
where f ( x, u ) represents the equations of motion evaluated at the nodes and midpoints.
The state vector and equations of motion at the midpoint of a segment are given by
1 h
y mk = [ y k + y k +1 ] + k f ( y k , uk ) − f ( y k +1 , uk +1 )
2 8
and
h
fmk = f y mk , u mk , p, tk + k
2
for k = 1,… , nN − 1
In these equations hk is the time interval between segments. For equal duration segments hk is
equal to ( t f − ti ) ( nN − 1) where nN is the number of nodes and nN − 1 is the number of
segments.
By treating the state vector defects as equality constraints, the NLP algorithm attempts to converge
the trapezoidal approximation to the actual trajectory as defined by the right-hand-sides of the
equations of motion. Furthermore, setting the vector of defects to zero enforces continuity at the
trajectory nodes.
tn +1 tn +1
xn +1 = xn + ∫ xdt = xn + ∫ f ( x )dt
tn tn
xn +1 + xn
xn +1 = xn + ∆t
2
xn +1 + xn
xn +1 − xn − ∆t = 0
2
page 4
which can also be expressed as
f ( xn +1 ) + f ( xn )
∆ n = xn +1 − xn − ∆t = 0
2
In these equations, f ( x ) is the first order equation of motion and ∆t = tn +1 − tn is the time
increment between nodes.
For example, a problem with 8 nodes (7 segments) can be written compactly as follows:
x1
∆1 −1 1 0 0 0 0 0 0 f ( x1 ) + f ( x2 )
x
∆ 0 −1 1 0 0 0 0
2
0
x3
2
f ( x2 ) + f ( x3 )
∆ 3 0 0 −1 1 0 0 0 0 f ( x3 ) + f ( x4 )
x 4 ∆t
∆ 4 = 0 0 0 −1 1 0 0 0 + f ( x4 ) + f ( x5 )
∆ 0 0 0 0 −1 1 0 x5 2
0 f ( x5 ) + f ( x6 )
5 x6
∆ 6 0 0 0 0 0 −1 1 0 f ( x6 ) + f ( x7 )
x7
∆ 7 0 0 0 0 0 0 −1 1
x8 f ( x7 ) + f ( x8 )
The first-order, two-dimensional equations of motion for the Bryson-Ho Earth-to-Mars orbit
transfer problem are given by
dr
r= =u
dt
du v 2 µ T
u= = − 2 + sin φ
dt r r m
dv uv T
v= = − + cos φ
dt r m
where
r = radial position
u = radial velocity
v = transverse velocity
T= propulsive thrust
m= spacecraft mass
φ= thrust angle
µ= gravitational constant
page 5
The thrust angle is defined relative to the “local horizontal” or tangential direction at the
spacecraft’s position. It is measured positive above the local horizontal plane and negative below.
The in-plane thrust angle φ is the single control variable for this problem.
m ( t ) = m0 (1 − mt )
where m is the propellant flow rate of the propulsive device and m0 is the initial mass.
The flight conditions of the spacecraft in its initial circular orbit are as follows:
r ( 0 ) = r0
u ( 0 ) = u0 = 0
µ
v ( 0) = ν 0 =
r0
The boundary conditions that create a circular orbit at the final time t f are given by
u (t f ) = u f = 0
µ
v (t f ) − =0
r (t f )
The first equation states that the radial velocity should be zero and the second equation is a
boundary condition that forces the final velocity to be equal to the local circular velocity at the
final radial distance.
The initial mass and propulsive characteristics for the Earth to Mars orbit transfer are as follows:
T m0
= 0.1405
µ r02
page 6
The non-dimensional acceleration unit is µ r02 and the non-dimensional total flight time is given
by the following expression:
tf
r03 µ
The non-dimensional time unit used in the software is r03 µ . The non-dimensional value of the
gravitational constant µ is 1. The dimensional value of the Sun's gravitational constant is
132712441933 km 3 sec 2 . The non-dimensional value of the initial distance r0 is 1. Finally, the
dimensional value of the radius of the Earth's circular orbit is 149597870.691 kilometers or one
Astronomical Unit.
From the dimensional propellant flow rate and other equations we can determine the non-
dimensional propellant flow rate which is equal to −0.07487 . This interplanetary mission requires
about 1129 kilograms of propellant.
For this problem we would like to maximize the radius of the final circular orbit. Therefore the
objective function is J = − rf .
In order to transcribe the optimal control problem (OCP) into a nonlinear programming problem
(NLP) for computer solution, the user must provide an initial guess for the state and control
vectors at the nodes and the following information and software components:
(1) problem definition (number of trajectory states, number of discretization nodes, number of
control variables, initial and final conditions, etc.)
(2) right-hand-side of the equations of motion
(3) state vector defect equality constraints
(4) collocation method
(5) scalar object function
To demonstrate this process a MATLAB script called dto_matlab was created to solve the
Bryson-Ho example. This section documents the construction and operation of this software.
The software begins by defining such things as the total number of nodes, control variables and
differential equations. The following is the problem definition for this example.
ndiffeq = 3;
ncv = 1;
page 7
% number of discretization nodes
nnodes = 50;
nc_aux = 2;
The next part of the computer program calculates such things as the time values at each node, the
initial and final state and control guesses, and the lower and upper boundaries for the NLP
variables. For this example the initial guess for the state vector at each node is set to the value of
the initial state vector. The initial guess for the control variable at each node is simply set to zero.
The following is part of the source code that performs these operations.
tarray(1) = tinitial;
tarray(nnodes) = tfinal;
for i = 2:1:nnodes - 1
tarray(i) = (i - 1) * deltat;
end
The NLP problem is solved by calling a MATLAB interface routine to the SNOPT 6.0 algorithm.
The following is the syntax for this operation.
In the calling arguments dtofunc_trap is a user-supplied function that evaluates the objective
function and equality constraints for any given trajectory conditions.
page 8
MATLAB versions of SNOPT 6.0 for several computer platforms can be found at Professor Philip
Gill’s web site which is located at http://scicomp.ucsd.edu/~peg/.
The source code for the MATLAB function that calculates the current value of the objective
function, the equality constraints (defects) and the boundary constraints for this example is as
follows:
% inputs
% outputs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1:1:nnodes - 1
% time elements
tk = tarray(k);
if (k == 1)
% first node
nks = 0;
nkp1s = ndiffeq;
else
% reset to previous node
nks = (k - 1) * ndiffeq;
nkp1s = nks + ndiffeq;
end
for i = 1:1:ndiffeq
xk(i) = x(nks + i);
page 9
if (k == 1)
% first node
nkc = nlp_state;
nkp1c = nkc + ncv;
else
% reset to previous node
nkc = nlp_state + (k - 1) * ncv;
nkp1c = nkc + ncv;
end
for i = 1:1:ncv
uk(i) = x(nkc + i);
for i = 1:1:ndiffeq
resid(nks + i) = reswrk(i);
end
end
for i = 1:1:nc_defect
f(i + 1) = resid(i);
end
xfinal(3) = x(nlp_state);
f(1) = -xfinal(1);
f(nc_defect + 2) = xfinal(2);
% transpose
f = f';
page 10
% no derivatives
g = [];
The following is the source code for the MATLAB function that calculates the state defect vector
for the trapezoidal collocation algorithm.
% input
% tk = time at node k
% tkp1 = time at node k + 1
% xk = state vector at node k
% xkp1 = state vector at node k + 1
% uk = control variable vector at node k
% ukp1 = control variable at node k + 1
% output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global ndiffeq
hk = tkp1 - tk;
for i = 1:1:ndiffeq
resid(i) = xkp1(i) - xk(i) - 0.5d0 * hk * (xdk(i) + xdkp1(i));
end
The right-hand-side of the equations of motion is defined in a function called rhs.m. Here is the
MATLAB source code for this function.
% equations of motion
% input
page 11
% t = current time
% x = current state vector
% u = current control vector
% output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
theta = u(1);
xdot(1) = x(2);
Simulation Results
The following is the NLP algorithm header and the last five iterations for this example.
After the NLP algorithm has converged, the software will display a printout of the initial and final
conditions. The following is the screen display for this example.
program dto_trap
radius = 1.00000000
page 12
transverse velocity = 1.00000000
radius = 1.52471522
The following are plots of the thrust angle and several other flight parameters. Please note that the
radial distance and velocities are non-dimensional. For this example the MATLAB script used 50
discretization nodes.
100
50
thrust angle (degrees)
−50
−100
−150
−200
0 20 40 60 80 100 120 140 160 180 200
simulation time (days)
page 13
Trajectory Optimization Using Direct Transcription & Collocation
1.7
1.6
1.5
radial distance (au)
1.4
1.3
1.2
1.1
1
0 20 40 60 80 100 120 140 160 180 200
simulation time (days)
0.3
0.25
radial velocity (au/day)
0.2
0.15
0.1
0.05
0
0 20 40 60 80 100 120 140 160 180 200
simulation time (days)
page 14
Trajectory Optimization Using Direct Transcription & Collocation
1.15
1.1
1.05
transverse velocity (au/day)
0.95
0.9
0.85
0.8
0.75
0 20 40 60 80 100 120 140 160 180 200
simulation time (days)
References
“Using Sparse Nonlinear Programming to Compute Low Thrust Orbit Transfers”, John T. Betts,
The Journal of the Astronautical Sciences, Vol. 41, No. 3, July-September 1993, pp. 349-371.
“Optimal Interplanetary Orbit Transfers by Direct Transcription”, John T. Betts, The Journal of
the Astronautical Sciences, Vol. 42, No. 3, July-September 1994, pp. 247-268.
“Survey of Numerical Methods for Trajectory Optimization”, John T. Betts, AIAA Journal of
Guidance, Control and Dynamics, Vol. 21, No. 2, March-April 1998, pp. 193-207.
Practical Methods for Optimal Control Using Nonlinear Programming, John T. Betts, Society for
Industrial and Applied Mathematics, Philadelphia, 2001.
page 15