why validateFcns is not working in my nlmpc Code?

8 views (last 30 days)
For your convinence i have attached the requird files that will save the copy paste time
% Parameters of SIRC model
n1=nlmpc(4,4,1);
Zero weights are applied to one or more OVs because there are fewer MVs than OVs.
n1.PredictionHorizon=100;
n1.ControlHorizon=10;
n1.Ts=1;
n1.Model.StateFcn="sircstatefcn";
n1.Model.OutputFcn="sircoutput";
S0 = 0.1; % Initial susceptible population
I0 = 0.3; % Initial infected population
R0 = 0.5; % Initial recovered population
C0 = 0.1;
x0 = [S0, I0, R0, C0]; % Initial conditions
u0=0.2;
validateFcns(n1,x0,u0)
Error using nlmpc/validateFcns
Function sircstatefcn does not exist.
function dx = sircstatefcn(x,u,p)
p.mu = 0.015;
p.alpha = 100;
p.delta = 0.625;
p.gamma = 0.30;
p.sigma= 0.1;
p.beta0 = 0.5;
fx=zeros(4,1);
fx(1)=p.mu*(1-x(1)) + p.gamma*x(4);
fx(2)= -(p.mu + p.alpha)*x(2);
fx(3)=p.alpha*x(2) - (p.mu + p.delta)*x(3);
fx(4)=p.delta*x(3) - (p.mu + p.gamma)*x(4);
gx=zeros(4,1);
gx(1)=-x(2)*x(1);
gx(2)=x(2)*x(1) + p.sigma*x(4)*x(2);
gx(3)=(1-p.sigma)*x(2)*x(4);
gx(4)=-x(4)*x(2);
dx=fx+gx*u;
%original state space model
% dx = [ p.mu*(1-x(1)) - u(1)*x(2)*x(1) + p.gamma*x(4);
% u(1)*x(2)*x(1) + p.sigma*u(1)*x(4)*x(2) - (p.mu + p.alpha)*x(2);
% (1-p.sigma)*u(1)*x(2)*x(4) + p.alpha*x(2) - (p.mu + p.delta)*x(3);
% p.delta*x(3) - u(1)*x(4)*x(2) - (p.mu + p.gamma)*x(4)];
dx=dx';
end
function y= sircoutput(x,u)
y(1)=x(1);
y(2)=x(2);
y(3)=x(3);
y(4)=x(4);
y=y';
end
Please copy both function and make seperate code files for them and then make seperate file for nlmpc evaluation and then execute the code.You will see the required error i was gettin which is ---------------------------------------------------
Error using nlmpc/validateFcns
Expecting 2 input arguments but "Model.StateFcn" appears to take 3 inputs.
Error in nmpcseir (line 15)
validateFcns(n1,x0,u0)

Accepted Answer

Walter Roberson
Walter Roberson on 29 Jan 2024
The problem is that when you specify a character vector for the function, then the function cannot be a local function (must have it's own .m file)
But you have another problem:
% Parameters of SIRC model
n1=nlmpc(4,4,1);
Zero weights are applied to one or more OVs because there are fewer MVs than OVs.
n1.PredictionHorizon=100;
n1.ControlHorizon=10;
n1.Ts=1;
n1.Model.StateFcn=@sircstatefcn;
n1.Model.OutputFcn=@sircoutput;
S0 = 0.1; % Initial susceptible population
I0 = 0.3; % Initial infected population
R0 = 0.5; % Initial recovered population
C0 = 0.1;
x0 = [S0, I0, R0, C0]; % Initial conditions
u0=0.2;
validateFcns(n1,x0,u0)
Error using nlmpc/validateFcns
Expecting 2 input arguments but "Model.StateFcn" appears to take 3 inputs.
function dx = sircstatefcn(x,u,p)
p.mu = 0.015;
p.alpha = 100;
p.delta = 0.625;
p.gamma = 0.30;
p.sigma= 0.1;
p.beta0 = 0.5;
fx=zeros(4,1);
fx(1)=p.mu*(1-x(1)) + p.gamma*x(4);
fx(2)= -(p.mu + p.alpha)*x(2);
fx(3)=p.alpha*x(2) - (p.mu + p.delta)*x(3);
fx(4)=p.delta*x(3) - (p.mu + p.gamma)*x(4);
gx=zeros(4,1);
gx(1)=-x(2)*x(1);
gx(2)=x(2)*x(1) + p.sigma*x(4)*x(2);
gx(3)=(1-p.sigma)*x(2)*x(4);
gx(4)=-x(4)*x(2);
dx=fx+gx*u;
%original state space model
% dx = [ p.mu*(1-x(1)) - u(1)*x(2)*x(1) + p.gamma*x(4);
% u(1)*x(2)*x(1) + p.sigma*u(1)*x(4)*x(2) - (p.mu + p.alpha)*x(2);
% (1-p.sigma)*u(1)*x(2)*x(4) + p.alpha*x(2) - (p.mu + p.delta)*x(3);
% p.delta*x(3) - u(1)*x(4)*x(2) - (p.mu + p.gamma)*x(4)];
dx=dx';
end
function y= sircoutput(x,u)
y(1)=x(1);
y(2)=x(2);
y(3)=x(3);
y(4)=x(4);
y=y';
end
  3 Comments
Walter Roberson
Walter Roberson on 29 Jan 2024
When I download the files, I get the same issue
Expecting 2 input arguments but "Model.StateFcn" appears to take 3 inputs.
You declare p as being in input parameter but you never use any property of p that you do not assign to. I suggest that you remove p as being an input parameter,
function dx = sircstatefcn(x,u)
AMAN
AMAN on 2 Feb 2024
Edited: AMAN on 2 Feb 2024
yes not declaingring p in function works thank you

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!