The code for nmpc is not working even if i put it in same folder ?

4 views (last 30 days)
nx = 4; %no. of states
ny = 2; %no. of outputs
nu = 2; %no. of inputs
n1 = nlmpc(nx,ny,nu);
n1.Model.StateFcn="statefunctiontank";
n1.Model.OutputFcn="outputtank";
n1.PredictionHorizon=400;
n1.ControlHorizon=50;
Ts = 0.25;
n1.Ts = Ts
n1 =
nlmpc with properties: Ts: 0.2500 PredictionHorizon: 400 ControlHorizon: 50 Dimensions: [1×1 struct] Model: [1×1 struct] States: [1×4 struct] OutputVariables: [1×2 struct] ManipulatedVariables: [1×2 struct] MeasuredDisturbances: [] Weights: [1×1 struct] Optimization: [1×1 struct] Jacobian: [1×1 struct] Passivity: [1×1 struct]
%setting constraints on states
n1.States(1).Max=40;
n1.States(1).Min=0;
n1.States(2).Max=40;
n1.States(2).Min=0;
n1.States(3).Max=40;
n1.States(3).Min=0;
n1.States(4).Max=40;
n1.States(4).Min=0;
%setting constraints on output
n1.Ov(1).Max=40;
n1.Ov(1).Min=0;
n1.Ov(2).Max=40;
n1.Ov(2).Min=0;
%setting constraints on input
n1.Mv(1).Max=10;
n1.Mv(1).Min=0;
n1.Mv(2).Max=10;
n1.Mv(2).Min=0;
%Validate model fucntion at arbitrary point
x0=[10;15;13;15]
x0 = 4×1
10 15 13 15
u=[5;07]
u = 2×1
5 7
validateFcns(n1,x0,u)
Error using nlmpc/validateFcns
Function statefunctiontank does not exist.
function dxdt= statefunctiontank(x,u)
A1 = 28;
A2 = 32;
A3 = 28;
A4 = 32;
a1 = 0.071;
a2 = 0.057;
a3 = 0.071;
a4 = 0.057;
gamma1 = 0.70;
gamma2 = 0.60;
g = 981;
dxdt(1) = -a1/A1*sqrt(2*g*x(1)) + a3/A1*sqrt(2*g*x(3))+(gamma1/A1)*u(1);
dxdt(2) = -a2/A2*sqrt(2*g*x(2)) + a4/A2*sqrt(2*g*x(4))+(gamma2/A2)*u(2);
dxdt(3) = -a3/A3*sqrt(2*g*x(3)) + ((1-gamma2)/A3)*u(2);
dxdt(4) = -a4/A4*sqrt(2*g*x(4)) + ((1-gamma1)/A4)*u(1);
end
function [y] = outputtank(x)
y=[x(1);x(2)];
end
these functions i was using as different code and called these in above code but i was not able to get what i should do to correct it.

Accepted Answer

Emmanouil Tzorakoleftherakis
I wasn't able to reproduce the error. Both state and output functions must be in different .m files. I did that and put everything on the same folder and did not get the error you are seeing (once you get it to work you will get a different error because you need to transpose the output dimensions of the state function).
  5 Comments
Emmanouil Tzorakoleftherakis
The error looks self-explanatory - take a look at the definition of the output function and make sure it's consistent with what the nlmpc object requires. Looks like there should be one more argument in there. This page provides more information on how to define state and output functions:

Sign in to comment.

More Answers (0)

Categories

Find more on Model Predictive Control Toolbox in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!