# Theory¶

Given at a set of first order continuous differential equations in time in implicit form:

where:

\(t\) is time

\(\mathbf{y}(t) \in \mathbb{R}^n\) the state vector at time \(t\)

\(\mathbf{r}(t) \in \mathbb{R}^m\) is the vector of specified (exongenous) inputs at time \(t\)

\(\mathbf{p} \in \mathbb{R}^p\) is the vector of constant parameters

From here on out, the notation \((t)\) will be dropped for convenience.

In opty, you would define these equations as a SymPy column matrix that contains SymPy expressions. For example, a simple compound pendulum is described by the first order ordinary differential equations:

In SymPy, this would look like:

```
>>> I, m, g, l, t = symbols('I, m, g, l, t')
>>> theta, omega, T = symbols('theta, omega, T', cls=Function)
>>> f = Matrix([theta(t).diff() - omega(t),
... I*omega(t).diff() + m*g*l*sin(theta(t)) - T(t)])
>>> f
⎡ -ω + θ̇ ⎤
⎢ ⎥
⎣I⋅ω̇ + g⋅l⋅m⋅sin(θ) - T⎦
```

One can then break up \(\mathbf{r}\) and \(\mathbf{p}\) into known, \(k\), and unknown, \(u\), quantities.

where the dimension of the unknown vectors are:

\(\mathbf{r}_u \in \mathbb{R}^q\)

\(\mathbf{p}_u \in \mathbb{R}^r\)

Then there are optimal state trajectories \(\mathbf{y}\), optimal unknown input trajectories \(\mathbf{r}_u\) and optimal unknown parameter values \(\mathbf{p}_u\) if some cost function, \(J(\mathbf{y}, \mathbf{r}_u, \mathbf{p}_u)\) is specified. For example, the integral of a cost rate \(L\) with respect to time:

Additionally, upper \(U\) and lower \(L\) boundary constraints on these variables can be specified as follows:

These constraints are commonly associated with:

path constraints

maximal bounds on the trajectories or parameter values

specific trajectory values at instances of time

This type of problem is called an optimal control problem with the objective of finding the open loop input trajectories and/or the optimal system parameters such that the dynamical system evolves in time in a way that minimizes the objective and enforces the constraints. This problem can be rewritten as a nonlinear programming (NLP) problem using direct collocation transcription methods and solved using a variety of optimization algorithms suitable for NLP problems [Betts2010].

Direct collocation transcribes the continuous differential equations into discretized difference equations using a variety of discrete integration methods. These difference equations are then treated as constraints and appended to the explicitly defined constraints shown above. These new constraints ensure that the system’s dynamics are satisfied at each discretized time instance and relieves the need to sequentially integrate the differential equations as one does in shooting optimization.

opty currently supports two first order integration methods: the backward Euler method and the midpoint method.

If \(h\) is the time interval between the \(N\) discretized instances of time \(t_i\), one can use the backward Euler method of integration to approximate the derivative of the state vector as:

Using the midpoint method the approximation of the derivative of the state vector is:

The discretized differential equation \(\mathbf{f}_i\) can the be written using both of the above approximations.

For the backward Euler method:

For the midpoint method:

Then, defining \(\mathbf{x}_i\) to be:

The above equations will create \(n(N-1)\) constraint equations and the optimization problem can formally be written as:

opty translates the symbolic definition of \(\mathbf{f}\) into \(\mathbf{f}_i\) and forms the highly sparse Jacobian of \(\frac{\partial\mathbf{f}_i}{\partial\mathbf{x}_i}\) with respect to \(\mathbf{x}_i\). These two numerical functions are highly optimized for computational speed, taking advantage of pre-compilation common sub expression elimination, efficient memory usage, and the sparsity of the Jacobian. This is especially advantageous if \(\mathbf{f}\) is very complex. The cost function \(J\) and it’s gradient \(\frac{\partial J}{\partial \mathbf{x}_i}\) must be specified by Python functions that return a scalar, or vector. Symbolic formulations of the cost function \(J\) are not yet supported and must be written in terms of \(\mathbf{x}_i\) manually.

## References¶

- Betts2010
Betts, J. Practical Methods for Optimal Control and Estimation Using Nonlinear Programming. Advances in Design and Control. Society for Industrial and Applied Mathematics, 2010. https://doi.org/10.1137/1.9780898718577.