# generateJacobianFunction

Generate MATLAB Jacobian functions for multistage nonlinear MPC using automatic differentiation

*Since R2023a*

## Description

uses automatic differentiation to generate a MATLAB`newMSobj`

= generateJacobianFunction(`oldMSobj`

,`type`

)^{®} function that calculates the derivatives, with respect to the plant states and
inputs, of a function specified in the multistage nonlinear MPC object
`oldMSobj`

and indicated by `type`

.

After successfully generating and saving the function file (along with two supporting
files) in the same folder as the one in which the original function is located,
`generateJacobianFunction`

returns the new multistage nonlinear MPC
object `newMSobj`

. This returned object is the same as the original
object `oldMSobj`

but has its corresponding Jacobian function fields (in
its `Model`

and `Stages`

properties) updated to use
the generated Jacobian function.

The name of the generated Jacobian function is the same as the original function with
the `Jacobian`

appended at the end. If a stage of
`oldMSobj`

does not have any cost or constraint function defined, no
cost or constraint Jacobian function is generated for that stage.

For more information or automatic differentiation, see Automatic Differentiation Background (Optimization Toolbox).

`generateJacobianFunction(___,`

also specifies the stage number and whether to save the generated function files in the
current folder, using one or more name-value pair arguments.`Name=Value`

)

## Examples

## Input Arguments

## Output Arguments

## Limitations

Automatic differentiation currently supports only a limited set of mathematical operations, which are described in Supported Operations for Optimization Variables and Expressions (Optimization Toolbox). If your original function uses operations or functions that are not in the list, or has if-else statements or loops,

`generateJacobianFunction`

terminates with an error.To generate Jacobian functions, do not preallocate any optimization variable. For example, suppose you try to generate Jacobians from a function containing the following code.

This code results in the following error.dxdt = zeros(2,1); dxdt(1) = x(1)*x(2); dxdt(2) = x(1)/x(2);

Instead, use the following code.Unable to perform assignment because value of type 'optim.problemdef.OptimizationExpression' is not convertible to 'double'.

Note that you can use conditional preallocation usingdxdt = [x(1)*x(2); x(1)/x(2)];

`optimexpr`

to preallocate variables; however, doing so results in a Jacobian function that does not support C/C++ code generation for deployment. Therefore, the best practice is to avoid preallocation entirely. For more information, see Initialize Optimization Expressions (Optimization Toolbox).Specifying the state, cost, and constraint functions in

`oldMSobj`

as files in the current folder or in a folder on the MATLAB path is recommended. While handles to local functions are supported for Jacobian function generation, they are not supported for generation of C/C++ deployment code. For more information on local functions, see Local Functions.

## Version History

**Introduced in R2023a**