Creating nlmpc using idnlarx object

4 次查看(过去 30 天)
I am trying to create a non-linear model predictive controller from the system identified model I obtained using nlarx. However, I don't know how I can transfer the information such as StateFcn and OutputFcn from the idnlarx object into the nlmpc. Is this possible? If yes, can you please teach me the procedure to do this? Thank you!

回答(1 个)

Umar
Umar 2024-10-13

Hi @John Carlo Perion,

After reviewing the documentations provided at the links below

https://www.mathworks.com/help/ident/ref/idnlarx.html?searchHighlight=idnlarx&s_tid=srchtitle_support_results_1_idnlarx

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.

  2 个评论
John Carlo Perion
John Carlo Perion 2024-10-21
I think the idnlarx object does not have a StateFcn property.
Umar
Umar 2024-10-21

Hi @ John Carlo Perion,

Although the idnlarx object does not directly provide StateFcn and OutputFcn, you can create these functions using the model's structure.

Here is a complete example demonstrating how to set up the NMPC with random data:

% Define random data for the idnlarx model
n = 100; % Number of data points
u = rand(n, 1); % Input data
y = rand(n, 1); % Output data
na = 2; % Number of past outputs
nb = 2; % Number of past inputs
nk = 0; % Input delay
% Create the idnlarx model
data = iddata(y, u, 1);
model = nlarx(data, [na nb nk]);
% Define the state and output functions
stateFcn = @(x, u) [x(1) + u(1); x(2) + u(2)]; % Example state   function
outputFcn = @(x) x(1); % Example output function
% Create the nlmpc object
nlmpcObj = nlmpc(2, 1, 'MV', 1, 'OV', 1);
nlmpcObj.Model.StateFcn = stateFcn;
nlmpcObj.Model.OutputFcn = outputFcn;
% Define prediction horizon and control horizon
nlmpcObj.PredictionHorizon = 10;
nlmpcObj.ControlHorizon = 2;
% Display the nlmpc object
disp(nlmpcObj);

Note: iddata requires System Identification Toolbox.

first create an idnlarx model using random input and output data,then define the state and output functions based on the model's dynamics. Finally, instantiate the nlmpc object and assign the defined functions.

Please implement this code and let me know what happens.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by