How to write StateFcn in Nonlinear MPC Controller

9 views (last 30 days)
Nowdays, I am doing some works about visual servoing. But there are some troubles with the nonlinear MPC Controller. I have read the example named 'Control Quadruple-Tank Using Passivity-Based Nonlinear MPC'. There is a code nlobj.Model.StateFcn = "stateFcnQuadrupleTank".
I opened it, and the content shows as a picture as follows. It looks like the example uses code to express the dynamic system. I want to know if I can use a Simulink model, which is already done, to replace the code.
Thank a lot for your help!

Accepted Answer

Shubham
Shubham on 18 Jul 2024
Hi Frank,
I understand that you want to use a Simulink model as a state function of a Nonlinear MPC Controller.
To use a Simulink model instead of a code to express dynamic system, say for example "stateFcnQuadrupleTank", you need to follow these steps:
  • Ensure that your Simulink model correctly represents correct dynamics of the system
  • The model should have input and outputs corresponding to the states and control inputs
  • Generate an S-Function from your Simulink model using Simulink Encoder (if the model is simple, then directly use MATLAB Function block to define state conditions directly) which is given below:
% Load the Simulink model
load_system('model_name');
% Generate the S-Function
rtwbuild('model_name');
  • You can use the "nlmpc" object in MATLAB to define the state function using the generated S-Function:
% Define the Nonlinear MPC object
nlobj = nlmpc(4, 1); % Assuming 4 states and 1 output for the quadruple-tank system
% Define the state function using the generated S-Function
nlobj.Model.StateFcn = 'model_name_sfcn';
Please refer to the following documentation link for more information on how to use S-Function:

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!