How to include a model (created by me at Simulink) in Matlab script?

so I have model predictive controller with a plant model, and I created the plant model using blogs, I couldn't use the state space representation because I have a disturbance acting on the system, so the only way to include it is by making the model with a blocks,
so anyway now i am writing a code for custom state estimation in script, and I need to mention the plant model I have used in this script, but when I write the name of the plant model or the name of the plant model with MPC, an error appears as
"error using mpc, the first input argument of the mpc command must be an LTI object or a structure of models and offsets"
so my question is how to include the model (I have created using block ) into my script?
% Custom State Estimation
ts=0.1;
mpcobj=mpc('plantmodel',ts)
setEstimator(mpcobj,'custom');
xmpc = mpcstate(mpcobj);
A=[1 0;0 1];
B=[0.0936 0.0936 0;0 0.0752 0];
C=[1 0;0 1];
Nc=2;
SOCpbref=75; % (en pourcentage)
SOCliref=85; % (en pourcentage)
t=25; % en seconds
Cmaxpb=22020; % As (Ampersecond)
Cmaxli=6000; % As (Ampersecond)
Ipb=[60 65 70 75 65 60 55 50 50 50 55 60 65 70 75 75 75 75 70 60 60 55 65 60 50]; % en Ampere
Ili=[70 75 80 85 75 70 65 60 60 60 65 70 75 80 85 85 85 85 80 70 70 65 75 70 60]; % en Ampere
Cpb= sum(Ipb)/t;
Cli= sum(Ili)/t;
SOCpb=(Cpb/Cmaxpb);
SOCli=(Cli/Cmaxli);
for t = 0:Nc
y = sys.C*xsys; % plant equations: output
YY(t+1) = y;
xmpc.Plant = [SOCpb, SOCli]; % state estimation
u = mpcmove(mpcobj,xmpc,[],[SOCpbref, SOCliref]); % y is not needed
UU(t+1) = u;
xsys = sys.A*xsys + sys.B*u; % plant equations: next state
end
command window:
Error using mpc
The first input argument of the "mpc" command must be an LTI object or a structure of models and offsets.
Error in customstateestimation (line 3)
mpcobj=mpc('model',ts)

 Accepted Answer

Hi jana,
Can you clarify this statement: "I couldn't use the state space representation because I have a disturbance acting on the system,"
The state space ss object can include all inputs to the system, i.e., control, disturbance, noise, etc.

10 Comments

hey paul,
yes sure,
i will show you the picture of the model and the only way to model it was by blocks, but if there another way please let me know,
and PH2 and Pgrid are manipulated variable as d(t) is disturbances, so they need to be seperated, they cannot be included in one matrice
I'm confused by the notation. Is this model a discrete-time model, where t is an integer index?
Disclaimer: I'm not an MPC user ....
Wouldn't the model be something like this (asusming both states are measured variables and the disturbance is not measured)?
% x = [SOC;LOH], u = [Ph2;Pgrid;d]; y = x (just guessing)
% Ts = 1 in absence of any other information
sys = ss(eye(2),[0.0936 0.0936 0.0936;-0.339 0 0],eye(2),0,1);
sys = setmpcsignals(sys,'UD',3,'MV',1:2)
sys = A = x1 x2 x1 1 0 x2 0 1 B = u1 u2 u3 x1 0.0936 0.0936 0.0936 x2 -0.339 0 0 C = x1 x2 y1 1 0 y2 0 1 D = u1 u2 u3 y1 0 0 0 y2 0 0 0 Input groups: Name Channels Unmeasured 3 Manipulated 1,2 Output groups: Name Channels Measured 1,2 Sample time: 1 seconds Discrete-time state-space model.
hey paul, thank you for your answer, but I need the disturbance alone in a matrice, not with the other manipulated variables, is there a way for that? the same in the picture that I have sent,
No, I don't know how to do that, and based on mpc I still don't understand why such a approach would be needed.
I don't think I understood you completely, is your method allow the disturbance enter as difference from the others inputs?
This line in your code
mpcobj=mpc('plantmodel',ts);
suggested to me that you are trying to design a model predictive controller based on a model of a plant. In accordance with the doc page for mpc the plant should be an LTI model or an identified linear model. Based on the equations you provided for the state propagation, it looks like you're trying to use an LTI model.
An LTI model is defined by (among other things) its A, B, C, and D matrices. The B and D matrices multiply the vector of inputs driving the system. These inputs can include inputs of different flavors, such as control inputs and disturbances. But the math doesn't care what the inputs are physically, which is why they can all be bundled together in one vector.
MPC Prediction Models indicates the plant is driven by a single input vector that contains up to three flavors of inputs: Manipulated Variables, Measured Disturbances, and Unmeasured Input Disturbances.
Becasue of how the LTI model is defined, we need to specify the plant with a single input vector, which is what this line does, where the three inputs to the plant are contained in a single vector of inputs, u
% x = [SOC;LOH], u = [Ph2;Pgrid;d]; y = x (just guessing)
% Ts = 1 in absence of any other information
sys = ss(eye(2),[0.0936 0.0936 0.0936;-0.339 0 0],eye(2),0,1);
The input to sys is a 3x1 vector. The MPC needs to know which of those inputs are which flavor, which is what this line does.
sys = setmpcsignals(sys,'UD',3,'MV',1:2)
In addition, the model sys has one vector of outputs which can contain two flavors: Unmeasured Output and Measured Outputs. setmpcsignals can be used to specify which elements of the output vector are which flavor.
okay that's great, but the ouput of this is it the same picture I showed it to you? or maybe I should try and see, what do you think?
Well, the picture showed did not define the output of the system, so I can't say.
I suggest reading the documentation and examples for the MPC toolbox and try to apply that material to the problem of interest.

Sign in to comment.

More Answers (1)

Hi,
Currently you cannot use a Simulink model as prediction model for MPC design. This is something we are working towards for a future release. Since it seems you are designing a linear MPC controller, you could linearize your Simulink model at the desired operating point to get a linear representation, or you could use system identification as well if you are fine with a data-driven model. Here are a couple of examples:
Hope this helps

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!