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.
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!