Error flag for infeasibility in nonlinear mpc.
I am getting the error flag -2 for my nonlinear mpc design despite their being no constraints. If there is no constraints, how is there infeasibility? The only way I can get it to be feasible is to make the intial condition the exact same as the reference. If there is deviation the problem is infeasible
clear all
nx = 4;
ny = 2;
nu = 3;
nlobj = nlmpc(nx,ny,nu);
nlobj.Ts = 0.1;
nlobj.PredictionHorizon = 10;
nlobj.ControlHorizon = 10;
nlobj.Model.StateFcn = @mystateFunctionREDUCED;
nlobj.Model.IsContinuousTime = true;
nlobj.Model.NumberOfParameters = 5;
rho = 0.1;
l = 1/2*1*rho^2;
%T = params(3);
g = 9.81;
J = 0.015;
m = 2;
nlobj.Model.OutputFcn = @OutputFcn
;
x0 = [1;0;pi/8;0];
mv = [m*g+1; 0; 0];
lastMV = mv;
%validateFcns(nlobj, x0, u0, [], {rho,l,g,J,m});
nloptions = nlmpcmoveopt;
nloptions.Parameters = {rho,l,g,J,m};
Duration = 1;
Ts = 0.01;
xHistory = x0′;
yref = [1;pi/9];
%nlobj.Weights.OutputVariables = [3 3];
%nlobj.Weights.ManipulatedVariablesRate = 0.1;
% nlobj.States(1).Min = 0;
% nlobj.States(1).Max = 20;
% nlobj.States(3).Min = 0;
% nlobj.States(3).Max = pi;
%nlobj.Optimization.CustomIneqConFcn = "myIneqConFunctionreduced";
nlobj.Weights.ManipulatedVariablesRate = [0 0 0];
nlobj.Weights.OutputVariables = [1 1];
%nlobj.Weights.ECR = 0;
Tarray = [];
Uarray = [];
for k = 1:(Duration/Ts)
xk = xHistory(k,:);
% Compute optimal control moves
[mv,nloptions,info] = nlmpcmove(nlobj,xk,lastMV,yref’,[],nloptions);
info
%disp(mv)
% Implement first optimal control move
ODEFUN = @(t,xk) statefcn(xk,mv);
[TOUT,XOUT] = ode45(ODEFUN,[0 Ts], xHistory(k,:)’);
x0 = mystateFunctionREDUCED(x0,mv,rho,l,g,J,m)*Ts+x0;
uHistory(k+1,:) = mv’;
lastMV = mv;
%[t,y] = ode15s(@mystateFunction,[0 Ts],[2 0]);
%T = (m*x0(1)*x0(4)^2-m*g*sin(x0(3))+mv(1)*sin(x0(3)+x0(5))-m*rho/l*mv(3))/(1+m*rho^2/l);
%Tarray = [Tarray T];
Uarray = [Uarray mv];
xHistory(k+1,:) = XOUT(end,:);
%opt.Slack0
%T
%par = m*x0(1).*x0(4).^2-m*g*sin(x0(3))
% Save plant states
%xHistory = [xHistory x0];
end
function z = mystateFunctionREDUCED(x,u,rho,l,g,J,m)
z = zeros(4,1);
% rho = params(1);
% l = params(2);
% %T = params(3);
% g = params(3);
% J = params(4);
% m = params(5);
z(1) = x(2);
z(2) = (rho/l*u(3)+(rho^2/l)*(m*x(1)*x(4)^2-m*g*sin(x(3)+u(1)*sin(x(3)+u(2)))))/(1+rho^2*m/l);
z(3) = x(4);
z(4) = (-1/x(1))*(2*x(2)*x(4)+g*cos(x(3)))+1/(m*x(1))*u(1)*cos(x(3)+u(2));
end
function y = OutputFcn(x,u,rho,l,g,J,m)
y = zeros(2,1);
y(1) = x(1);
y(2) = x(3);
end
function f = statefcn(in1,in2)
%QuadrotorStateFcn
% F = QuadrotorStateFcn(IN1,IN2)
% This function was generated by the Symbolic Math Toolbox version 24.1.
% 09-Jan-2025 19:28:44
rho = 0.1;
l = 1/2*1*rho^2;
%T = params(3);
g = 9.81;
J = 0.015;
m = 2;
x = in1;
u = in2;
z(1) = x(2);
z(2) = (rho/l*u(3)+(rho^2/l)*(m*x(1)*x(4)^2-m*g*sin(x(3)+u(1)*sin(x(3)+u(2)))))/(1+rho^2*m/l);
z(3) = x(4);
z(4) = (-1/x(1))*(2*x(2)*x(4)+g*cos(x(3)))+1/(m*x(1))*u(1)*cos(x(3)+u(2));
f = [z(1);z(2);z(3);z(4)];
endI am getting the error flag -2 for my nonlinear mpc design despite their being no constraints. If there is no constraints, how is there infeasibility? The only way I can get it to be feasible is to make the intial condition the exact same as the reference. If there is deviation the problem is infeasible
clear all
nx = 4;
ny = 2;
nu = 3;
nlobj = nlmpc(nx,ny,nu);
nlobj.Ts = 0.1;
nlobj.PredictionHorizon = 10;
nlobj.ControlHorizon = 10;
nlobj.Model.StateFcn = @mystateFunctionREDUCED;
nlobj.Model.IsContinuousTime = true;
nlobj.Model.NumberOfParameters = 5;
rho = 0.1;
l = 1/2*1*rho^2;
%T = params(3);
g = 9.81;
J = 0.015;
m = 2;
nlobj.Model.OutputFcn = @OutputFcn
;
x0 = [1;0;pi/8;0];
mv = [m*g+1; 0; 0];
lastMV = mv;
%validateFcns(nlobj, x0, u0, [], {rho,l,g,J,m});
nloptions = nlmpcmoveopt;
nloptions.Parameters = {rho,l,g,J,m};
Duration = 1;
Ts = 0.01;
xHistory = x0′;
yref = [1;pi/9];
%nlobj.Weights.OutputVariables = [3 3];
%nlobj.Weights.ManipulatedVariablesRate = 0.1;
% nlobj.States(1).Min = 0;
% nlobj.States(1).Max = 20;
% nlobj.States(3).Min = 0;
% nlobj.States(3).Max = pi;
%nlobj.Optimization.CustomIneqConFcn = "myIneqConFunctionreduced";
nlobj.Weights.ManipulatedVariablesRate = [0 0 0];
nlobj.Weights.OutputVariables = [1 1];
%nlobj.Weights.ECR = 0;
Tarray = [];
Uarray = [];
for k = 1:(Duration/Ts)
xk = xHistory(k,:);
% Compute optimal control moves
[mv,nloptions,info] = nlmpcmove(nlobj,xk,lastMV,yref’,[],nloptions);
info
%disp(mv)
% Implement first optimal control move
ODEFUN = @(t,xk) statefcn(xk,mv);
[TOUT,XOUT] = ode45(ODEFUN,[0 Ts], xHistory(k,:)’);
x0 = mystateFunctionREDUCED(x0,mv,rho,l,g,J,m)*Ts+x0;
uHistory(k+1,:) = mv’;
lastMV = mv;
%[t,y] = ode15s(@mystateFunction,[0 Ts],[2 0]);
%T = (m*x0(1)*x0(4)^2-m*g*sin(x0(3))+mv(1)*sin(x0(3)+x0(5))-m*rho/l*mv(3))/(1+m*rho^2/l);
%Tarray = [Tarray T];
Uarray = [Uarray mv];
xHistory(k+1,:) = XOUT(end,:);
%opt.Slack0
%T
%par = m*x0(1).*x0(4).^2-m*g*sin(x0(3))
% Save plant states
%xHistory = [xHistory x0];
end
function z = mystateFunctionREDUCED(x,u,rho,l,g,J,m)
z = zeros(4,1);
% rho = params(1);
% l = params(2);
% %T = params(3);
% g = params(3);
% J = params(4);
% m = params(5);
z(1) = x(2);
z(2) = (rho/l*u(3)+(rho^2/l)*(m*x(1)*x(4)^2-m*g*sin(x(3)+u(1)*sin(x(3)+u(2)))))/(1+rho^2*m/l);
z(3) = x(4);
z(4) = (-1/x(1))*(2*x(2)*x(4)+g*cos(x(3)))+1/(m*x(1))*u(1)*cos(x(3)+u(2));
end
function y = OutputFcn(x,u,rho,l,g,J,m)
y = zeros(2,1);
y(1) = x(1);
y(2) = x(3);
end
function f = statefcn(in1,in2)
%QuadrotorStateFcn
% F = QuadrotorStateFcn(IN1,IN2)
% This function was generated by the Symbolic Math Toolbox version 24.1.
% 09-Jan-2025 19:28:44
rho = 0.1;
l = 1/2*1*rho^2;
%T = params(3);
g = 9.81;
J = 0.015;
m = 2;
x = in1;
u = in2;
z(1) = x(2);
z(2) = (rho/l*u(3)+(rho^2/l)*(m*x(1)*x(4)^2-m*g*sin(x(3)+u(1)*sin(x(3)+u(2)))))/(1+rho^2*m/l);
z(3) = x(4);
z(4) = (-1/x(1))*(2*x(2)*x(4)+g*cos(x(3)))+1/(m*x(1))*u(1)*cos(x(3)+u(2));
f = [z(1);z(2);z(3);z(4)];
end I am getting the error flag -2 for my nonlinear mpc design despite their being no constraints. If there is no constraints, how is there infeasibility? The only way I can get it to be feasible is to make the intial condition the exact same as the reference. If there is deviation the problem is infeasible
clear all
nx = 4;
ny = 2;
nu = 3;
nlobj = nlmpc(nx,ny,nu);
nlobj.Ts = 0.1;
nlobj.PredictionHorizon = 10;
nlobj.ControlHorizon = 10;
nlobj.Model.StateFcn = @mystateFunctionREDUCED;
nlobj.Model.IsContinuousTime = true;
nlobj.Model.NumberOfParameters = 5;
rho = 0.1;
l = 1/2*1*rho^2;
%T = params(3);
g = 9.81;
J = 0.015;
m = 2;
nlobj.Model.OutputFcn = @OutputFcn
;
x0 = [1;0;pi/8;0];
mv = [m*g+1; 0; 0];
lastMV = mv;
%validateFcns(nlobj, x0, u0, [], {rho,l,g,J,m});
nloptions = nlmpcmoveopt;
nloptions.Parameters = {rho,l,g,J,m};
Duration = 1;
Ts = 0.01;
xHistory = x0′;
yref = [1;pi/9];
%nlobj.Weights.OutputVariables = [3 3];
%nlobj.Weights.ManipulatedVariablesRate = 0.1;
% nlobj.States(1).Min = 0;
% nlobj.States(1).Max = 20;
% nlobj.States(3).Min = 0;
% nlobj.States(3).Max = pi;
%nlobj.Optimization.CustomIneqConFcn = "myIneqConFunctionreduced";
nlobj.Weights.ManipulatedVariablesRate = [0 0 0];
nlobj.Weights.OutputVariables = [1 1];
%nlobj.Weights.ECR = 0;
Tarray = [];
Uarray = [];
for k = 1:(Duration/Ts)
xk = xHistory(k,:);
% Compute optimal control moves
[mv,nloptions,info] = nlmpcmove(nlobj,xk,lastMV,yref’,[],nloptions);
info
%disp(mv)
% Implement first optimal control move
ODEFUN = @(t,xk) statefcn(xk,mv);
[TOUT,XOUT] = ode45(ODEFUN,[0 Ts], xHistory(k,:)’);
x0 = mystateFunctionREDUCED(x0,mv,rho,l,g,J,m)*Ts+x0;
uHistory(k+1,:) = mv’;
lastMV = mv;
%[t,y] = ode15s(@mystateFunction,[0 Ts],[2 0]);
%T = (m*x0(1)*x0(4)^2-m*g*sin(x0(3))+mv(1)*sin(x0(3)+x0(5))-m*rho/l*mv(3))/(1+m*rho^2/l);
%Tarray = [Tarray T];
Uarray = [Uarray mv];
xHistory(k+1,:) = XOUT(end,:);
%opt.Slack0
%T
%par = m*x0(1).*x0(4).^2-m*g*sin(x0(3))
% Save plant states
%xHistory = [xHistory x0];
end
function z = mystateFunctionREDUCED(x,u,rho,l,g,J,m)
z = zeros(4,1);
% rho = params(1);
% l = params(2);
% %T = params(3);
% g = params(3);
% J = params(4);
% m = params(5);
z(1) = x(2);
z(2) = (rho/l*u(3)+(rho^2/l)*(m*x(1)*x(4)^2-m*g*sin(x(3)+u(1)*sin(x(3)+u(2)))))/(1+rho^2*m/l);
z(3) = x(4);
z(4) = (-1/x(1))*(2*x(2)*x(4)+g*cos(x(3)))+1/(m*x(1))*u(1)*cos(x(3)+u(2));
end
function y = OutputFcn(x,u,rho,l,g,J,m)
y = zeros(2,1);
y(1) = x(1);
y(2) = x(3);
end
function f = statefcn(in1,in2)
%QuadrotorStateFcn
% F = QuadrotorStateFcn(IN1,IN2)
% This function was generated by the Symbolic Math Toolbox version 24.1.
% 09-Jan-2025 19:28:44
rho = 0.1;
l = 1/2*1*rho^2;
%T = params(3);
g = 9.81;
J = 0.015;
m = 2;
x = in1;
u = in2;
z(1) = x(2);
z(2) = (rho/l*u(3)+(rho^2/l)*(m*x(1)*x(4)^2-m*g*sin(x(3)+u(1)*sin(x(3)+u(2)))))/(1+rho^2*m/l);
z(3) = x(4);
z(4) = (-1/x(1))*(2*x(2)*x(4)+g*cos(x(3)))+1/(m*x(1))*u(1)*cos(x(3)+u(2));
f = [z(1);z(2);z(3);z(4)];
end control, mpc MATLAB Answers — New Questions