generateJacobianFunction
Generate MATLAB Jacobian functions for multistage nonlinear MPC using automatic differentiation
Since R2023a
Description
uses automatic differentiation to generate a MATLAB® function that calculates the derivatives, with respect to the plant states and
inputs, of a function specified in the multistage nonlinear MPC object
newMSobj = generateJacobianFunction(oldMSobj,type)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.
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
Name-Value Arguments
Output Arguments
Limitations
Automatic differentiation currently supports only a limited set of mathematical operations, which are described in Supported Operations on Expressions. If your original function uses operations or functions that are not in the list, or has if-else statements or loops,
generateJacobianFunctionterminates with an error.To generate Jacobian functions, do not preallocate any optimization variable. For example, do not try to generate Jacobians from a function containing the following code.
Instead, use the following code.dxdt = zeros(2,1); dxdt(1) = x(1)*x(2); dxdt(2) = x(1)/x(2);
For more information, see Resolve Error: Unable to Assign Expression During Automatic Differentiation.dxdt = [x(1)*x(2); x(1)/x(2)];
Specifying the state, cost, and constraint functions in
oldMSobjas 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.
Tips
If you design a controller for a data-driven plant model described by a idNeuralStateSpace (System Identification Toolbox)
or idnlgrey (System Identification Toolbox) model, the generateMATLABFunction (System Identification Toolbox) function generates both the model state transition
function as well as its Jacobians with respect to the state and input vectors. Therefore, in
this case, using generateJacobianFunction to generate the Jacobians of
the state transition is not necessary. On the other hand,
generateJacobianFunction is still useful to generate the Jacobians of
the cost and constraint functions.
Version History
Introduced in R2023a
See Also
Functions
validateFcns|nlmpcmove|nlmpcmoveCodeGeneration|getSimulationData|getCodeGenerationData|generateMATLABFunction(System Identification Toolbox)