Get Started

Runing a single solve

This guide will help you setup a single solve of optimal control problem with main.m.


Fetch, transcribe and solve the problem

First we load the formulated optimal control problem (in myProblem.m)

[problem,guess]=myProblem;

Then we can load the corrsponding options and solver settings. For fixed order h methods, use

options= settings_h(...);

with the input argument being the number of equally spaced mesh points. For pseudo spectral methods, two input arguments are needed.

options= settings_hp(...,...);

Scalar numbers can be used for equally spaced intervals with the same polynoimial orders. For example, settings_hp(5,4) means using 5 LGR intervals each of polynomial degree of 4. Alternatively, the user can supply two arrays in the argument with customized meshing. For example, settings_hp([4 5 3],[-1 0.3 0.4 1]) will have 3 segments on the normalized interval [-1 0.3], [0.3 0.4] and [0.4 1], with polynomial order of 4, 5, and 3 respectively.


Next, the problem can be transcribed and solved using the following standard command

[infoNLP,data,options]=transcribeOCP(problem,guess,options);
[solution,status,data] = solveNLP(infoNLP,data); 

with the returning structure variable solution containing only the raw output from the NLP solver (sampled data points). The solution construction (into continous trajectories via interpolation or fitting), error analysis and plottings can be done with the following command

[solution]=output(problem,solution,options,data,...);

The last input argument is an integer indicating figure generations.

  • 1: Plot all figures (state and input trajectory, multipliers/costate values and errors)
  • 2: Plot only the state and input trajectory
  • 3: Plot only the multipliers/costate values
  • 4: Plot only the error values (absolute local error, relative local error and absolute constraint violation error)

Solution variables

Here we provide short descriptions for the information contained in the solution variable

  • multipliers: a structure variable containing the multiplier/costate information
  • computation_time: time spent on solving the NLP problem
  • z: the NLP variable vector returned from the NLP solver
  • tf: the terminal(final) time
  • p: parameters values (part of optimization solution)
  • X: sampled state values at mesh points
  • x0: initial states
  • U: sampled input/control values at mesh points
  • T: a time array corresponding to the mesh points
  • z_orgscale: the NLP variable vector after transforming back to the original variable scale
  • multipliers: a structure variable containing information at all collocation points (for Hermite-Simpson method)
  • Xp: Constructed continuous state trajectory
  • dXp: Constructed continuous state rate (dynamics) trajectory
  • Up: Constructed input/control trajectory
  • Error: the absolute local error for state variables
  • ErrorRelative: the relative local error for state variables
  • ConstraintError: the absolute constraint violation error (organized as [path constraint upper bound, path constraint lower bound, state upper bound, state lower bound, input upper bound, input lower bound]
  • T_ConstraintError: a time array corresponding to the absolute constraint violation error evaluation
  • NumActiveConstraint: the number of active constraints
  • TSeg_Bar: time interval barriers between LGR polynomial segments (p/hp method only)
  • dX: sampled state rate values at mesh points
  • dU: sampled control rate values at mesh points

Evaluation of contructed solution

The constructed continuous trajectoris (Xp, dXp and Up) are in the form of piecewise splines, and can be evaluated in the following ways.

Suppose we want to obtain the values of the state at 1000 equally spaced points along the trajectory (defined below)

xx=linspace(solution.T(1,1),solution.T(end,1),1000);

For solutions obtained from h methods, the command for obtaining the corrsponding values of the third (second input argument) state variable will be

x1=speval(solution.Xp,3,xx);

and for p/hp methods, one additional input argument need to be provided

x1=speval(solution.Xp,3,solution.TSeg_Bar,xx);