Very often the model of a dynamical system is given in the form of a continuous-time ordinary differential equation (ODE). For the Matlab interface, we offer implementations of the following standard methods for discretizing these dynamics for numerical computations with FORCES Pro:

Identifier | Type | Order |
---|---|---|

ForwardEuler |
Explicit Euler Method | 1 |

ERK2 |
Explicit Runge-Kutta | 2 |

ERK3 |
Explicit Runge-Kutta | 3 |

ERK4 |
Explicit Runge-Kutta | 4 |

BackwardEuler |
Implicit Euler Method | 1 |

IRK2 |
Implicit Runge-Kutta | 2 |

IRK4 |
Implicit Runge-Kutta | 4 |

FORCES Pro requires you to describe the dynamics of the system in the following form:

\[
\dot{x} = f(x, u, p)

\]

where \( x \) are the states of the system, \( u \) are the inputs and \( p \) a vector of parameters, e.g. the mass or inertia. For example, let's assume that the system to be controlled has the dynamics \[ \dot{x} = p_1 x_1 x_2 + p_2 u\enspace. \] Carry out the following steps to discretize the system for use with FORCES Pro:

- Implement the continuous-time dynamics as a Matlab function (here shown for the example above):

### Implement continuous time dynamics

`function xdot = continous_dynamics(x, u, p)`

xdot = p(1)*x(1)*x(2) + p(2)*u;

end

`continous_dynamics_anonymous = @(x,u,p) p(1)*x(1)*x(2) + p(2)*u;` - Tell FORCES Pro that you are using continuous-time dynamics by setting the
`continuous_dynamics`field of the model to a function handle to one of the functions above:

### Give FORCES Pro a function handle to the dynamics

`model.continuous_dynamics = @continuous_dynamics;``model.continuous_dynamics = continuous_dynamics_anonymous;` - Choose one of the integrator functions from the table below (the default is
`ERK4`):

### Set integrator / discretization method

`codeoptions.nlp.integrator.type = 'ERK2';`

codeoptions.nlp.integrator.Ts = 0.1;

codeoptions.nlp.integrator.nodes = 5;`type`field of the options struct`codeoptions.nlp.integrator`. It must carry one of the identifiers from the table above. The field`Ts`determines the absolute time between two integration intervals, while`nodes`defines the number of intermediate integration nodes within that integration interval. In the example above, we use 5 steps to integrate for 0.1 seconds, i.e. each integration step covers an interval of 0.1/5 = 0.02 seconds. - Usually an explicit integrator such as RK4 should suffice for most applications. If you have stiff systems, or suspect inaccurate integration to be the cause of convergence failure of the NLP solver, consider using implicit integrators from the table above.