# 16. Modelling Utilities¶

Like any derivative-based optimization solver, FORCESPRO works best if all functions defining the optimization problem are sufficiently smooth (i.e. at least continuously differentiable once). Both the Matlab and the Python client of FORCESPRO come along with a couple of utility functions to assist the user with setting up such smooth problem formulations. This chapter provides details on those utility functions for modelling.

## 16.1. Interpolations (e.g. splines)¶

If a given function is based on measurement data (or any other set of discrete data points), one can interpolate between those data points to yield a continuous function. FORCESPRO can either create such a function directly from the data points or allows you to provide a polynomial parameterization that can be used inside your symbolic problem formulation.

### 16.1.1. Polynomial Parameterization¶

A polynomial parameterization can be obtained by providing a vector containing M+1 break points (defining M interpolaton intervals or pieces) as well as an array defining M sets of N+1 polynomial coefficients, each set defining a local polynomial of order N for each of those pieces.

Calling the line

f = ForcesInterpolation(breaks, coefs);

f = forcespro.modelling.Interpolation(breaks, coefs)


will yield a symbolic representation of a polynomial in standard form

$f(x) = \sum\limits_{j=0}^N c_{ij} x^j \quad \forall\,b_{i-1} \leq x \leq b_i \quad \forall\,i \in\{1,\ldots,M\}\,,$

where b denotes the break points breaks and c denotes the coefficients coefs. f(x) can be a scalar or a K-dimensional function, i.e. coefs may be given for a multi-valued interpolation. For more details on how to pass those input parameters, we refer to the respective help function as the format differs slightly between the Matlab and the Python client to follow domain-specific conventions.

In case your coefficients are defined relative to beginning of each piece, you can call

f = ForcesInterpolation(breaks, coefs, 'pp');

f = forcespro.modelling.Interpolation(breaks, coefs, 'pp')


to yield a symbolic representation of a polynomial in “piecewise polynomial” form

$f(x) = \sum\limits_{j=0}^N c_{ij} (x-b_i)^j \quad \forall\,b_{i-1} \leq x \leq b_i \quad \forall\,i \in\{1,\ldots,M\}\,.$

Note

In addition to providing fixed numerical values for break points and coefficients, you may also pass symbolic quantities for some or all of those! This will allow you to change the parameterization of your interpolation on the fly, e.g. by means of real-time parameters that are passed to the FORCESPRO solver.

The symbolic interpolation f can now be used inside your problem formulation by evaluating it, either at a fixed value or at any symbolic quantity, e.g.

% assuming a state vector x and a control input u
y = f(x(1)) + u(1);

% assuming a state vector x and a control input u
y = f(x) + u


Important

Symbolic interpolations are currently only supported when using CasADi as AD tool.

### 16.1.2. Automatic Fit from Data¶

In case you do not want to specify break points and coefficients yourself, you can fit data points directly by calling:

f = ForcesInterpolationFit(X, Y, method);

f = forcespro.modelling.InterpolationFit(X, Y, kind)


Here, X and Y are vectors (say, of dimension L) of data points to yield an interpolation that satisfies

$f(X_i) = Y_i \quad \forall\,i \in\{1,\ldots,L\}\,.$

The third argument method/kind specifies the method to be used to obtain that fit using built-in functionality of either Matlab (see Table Table 16.1) or Python (see Table Table 16.2).

The symbolic interpolation f can be used the same way as described in section Section 16.1.1.

Table 16.1 Interpolation Method for Matlab (see Matlab’s interp1 for more details)

method

Description

'linear'

Piecewise linear

'nearest'

Piecewise constant, value from nearest data point

'next'

Piecewise constant, value from next data point

'previous'

Piecewise constant, value from previous data point

'spline' (default)

Piecewise cubic spline

'pchip'

Shape-preserving piecewise cubic spline

Table 16.2 Interpolation Method for Python (see SciPy’s interpolation class for more details)

kind

Description

'cubic' (default)

Piecewise cubic spline

'pchip'

Shape-preserving piecewise cubic spline

### 16.1.3. Application Example¶

A full example on how to use interpolations inside your problem formulation can be found here (MATLAB) or here (Python). Therein, both road limits are defined as splines and enforced as inequality constraints.

## 16.2. Smooth Approximations¶

There are a number of useful basic functions that are not differentiable everywhere. For some of them FORCESPRO provides a built-in smooth approximation and we plan to add more in an upcoming release.

### 16.2.1. Smooth Minimum¶

The minimum value of two scalars is not differentiable at the points where both values are identical. You can use the following smooth approximation instead:

c = ForcesMin(a, b);

c = forcespro.modelling.smooth_min(a, b)


This function accepts an optional third argument to trade-off smoothness and approximation quality. The default value is set to 1e-8; higher values make the function smoother but less accurate.

### 16.2.2. Smooth Maximum¶

The maximum value of two scalars is not differentiable at the points where both values are identical. You can use the following smooth approximation instead:

c = ForcesMax(a, b);

c = forcespro.modelling.smooth_max(a, b)


This function accepts an optional third argument to trade-off smoothness and approximation quality. The default value is set to 1e-8; higher values make the function smoother but less accurate.