The code for nmpc is not working even if i put it in same folder ?
1 view (last 30 days)
Show older comments
AMAN
on 22 Oct 2023
Commented: AMAN
on 23 Oct 2023
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
%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]
u=[5;07]
validateFcns(n1,x0,u)
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.
0 Comments
Accepted Answer
Emmanouil Tzorakoleftherakis
on 23 Oct 2023
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
on 23 Oct 2023
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:
More Answers (0)
See Also
Categories
Find more on Model Predictive Control Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!