How can one introduce the previous control inputs(manipulated variables sequence) from solved OCP to define a custom cost function in NLMPC.

5 views (last 30 days)
I am trying to create cust cost function that penalized rate of change of the input. I would appreciate some help
How can you introduce the last control inputs(manipulated variables) from OCP to define a custom custom function in NLMPC.
Accordnt to the guide "Specify Cost Function for Nonlinear MPC: mathworks.com/help/mpc/ug/specify-cost-function-for-nonlinear-mpc.html', the last control action can be found in data.LastMV. However this only return size (nu x 1) of the input dimension and not complete horizon. Is the an option to extract control input sequence for the full horizon from the "data". ? Thank you

Answers (1)

Rahul
Rahul on 29 Jul 2025
Edited: Rahul on 29 Jul 2025
Hi Bay,
I believe that you are working with a Nonlinear Model Predictive Controller (NLMPC) in MATLAB and looking to define a custom cost function that penalizes the rate of change of the control inputs. It appears that you are trying access to the full predicted sequence of manipulated variables (MVs) across the prediction horizon within the custom cost function.
As mentioned, the 'data.LastMV' field in the 'nlmpcmove' function only provides the last applied control input (of size 'nu × 1') prior to the current control action. This is useful for calculating the first delta in control effort (e.g., 'MV(1,:) - LastMV') but it does not give access to the full sequence of control inputs over the prediction horizon.
Key Points and Workarounds:
  • MV trajectory: The NLMPC framework in MATLAB does not expose the full MV trajectory to the custom cost or constraint functions during internal solver iterations. The solver treats the prediction and optimization internally, and only passes the current state, proposed MV, and the last MV as context to the custom function.
  • Typically, when penalizing input rate 'Δu', the most common approach is to compute only the first-step delta:
dU = MV(1,:) - data.LastMV;
cost = cost + weight * sum(dU.^2);
  • Limitations of Custom Cost Function: Since you cannot access future values of the MV sequence '(MV(2:end,:))', it's not possible within the current 'nlmpcmove' architecture to penalize the full sequence of input rate changes directly (like, 'sum_i ||MV(i+1) - MV(i)||^2') in the custom function.
Possible Alternatives:
  • Custom Solver Integration: If penalizing the full 'Δu' trajectory is critical to the application, you can consider building the NMPC using a lower-level framework such as Optimization Toolbox 'fmincon'. These allow full access to the decision variable trajectory, but require you to formulate and solve the OCP manually.
  • You can read more about the usage of 'fmincon' function in the following documentation link:
Moreover, here are some hybrid alternative using MATLAB NLMPC that you can consider:
  • Extending the internal state of the plant model to carry previous control inputs as internal states.
  • In the custom cost function, defining the rate penalty using these internal states.
While slightly indirect, these workarounds can enable encoding penalties that rely on historical MV behavior over time, which outlines what data is available at runtime and explains the role of 'data.LastMV'. You may refer to the following guide on custom cost functions here: https://www.mathworks.com/help/releases/r2024b/mpc/ug/specify-cost-function-for-nonlinear-mpc.html.
Although, 'data.LastMV' is the only historical control input available to the custom function, full future MV sequences aren't accessible within the standard 'nlmpcmove' framework. You would either need to design your cost using the available first-step delta or transition to a more flexible optimization-based controller architecture if penalizing the entire MV trajectory is necessary.
For more information regarding computing optimal control action for nonlinear MPC controller, you can refer to the following documentation link:
Hope this helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!