Hi @John Carlo Perion,
After reviewing the documentations provided at the links below
https://www.mathworks.com/help/mpc/ref/nlmpc.html
Here is how you can transfer the necessary functions from your idnlarx object to the nlmpc controller by following these steps:
Step 1: Extracting Functions from idnlarx
Obtain State and Output Functions: After creating your `idnlarx` model (let's call it sys), you can access its state and output functions directly:
stateFcn = sys.StateFcn; % Extracts the state function outputFcn = sys.OutputFcn; % Extracts the output function
Verify Function Types: Ensure that both extracted functions are compatible with the nlmpc requirements. They should be specified as either strings (if they refer to function names) or as function handles.
Step 2: Setting Up the Nonlinear MPC Controller
Create nlmpc Object: Initialize your nonlinear MPC controller by specifying the number of states (nx), outputs (ny), and inputs (nu). You can also specify manipulated variables, measured disturbances, etc.
nx = <number_of_states>; % Set this based on your model ny = <number_of_outputs>; % Set this based on your model nu = <number_of_inputs>; % Set this based on your model nlobj = nlmpc(nx, ny, nu);
Assign State and Output Functions: Now you can assign the extracted state and output functions to your nlmpc object:
nlobj.Model.StateFcn = stateFcn; % Assign extracted state function nlobj.Model.OutputFcn = outputFcn; % Assign extracted output function
Configure Additional Properties: Set other properties such as sample time, prediction horizon, and control horizon:
nlobj.Ts = <sample_time>; % Define sample time nlobj.PredictionHorizon = <prediction_horizon>; % Define prediction horizon nlobj.ControlHorizon = <control_horizon>; % Define control horizon
Step 3: Validate and Run
Validation: Before running simulations or deploying your controller, validate that everything is correctly set up:
x0 = <initial_state_vector>; u0 = <initial_input_vector>; validateFcns(nlobj, x0, u0);
Run NMPC: Finally, use the nlmpcmove function within a loop to compute optimal control actions over time:
for k = 1:num_steps [u, nloptions] = nlmpcmove(nlobj, xk, mv_ref); % Update states and inputs here... xk = <update_state_based_on_u>; % Store results as needed... end
Make sure that your state and output functions are properly defined without any direct feedthrough unless intended. Before deploying in a real system, simulate extensively with various scenarios to ensure stability and performance.
By following these structured steps, you should be able to effectively transfer information from your identified nonlinear ARX model into a nonlinear model predictive controller setup in MATLAB.
Hope this helps.
Please let me know if you have any further questions.