NMPC with CasADi and Python – Part 2: Simulation of an uncontrolled system

CasADi is a powerful open-source tool for nonlinear optimization. It can be used with MATLAB/Octave, Python, or C++, with the bulk of the available resources referencing the former two options. This post series is intended to show a possible method of developing a simulation for an example system controlled by Nonlinear Model Predictive Control (NMPC) using CasADi and Python.

In this post, we will try to simulate an uncontrolled system with a forward Euler and a 4th order Runge-Kutta integration method. The latter can be the base for future closed-loop simulations.

NMPC with CasADi and Python – Part 1: ODE and steady state

CasADi is a powerful open-source tool for nonlinear optimization. It can be used with MATLAB/Octave, Python, or C++, with the bulk of the available resources referencing the former two options. This post series is intended to show a possible method of developing a simulation for an example system controlled by Nonlinear Model Predictive Control (NMPC) using CasADi and Python.

In this post, a file describing the system equations and a script to determine a steady-state setpoint will be developed. This older post contains similar code for CasADi inside MATLAB.

Linearize nonlinear state space equation in MATLAB at steady state setpoint

This post shows one way to linearize a nonlinear state equation $\dot{x} = f(x,u)$ at a steady state setpoint $(x_0, u_0)$ in MATLAB. It is assumed that a function ode.m exists in which the state equation is implemented:

function dx = ode(t, x, u)
% example ODE
dx1 = tan(x(4)) * x(2) + 2*u(2) - 1;
dx2 = x(1) - sin(x(2));
dx3 = 13 * x(4) + u(1) + 1;
dx4 = 2*x(1);

dx = [dx1; dx2; dx3; dx4];
end