This repository explores the optimal control of a cart system subject to drag 1. It provides a comprehensive comparison between the analytical solution (derived via Pontryagin's Minimum Principle) and various numerical direct methods, including shooting, collocation and pseudospectral techniques. IPOPT is used for numerical optimization.
The code is available for both MATLAB/Octave and Python.
Ensure you have Python 3.11+ installed. You can install the dependencies via pip:
pip install casadi numpy matplotlibNotebooks: Interactive .ipynb files for all methods are located in the /python folder.
Utility: lglpsmethods.py contains the logic for differentiation matrices and LGL node generation.
Environment: MATLAB or Octave 6.1.0+
Dependency: CasADi 3.5.5+ must be added to your path
We aim to find the force
-
$z_1$ - position of the cart -
$z_2$ - velocity of the cart -
$f$ - force applied to the cart
Using Pontryagin's Minimization Principle, the exact solution for the state trajectories and control law is determined to be:
This project implements several Direct Methods to transform the continuous optimal control problem (OCP) into a nonlinear programming (NLP) problem.
| method | control parameterization | files |
|---|---|---|
| trapezoidal piecewise | piecewise constant control | trapezoidal_constant.m |
| Hermite Simpson | piecewise linear control | simpson.m |
| Runge Kutta 4 (single shooting) | piecewise constant control | single_shooting.m |
| Runge Kutta 4 (multiple shooting) | piecewise constant control | multiple_shooting.m |
| Legendre Gauss Lobatto 2 | global polynomial | LGL pseudospectral.m, legslb.m, legslbdiff.m, lepoly.m, lepolym.m |
All numerical methods in directmethods.py
The numerical methods closely approximate the analytical curves. Below are the comparisons of the phase plot and control effort.
Footnotes
-
Conway, B. A. and K. Larson (1998). Collocation versus differential inclusion in direct optimization. Journal of Guidance, Control, and Dynamics, 21(5), 780–785 ↩
-
Shen, Jie, Tao Tang, and Li-Lian Wang. Spectral methods: algorithms, analysis and applications. Vol. 41. Springer Science & Business Media, 2011. ↩