Custom cost function nlmpc

5 views (last 30 days)
Kasper
Kasper on 11 May 2023
Hi I designed a Non linear mpc for a differential wheeled robot using the nlmpc function. The mpc has 7 states and 2 manipulated variables.
I want to design my own cost function to be J = x_e'*Q*x_e + u_e'*R*u_e;
where x_e is the state error and u_e is the input error (Q and R are weight matrices).
I d the following
nlobj = nlmpc(nx,ny,nu);
nlobj.Ts = Ts_mpc;
nlobj.PredictionHorizon = N;
nlobj.ControlHorizon = Nc;
nlobj.Model.IsContinuousTime = false;
nlobj.Optimization.ReplaceStandardCost = true;
nlobj.Model.StateFcn = @MPC_state_function;
nlobj.Optimization.CustomCostFcn = @Cost_function;
However doing this I get a dimensioning error in simulink (using the nonlinear mpc block), it states that my manipulated variables (mv) output from the block is 1 for some reason.
The error is:
Error using Main
Compilation of model 'Robot_model' failed while trying to resolve underspecified signal dimensions.
Suggested Actions:
Enable 'warning' or 'error' diagnostics for the list of underspecified signal dimensions. - Open
Caused by:
Error using Main
Error in port widths or dimensions. The signal connected to the "last_mv" port of the "Robot_model/Control/Nonlinear MPC Controller" block must be a row or column vector signal with 2
elements.
What is causing this? if I go back to not specify my own cost function it works fine, there should be no error in the cost function as the help page states that the output of the cost function should be a scalar.
Best regards
  1 Comment
Emmanouil Tzorakoleftherakis
Edited: Emmanouil Tzorakoleftherakis on 16 May 2023
Hi,
There is not enough information to find the reason you are getting this error. If you share a reproduction model I can take a look.

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!