The problem was adapted from Example 4.11 from .
Consider the system illustrated below
If we neglect viscous friction, consider a unit mass and let u=F, then the optimal control problem to find control input such that the object is repositioned from x=0 [m] to x=300 [m] in mimimum time will be
Subject to dynamics constraints
simple bounds on variables
and boundary conditions
We require the numerical solution to fulfill the following accuracy criteria
We can specify the system dynamics in function BangBang_Dynamics_Internal with
For this example problem, we do not have any path constraints, so function BangBang_Dynamics_Internal only need to return the value of variable dx .
Similarly, the (optional) simulation dynamics can be specifed in function BangBang_Dynamics_Sim .
First we provide the function handles for system dynamics with,
the optional files providing analytic derivatives,
and the function handle for the settings file.
Next we define the time variables with
For state variables, we need to provide the initial conditions
bounds on intial conditions
bounds on the absolute local and global (integrated) discretization error of state variables
tolerance for state variable box constraint violation
terminal state bounds
and an initial guess of the state trajectory (optional but recommended)
Lastly for control variables, we need to define simple bounds
bounds on the first control action
tolerance for control variable box constraint violation
as well as an initial guess of the input trajectory (optional but recommended)
Since this problem only has a Mayer (boundary) cost , and no Lagrange (stage) cost ,we simply have
for sub-function E_unscaled and L_unscaled respectively.
For this example problem, we do not have additional boundary constraints (in addition to varialbe simple bounds). Thus we can leave routines b_unscaled as they are.
The analytical derivatives for the problem can be supplied with the following additional files: gradCost_BangBang.m for cost gradient, jacConst_BangBang.m for constraint Jacobian, and hessianLagrangian_BangBang.m for the Lagrangian Hessian. Instructions on how to formulate these files are included in the function descriptions.
Now we can fetch the problem and options, solve the resultant NLP, and generate some plots for the solution.
Using the 'AutoDirect' (Automatic chosen direct collocation) method of ICLOCS2, the following state and input trajectories are obtained after one mesh refinement iterations with the Hermite-Simpson discretization scheme. The graphs are generated by ICLOCS 2.5 built-in genSolutionPlots command.
Starting from 30 equally spaced mesh points, it can be observed that additional mesh points are placed in the mesh refinement process to capture the control switching structure ('bang-bang' behavior). The computation time is about 0.7 seconds using analytical derivative calculations on an Intel i7-6700 desktop computer. Subsequent recomputation using the refined mesh and warm starting will take on average 0.2 seconds and 18 iterations.
For this problem, we may also use ICLOCS2's unique flexible hp-adaptive mesh to yield solution that better captures the switch of the input, without the need to add additional mesh points.
We start with four equally-spaced mesh points, each with a polynomial degree of four. As the NLP converges, the mesh intervals and collocation points start to gather around the control switching time instance, accurately capturing this discontinuity in the optimal solution at 20s.
 J. Betts, Practical Methods for Optimal Control and Estimation Using Nonlinear Programming: Second Edition, Advances in Design and Control, Society for Industrial and Applied Mathematics, 2010.