Is it possible to add variables to my nonlinear constraints if they are not in the objective function?

1 view (last 30 days)
I am using fmincon to minimize for my objective function:
objective = @(x) x(1)^2+x(2)^2;
But in my nonlinear constraints, I have three more variables: x(3), x(4) and x(5). I have four equations for coordinates (dependent on x(1),x(2),x(3),x(4) and x(5)), for which I think x(3), x(4) and x(5) can be calculated from. But as you can guess, I am not able to solve for my model so far. These are my nonlinear constraints:
function [c,ceq] = nlcon34(x)
%x=[wxo,wyo,vxo,vyo,vzo]
%x(1)=wxo x(2)=wyo x(3)=vxo x(4)=vyo x(5)=vzo
%x(6)=t, is nu (-vzo*ez +/- sqrt((vzo*ez)^2-2*g*Zg))/-g, kies de -, dan
%t= (vzo*ez + sqrt((vzo*ez)^2-2*g*Zg))/g
a = 0.83; % ball factor
R = 0.1155; %radius ball
g = 9.81; %gravity
ez = 0.84; % COR
LAT = 1.5; %hoogte locatie bovenlat (weet niet precies nog)
L = 0.75/2; %width goal, from middle
xbounce = 0.7; %place where ball bounces first, with respect to x-as from mid
ybounce = 9; % waar bal stuitert tov y-as vanaf midden. y=0 op middenstip. y=7.4 m op penaltystip. y=11 m op doellijn
zg = (0:0.1:(LAT-R)); % Is this possible?
t = (x(5)*ez+sqrt((x(5)*ez)^2-(2*g*zg)))./g; % Dots in right place?
xg = (x(3)+(a*R*x(2)))*t./(1+a) + 2*x(5)*x(3)/g;
yg = (x(4)+(a*R*x(1)))*t./(1+a) + 2*x(5)*x(4)/g;
xb = 2*x(5)*x(3)/g;
yb = 2*x(5)*x(4)/g;
vabs = sqrt(x(3)^2+x(4)^2+x(5)^2);
phi = atan2(x(5),sqrt(x(3)^2+x(4)^2));
c(1) = xg - L; %xg < L
c(2) = -(xg+L); %-L < xg
c(3) = vabs-12; % v < 12
c(4) = -vabs; % v > 0. Is this necessary? If no imaginary numbers it will always be positive I guess
c(5) = phi - pi/2; % phi < pi/2
c(6) = -phi; % phi > 0
c(7) = -t; % t > 0
ceq(1) = yg - 11 - 7.4; % yg = 11 m from middle, robot at 3.6 m from goal
ceq(2) = yb - ybounce - 7.4; % yb = ybounce from mid, robot at 3.6 m from goal, yb=ybounce-7.4 from robot
ceq(3) = xb - xbounce; % bounce location in x, xb = xbounce (assuming robot on same x-line)
pause(1)
end
And with the script below I calculate fmincon. This results in a great deal of errors. I'm new to using fmincon, so there might be some obvious things that I'm missing. I was wondering if one of the problems might be that I cannot solve for x(3), x(4) and x(5) because they are not in the objective function? I have checked the "
How do I pass additional parameters to the constraint and objective functions in the Optimization Toolbox functions'
file, but I'm still not sure. I am now using some nonlinear constraints, for which some not even contain x(1) and x(2), is this possible? If not, how else can I solve for those constraints?
clear all;close all; clc
objective = @(x) x(1)^2+x(2)^2;
% initial guess (just random for now)
x0 = [20,20];
%you might see that these values are also already mentioned in the nonlinear constraints function. I'm not sure where they should be.
a = 0.83; % ball factor
R = 0.1155; %radius bal
g = 9.81; %gravity
ez = 0.84; % COR
LAT = 1.5; %height
L = 0.75/2; %width half goal
xbounce = 0.7; %place where ball bounces first, with respect to x-as from mid
ybounce = 9; % waar bal stuitert tov y-as vanaf midden. y=0 op middenstip. y=7.4 m op penaltystip. y=11 m op doellijn
% variable bounds
lb = [];
ub = [];
% show initial objective
disp(['Initial Objective: ' num2str(objective(x0))])
% linear constraints
A = [];
b = [];
Aeq = [];
beq = [];
% nonlinear constraints
nonlincon = @nlcon34;
% optimize with fmincon
%[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]
% = fmincon(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS)
options = optimoptions('fmincon', 'Algorithm','sqp', 'MaxIterations', 500000,'ConstraintTolerance', 1e-5, 'OptimalityTolerance', 1e-6, 'StepTolerance', 1e-12, 'MaxFunctionEvaluations', 5000);
x = fmincon(objective,x0,A,b,Aeq,beq,lb,ub,nonlincon,options);
% show final objective
disp(['Final Objective: ' num2str(objective(x))])
% print solution
disp('Solution')
disp(['wxo = ' num2str(x(1))])
disp(['wyo = ' num2str(x(2))])
Any help is welcome. Thanks in advance!
  1 Comment
Matt J
Matt J on 29 Nov 2020
And with the script below I calculate fmincon. This results in a great deal of errors.
It's usually a good idea to copy/paste the errors for us, so we know what they are.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 29 Nov 2020
Edited: Matt J on 29 Nov 2020
Your initial guess x0=[20,20] contains only 2 elements but it should contain 5 because that is how many unknowns you have. If you make your x0 of length N, then fmincon will know your problem has N unknowns and will pass vectors of length N to the objective and the constraints as it does its iterative search. However, this does not mean that within the code for your objective or constraints you must use all N of the x(i).
  4 Comments
Isabelle
Isabelle on 29 Nov 2020
Edited: Isabelle on 29 Nov 2020
Hi Matt, I see that fmincon is ignoring my inequality constraints c(5) and c(6), might it be that there are no solutions for the constraining interval of phi, or is there a different problem? Changing the algorithm from 'sqp' to 'interior-point' does give an answer within the constraints of phi, but it now gives the error 'coverged to an infeasible point', and it changed the values of xb,yb,xg,yg,zg which I give as an input.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization 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!