I am getting the error as "Too many Input arguments", " Failure in Initial Objective function evaluation" .Also unable to run the function vivek1

I am getting the above said error....basically here I have written function to calculate the beta and mach, for every iteration externally. Then the function is being called into objective function evaluation.
Thank you.
function x = vivek1(mach,theta)
B=0.2;
gamma=1.4;
m1 = mach;
theta1 = theta*pi/180;
beta1 = obliquerelations('mach', m1, 'theta', theta1, 1.4)*180/pi;
%To find Mn1
Mn1= m1*sin(beta1*pi/180);
%To find Mn2
g=((1+(B*(Mn1^2)))/((gamma*(Mn1^2))-B));
G=sqrt(g);
%To find M2
M2=G/sin((beta1*pi/180)-(theta1));
U=sin(theta1);
end
m1=6.8;
%Obijective Function
f=@(x) (((1.2)^3)*((v(mach,x(1))*sin(x(1)))*(v(v(mach,x(1)),x(2))*sin(x(2)))*(v(v(v(mach,x(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^(3*1.4/(1.4-1))/((1+(0.2*((v(v(v(mach(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^2))*(1+(0.2*((v(v(mach,x(1)),x(2))*sin(x(2))))^2))*(1+((0.2)*((v(mach,x(1))*sin(x(1))))^2))^(3/(1-1.4));
%INitial condition
x0=[8,10];
%Lower Bounds
lb=[1,1];
%Upper Bounds
ub=[15,30];
A=[];
b=[];
%Equality Constraints
Aeq=[];
beq=[];
%Non-linear Constraints
nonlcon=[];
options = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter','Algorithm','sqp-legacy');
x=fminunc(f,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);

7 comentarios

I do not see a definition for v anywhere.
Unrecognized function or variable 'mach'
Unrecognized function or variable 'obliquerelations'
Output argument "x" (and possibly others) not assigned a value in the execution with "v" function.
From the below code, I am getting error "
Error in Eo_try>
f=@(x) (((1.2)^3)*((x(m1,x(1))*sin(x(1)))*(v(v(m1,x(1)),x(2))*sin(x(2)))*(v(v(v(m1,x(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^((3*gamma)/0.4)/((1+(0.2*((v(v(v(m1(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^2))*(1+(0.2*((v(v(m1,x(1)),x(2))*sin(x(2))))^2))*(1+((0.2)*((v(m1,x(1))*sin(x(1))))^2))^(3/(1-gamma));
Error in fmincon (line 568)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in Eo_try (line 75)
fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon)
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
%Objective Function
f=@(x) (((1.2)^3)*((x(m1,x(1))*sin(x(1)))*(v(v(m1,x(1)),x(2))*sin(x(2)))*(v(v(v(m1,x(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^((3*gamma)/0.4)/((1+(0.2*((v(v(v(m1(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^2))*(1+(0.2*((v(v(m1,x(1)),x(2))*sin(x(2))))^2))*(1+((0.2)*((v(m1,x(1))*sin(x(1))))^2))^(3/(1-gamma));
%INitial condition
x0=[8,10];
%Lower Bounds
lb=[1,1];
%Upper Bounds
ub=[15,30];
A=[];
b=[];
%Equality Constraints
Aeq=[];
beq=[];
%Non-linear Constraints
nonlcon=[];
options = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter','Algorithm','trust-region-reflective','SpecifyObjectiveGradient',true,'BarrierParamUpdate','predictor-corrector');
fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
m1=6.8;
function x = v(mach,theta)
B=0.2;
gamma=1.4;
m1 = mach;
theta1 = theta*pi/180;
beta1 = obliquerelations('mach', m1, 'theta', theta1, 1.4)*180/pi;
%To find Mn1
Mn1= m1*sin(beta1*pi/180);
%To find Mn2
g=((1+(B*(Mn1^2)))/((gamma*(Mn1^2))-B));
G=sqrt(g);
%To find M2
M2=G/sin((beta1*pi/180)-(theta1));
% U=sin(theta1);
end
Your function "v" does not return the desired "x".
I also found an error in one of your nested calls to v in the definition for f. In one of them, you do not supply a second input, so you get the opposite error message of "Not enough inputs"
f=@(x) (((1.2)^3)*((x(m1,x(1))*sin(x(1)))*(v(v(m1,x(1)),x(2))*sin(x(2)))*(v(v(v(m1,x(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^((3*gamma)/0.4)/...
((1+(0.2*((v(v(v(m1(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^2))*(1+(0.2*((v(v(m1,x(1)),x(2))*sin(x(2))))^2))*(1+((0.2)*((v(m1,x(1))*sin(x(1))))^2))^(3/(1-gamma));
% ^^^^^^^^ -> No theta input in this call to v
obliqueshockrelations is the external script function https://www.mathworks.com/matlabcentral/fileexchange/28242-oblique-shock-relations-solver by David Padgget.
As x0 is the Initial Conditions array, x(1) was initially I had given it as x0(1) thinking that, that value has to be taken from the Initial condition array. But it doesn't work like that, so I changed it to x(1).
Aim of the Algorithm :
1) There is external obliqueshockrelations solver function file
2) I have used fminunc or fmincon to optimise the objective function f(x).
3) I want the code to evaluate the external function for every iterarion as the m1 had to be updated for evry iteration.

Iniciar sesión para comentar.

 Respuesta aceptada

Using the following syntax (from your original post) will result in a "Too many input arguemnts" error because, well, there are too many input arguments.
  • x=fminunc(f,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
It is unconstrained, so there are no inputs for "A,b,Aeq,beq,lb,ub,nonlcon". The valid syntax is
If you switch to using fmincon then that error goes away. However, you still have the problem that your function v does not assign anything to its output variable, x. Even if I assume it is supposed to be U, you then run into the 'Not enough input arguments' I mentioned previously.
m1=6.8;
%Obijective Function
f=@(x) (((1.2)^3)*((v(m1,x(1))*sin(x(1)))*...
(v(v(m1,x(1)),x(2))*sin(x(2)))*...
(v(v(v(m1,x(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^(3*1.4/(1.4-1))/...
((1+(0.2*((v(v(v(m1(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^2))*... % v(m1(1)) is causing error
(1+(0.2*((v(v(m1,x(1)),x(2))*sin(x(2))))^2))*...
(1+((0.2)*((v(m1,x(1))*sin(x(1))))^2))^(3/(1-1.4));
%INitial condition
x0=[8,10];
%Lower Bounds
lb=[1,1];
%Upper Bounds
ub=[15,30];
A=[];
b=[];
%Equality Constraints
Aeq=[];
beq=[];
%Non-linear Constraints
nonlcon=[];
options = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter','Algorithm','sqp-legacy');
x=fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
Not enough input arguments.

Error in solution>v (line 33)
theta1 = theta*pi/180;

Error in solution (line 6)
((1+(0.2*((v(v(v(m1(1)),x(2)),(x(1) + x(2)))*(sin(x(2))^2))))^2))*... % v(m1(1)) is causing error

Error in fmincon (line 568)
initVals.f = feval(funfcn{3},X,varargin{:});

Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
function x = v(mach,theta)
B=0.2;
gamma=1.4;
theta1 = theta*pi/180;
beta1 = obliquerelations('mach', mach, 'theta', theta1, 1.4)*180/pi;
%To find Mn1
Mn1= mach*sin(beta1*pi/180);
%To find Mn2
g=((1+(B*(Mn1^2)))/((gamma*(Mn1^2))-B));
G=sqrt(g);
%To find M2
M2=G/sin((beta1*pi/180)-(theta1));
U=sin(theta1);
x=U; % Need to assign something to output variable 'x'
end

15 comentarios

The function is not taking the values from 'x0' and the function 'v'.
Is the right way to call the function 'v' into objective function?
Please help me to sort the issue.
Thank you
m1 = 6.8;
% f=@(x) (((1.2)^3)*((g(m1,x0(1))*sin(x0(1)))*(g(g(m1,x0(1)),x0(2))*sin(x0(2)))*(g(g(g(m1,x0(1)),x0(2)),(x0(1) + x0(2)))*(sin(x0(2))^2))))^((3*gamma)/0.4)/((1+(0.2*((g(g(g(m1,x0(2)),(x0(1) + x0(2))))*(sin(x0(2))^2))))^2))*(1+(0.2*((g(g(m1,x0(1)),x0(2))*sin(x0(2))))^2))*(1+((0.2)*((g(m1,x0(1))*sin(x0(1))))^2))^(3/(1-gamma));
f=@(x) (((1.2)^3)*((v(m1,x0(1))*sin(x0(1)))*(v(v(m1,x0(1)),x0(2))*sin(x0(2)))*(v(v(v(m1,x0(1)),x0(2)),(x0(1) + x0(2)))*(sin(x0(2))^2))))^((3*gamma)/0.4)/((1+(0.2*((v(v(v(m1,x0(2)),(x0(1) + x0(2))))*(sin(x0(2))^2))))^2))*(1+(0.2*((v(v(m1,x0(1)),x0(2))*sin(x0(2))))^2))*(1+((0.2)*((v(m1,x0(1))*sin(x0(1))))^2))^(3/(1-gamma));
%INitial condition
x0=[(8*pi/180),(10*pi/180)];
%Lower Boundslb
lb=[1,1];
%Upper Bounds
ub=[15,30];
% A=[];
% b=[];
%Equality Constraints
% Aeq=[];
% beq=[];
%Non-linear Constraints
% nonlcon=[];
oldoptions = optimoptions(@fmincon,'Algorithm','sqp','MaxIterations',1500);
options = optimoptions(@fminunc,oldoptions);
% options = optimoptions('fminunc','SpecifyObjectiveGradient',true);
% options = optimoptions('fminunc','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter','Algorithm','trust-region','SpecifyObjectiveGradient',true);
fminunc(f,x0,options)
Unrecognized function or variable 'x0'.

Error in solution (line 4)
f=@(x) (((1.2)^3)*((v(m1,x0(1))*sin(x0(1)))*(v(v(m1,x0(1)),x0(2))*sin(x0(2)))*(v(v(v(m1,x0(1)),x0(2)),(x0(1) + x0(2)))*(sin(x0(2))^2))))^((3*gamma)/0.4)/((1+(0.2*((v(v(v(m1,x0(2)),(x0(1) + x0(2))))*(sin(x0(2))^2))))^2))*(1+(0.2*((v(v(m1,x0(1)),x0(2))*sin(x0(2))))^2))*(1+((0.2)*((v(m1,x0(1))*sin(x0(1))))^2))^(3/(1-gamma));

Error in fminunc (line 307)
f = feval(funfcn{3},x,varargin{:});
%x=fminunc(f,x0,options)
% ga(f,2,lb,ub)
% x=fminunc(f,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
%problem=optimproblem
%problem.Objective=(0.036*x(1)^0.758)*(x(2)^0.0035)*((x(3)/cos(x(4))^2)^0.6)*((x(5))^0.006)*(x(6)^0.04)*(((100*x(7))/x(4))^(-0.3))*((x(8)*x(9))^0.49);
%options = optimoptions("fmincon","PlotFcn",["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"]);
%[solution,objectiveValue,reasonSolverStopped] = solve(problem,initialPoint,"Solver","fmincon","Options",options);
function x = v(mach,theta)
B=0.2;
gamma=1.4;
m1 = mach;
theta1 = theta*pi/180;
beta1 = obliquerelations('mach', m1, 'theta', theta1, 1.4)*180/pi;
%To find Mn1
Mn1= m1*sin(beta1*pi/180);
%To find Mn2
g=((1+(B*(Mn1^2)))/((gamma*(Mn1^2))-B));
G=sqrt(g);
%To find M2
x=G/sin((beta1*pi/180)-(theta1));
% U=sin(theta1);
end
Read the full error message. The problem is in your definition of f.
Error in solution (line 4)
f=@(x) (((1.2)^3)*((v(m1,x0(1))*sin(x0(1)))*(v(v(m1,x0(1)),x0(2))*sin(x0(2)))*(v(v(v(m1,x0(1)),x0(2)),(x0(1) + x0(2)))*(sin(x0(2))^2))))^((3*gamma)/0.4)/((1+(0.2*((v(v(v(m1,x0(2)),(x0(1) + x0(2))))*(sin(x0(2))^2))))^2))*(1+(0.2*((v(v(m1,x0(1)),x0(2))*sin(x0(2))))^2))*(1+((0.2)*((v(m1,x0(1))*sin(x0(1))))^2))^(3/(1-gamma));
You define the input variable to be x, but then use x0 in the equation. So inside the anonymous function f the variable x0 is not defined. Fix that, and we return to the error I've mentioned a couple times already - 'Not enough input arguments.'
m1 = 6.8;
f=@(x0) (((1.2)^3)*((v(m1,x0(1))*sin(x0(1)))*(v(v(m1,x0(1)),x0(2))*sin(x0(2)))*(v(v(v(m1,x0(1)),x0(2)),(x0(1) + x0(2)))*(sin(x0(2))^2))))^((3*gamma)/0.4)/((1+(0.2*((v(v(v(m1,x0(2)),(x0(1) + x0(2))))*(sin(x0(2))^2))))^2))*(1+(0.2*((v(v(m1,x0(1)),x0(2))*sin(x0(2))))^2))*(1+((0.2)*((v(m1,x0(1))*sin(x0(1))))^2))^(3/(1-gamma));
%INitial condition
x0=[(8*pi/180),(10*pi/180)];
options = optimoptions(@fminunc,'MaxIterations',1500);
fminunc(f,x0,options)
Error using gamma
Not enough input arguments.

Error in solution (line 2)
f=@(x0) (((1.2)^3)*((v(m1,x0(1))*sin(x0(1)))*(v(v(m1,x0(1)),x0(2))*sin(x0(2)))*(v(v(v(m1,x0(1)),x0(2)),(x0(1) + x0(2)))*(sin(x0(2))^2))))^((3*gamma)/0.4)/((1+(0.2*((v(v(v(m1,x0(2)),(x0(1) + x0(2))))*(sin(x0(2))^2))))^2))*(1+(0.2*((v(v(m1,x0(1)),x0(2))*sin(x0(2))))^2))*(1+((0.2)*((v(m1,x0(1))*sin(x0(1))))^2))^(3/(1-gamma));

Error in fminunc (line 307)
f = feval(funfcn{3},x,varargin{:});

Caused by:
Failure in initial objective function evaluation. FMINUNC cannot continue.
function x = v(mach,theta)
B=0.2;
gamma=1.4;
m1 = mach;
theta1 = theta*pi/180;
beta1 = obliquerelations('mach', m1, 'theta', theta1, 1.4)*180/pi;
%To find Mn1
Mn1= m1*sin(beta1*pi/180);
%To find Mn2
g=((1+(B*(Mn1^2)))/((gamma*(Mn1^2))-B));
G=sqrt(g);
%To find M2
x=G/sin((beta1*pi/180)-(theta1));
end
I have changed the syntax (broke the objective function) which made it clear to debug.
Now the code is running properly also it is calling values properly from the function 'v' (geven at the last).
I have attached the external functions used by function 'v'.
The problem is , it is running in infinite loop, after stopping saying 'out of memory'.
function y=f(x0)
x0=[8,10];
gamma=1.4;
m1 = 6.8;
a= v(m1,x0(1)); %M1
b= v(a,x0(2)); %M2
c= v(b,(x0(1)+x0(2))); %M3
% d= v(a,(x0(1,1)+x0(1;,2)));
% e= b*sin(x0(1,2));
s1=sin(x0(1)*pi/180); %theta1
s2=sin(x0(2)*pi/180); %theta2
s3=sin((x0(1)+x0(2))*(pi/180)) %theta3
n=(((1.728)*((a*s1)*(b*s2)*(c*s3))^2)^(10.5));
y= n/((1+(0.2*(c*s3)^2))*(1+(0.2*(b*s2)^2))*(1+(0.2*(a*s1)^2)))^(-7.5)
lb=[1,2];
%Upper Bounds
ub=[15,50];
Aineq=[];
bineq=[];
%Equality Constraints
Aeq=[];
beq=[];
%Non-linear Constraints
nonlcon=[];
%
% opts = optimoptions('gamultiobj','PlotFcn', @gaplotbestf,'MaxGenerations',2)
% opts = optimoptions(opts,'MaxIterations',1e4); % Recommended
% [x,fval,output,population,scores,exitflag]= gamultiobj(@f,2,Aineq,bineq,Aeq,beq,lb,ub,nonlcon,options)
%
%
opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter','Algorithm','trust-region-reflective','SpecifyObjectiveGradient',true);
opts1 = optimoptions(opts,'MaxIterations',10) % Recommended
% options=optimoptions('fmincon','PlotFcn','Display','Algorithm','sqp');
[x,fval,exitflag,output]= fmincon(f,x0,Aeq,beq,lb,ub,nonlcon,opts1);
end
function M2 = v(mach,theta)
B=0.2;
gamma=1.4;
% m1 = mach;
m1=mach;
theta1 = theta*pi/180;
beta1 = obliquerelations('mach', m1, 'theta', theta1, 1.4)*180/pi;
%To find Mn1
Mn1= m1*sin(beta1*pi/180);
%To find Mn2
g=((1+(B*(Mn1^2)))/((gamma*(Mn1^2))-B));
G=sqrt(g);
%To find M2
M2=G/sin((beta1*pi/180)-(theta1));
end
I'm not sure how you are getting your code to run infinitely, as I can't get it to run at all.
I am observing that you have not supplied enough inputs to fmincon. The syntax should be
You are missing the inputa A and b. Perhaps you meant to do the following?
[x,fval,exitflag,output] = fmincon(f,x0,Aineq,bineq,Aeq,beq,lb,ub,nonlcon,opts1);
Vivek
Vivek el 30 de Nov. de 2022
Editada: Vivek el 30 de Nov. de 2022
As I dont have the constraints, I have kept them empty
Even if I use the format x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) it will not give answer rather will run in infinite loop
It will take the empty arrays.
The code you are working with appears to be different from the code you have shared here. I currently get that there are too many output arguments. When I track that down, it is because your option SpecifyObjectiveGradient is true.
Removing that and fixing the issues already discussed, plus some new ones with how you have shared your code, it now runs. Don't know if the resutls are correct, though.
x0=[8,10];
lb=[1,2];
ub=[15,50];
opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter');
opts1 = optimoptions(opts,'MaxIterations',10) % Recommended
opts1 =
fmincon options: Options used by current Algorithm ('interior-point'): (Other available algorithms: 'active-set', 'sqp', 'sqp-legacy', 'trust-region-reflective') Set properties: Display: 'iter' MaxIterations: 10 PlotFcn: {'optimplotx' 'optimplotfunccount' 'optimplotfvalconstr' 'optimplotfval'} Default properties: Algorithm: 'interior-point' BarrierParamUpdate: 'monotone' CheckGradients: 0 ConstraintTolerance: 1.0000e-06 EnableFeasibilityMode: 0 FiniteDifferenceStepSize: 'sqrt(eps)' FiniteDifferenceType: 'forward' HessianApproximation: 'bfgs' HessianFcn: [] HessianMultiplyFcn: [] HonorBounds: 1 MaxFunctionEvaluations: 3000 ObjectiveLimit: -1.0000e+20 OptimalityTolerance: 1.0000e-06 OutputFcn: [] ScaleProblem: 0 SpecifyConstraintGradient: 0 SpecifyObjectiveGradient: 0 StepTolerance: 1.0000e-10 SubproblemAlgorithm: 'factorization' TypicalX: 'ones(numberOfVariables,1)' UseParallel: 0 Show options not used by current Algorithm ('interior-point')
% options=optimoptions('fmincon','PlotFcn','Display','Algorithm','sqp');
[x,fval,exitflag,output]= fmincon(@f,x0,[],[],[],[],lb,ub,[],opts1);
First-order Norm of Iter F-count f(x) Feasibility optimality step 0 3 2.562179e-03 0.000e+00 4.440e-03 1 6 2.524365e-03 0.000e+00 4.282e-03 6.070e-03 2 9 2.341727e-03 0.000e+00 3.980e-03 3.056e-02 3 12 1.641003e-03 0.000e+00 2.804e-03 1.424e-01 4 15 6.447514e-04 0.000e+00 1.110e-03 3.573e-01 5 18 3.287750e-04 0.000e+00 6.375e-04 2.440e-01 6 21 1.506279e-04 0.000e+00 2.971e-04 2.702e-01 7 24 7.343832e-05 0.000e+00 1.548e-04 2.377e-01 8 27 3.403503e-05 0.000e+00 7.791e-05 2.439e-01 9 30 1.609576e-05 0.000e+00 3.772e-05 2.279e-01 10 33 7.597320e-06 0.000e+00 1.776e-05 2.198e-01
Solver stopped prematurely. fmincon stopped because it exceeded the iteration limit, options.MaxIterations = 1.000000e+01.
function y=f(x0)
m1 = 6.8;
a= v(m1,x0(1)); %M1
b= v(a,x0(2)); %M2
c= v(b,(x0(1)+x0(2))); %M3
% d= v(a,(x0(1,1)+x0(1;,2)));
% e= b*sin(x0(1,2));
s1=sin(x0(1)*pi/180); %theta1
s2=sin(x0(2)*pi/180); %theta2
s3=sin((x0(1)+x0(2))*(pi/180)); %theta3
n=(((1.728)*((a*s1)*(b*s2)*(c*s3))^2)^(10.5));
y= n/((1+(0.2*(c*s3)^2))*(1+(0.2*(b*s2)^2))*(1+(0.2*(a*s1)^2)))^(-7.5);
end
function M2 = v(mach,theta)
B=0.2;
gamma=1.4;
% m1 = mach;
m1=mach;
theta1 = theta*pi/180;
beta1 = obliquerelations('mach', m1, 'theta', theta1, 1.4)*180/pi;
%To find Mn1
Mn1= m1*sin(beta1*pi/180);
%To find Mn2
g=((1+(B*(Mn1^2)))/((gamma*(Mn1^2))-B));
G=sqrt(g);
%To find M2
M2=G/sin((beta1*pi/180)-(theta1));
end
If you do want to use a gradient, then you must modify your objective function to first compute the gradient, and then return the gradient vector, g(x), as a second output. See the following sections from the fmincon documentation page:
Sir, I changed the command as you said still its the same(running infinitely) -(code is shown below).
** I also tried to make a separate function file for the objective function 'f' and run. It is runnning for only 1 iteration and it is converging at the initial point saying that it is the best point as the OF is non-decreasing in feasible directions
function y=f(x0)
m1 = 6.8;
x0=[8,10];
a= v(m1,x0(1)); %M1
b= v(a,x0(2)); %M2
c= v(b,(x0(1)+x0(2))); %M3
% d= v(a,(x0(1,1)+x0(1;,2)));
% e= b*sin(x0(1,2));
s1=sin(x0(1)*pi/180); %theta1
s2=sin(x0(2)*pi/180); %theta2
s3=sin((x0(1)+x0(2))*(pi/180)); %theta3
n=(((1.728)*((a*s1)*(b*s2)*(c*s3))^2)^(10.5));
y= n/((1+(0.2*(c*s3)^2))*(1+(0.2*(b*s2)^2))*(1+(0.2*(a*s1)^2)))^(-7.5);
lb=[1,2];
ub=[15,50];
opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter');
opts1 = optimoptions(opts,'MaxIterations',10) % Recommended
% options=optimoptions('fmincon','PlotFcn','Display','Algorithm','sqp');
[x,fval,exitflag,output]= fmincon(@f,x0,[],[],[],[],lb,ub,[],opts1);
end
function M2 = v(mach,theta)
B=0.2;
gamma=1.4;
% m1 = mach;
m1=mach;
theta1 = theta*pi/180;
beta1 = obliquerelations('mach', m1, 'theta', theta1, 1.4)*180/pi;
%To find Mn1
Mn1= m1*sin(beta1*pi/180);
%To find Mn2
g=((1+(B*(Mn1^2)))/((gamma*(Mn1^2))-B));
G=sqrt(g);
%To find M2
M2=G/sin((beta1*pi/180)-(theta1));
end
Your code to running infinitely because you have created a recursive function, f. That means the function calls itself. This occurs because your fmincon code is calling the function f, which then causes a 2nd call to fmincon, etc in an infinite loop. If you notice in the output, you are getting the contents of variable opts1 printed many times, but no outputs from fmincon, even through you have set 'Display' to 'iter'.
Please look at the code I shared, which runs and produces a result, and compare it to the code you have, and see what the differences are.
Thank you so much for the help sir!!
Its working now.
Thank you so much @Cris LaPierre @Walter Roberson @Torsten for responding and helping me to solve the problem.
I have done changes suggested and it is working well now.
Further I am trying for the constrained Optimisation. Here, I want to give the constraint as shown below. In Mathwork documentation it is mentioned in terms of defining the array for constraint and I donno wether the double equality constraint will work in that way or not.
M*sin(beta1)==M2*sin(beta2)==M3*sin(beta3);
This is the condition to get maximum pressure recovry at the end of isloator section which is having double ramp inlet.
It will do something. It is very likely it will not do what you expect. To check 2 conditions, use &
M*sin(beta1)==M2*sin(beta2) & M2*sin(beta2)==M3*sin(beta3);
Otherwise, it will check the first comparison, then take the result of that (a logical 0 or 1) and compare it to the second. Here are some examples
% first checks if 2==2, then if result (1) == 1
2==2==1
ans = logical
1
1==2==0
ans = logical
1
For optimisation the command used is as shown below
options=optimoptions('ga','ConstraintTolerance',1e-3,'Display','iter')
[x,fval,exitflag,output,population,scores]=ga(@f,2,[],[],[],[],lb,ub,[],[],options)
Here the constraint M*sin(beta1)==M2*sin(beta2)==M3*sin(beta3) has to be given in the below shown format.
I have created the separate function for non linear constraint i.e., ceq=[ ] and given the constraint as you mentioned M*sin(beta1)==M2*sin(beta2) & M2*sin(beta2)==M3*sin(beta3) as shown.
All the necessary functions needed to calculate the other parameters like (M2,M3,beta1,beta2,beta3)are coded in different function files.
function [ceq] = area(M)
%M=5;
M*sin(beta1)==M2*sin(beta2);
M2*sin(beta2)==M3*sin(beta3);
ceq = [ ];
end
But while running its showing error as
Error using area
Too many output arguments.
Error in fmincon (line 655)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in g (line 17)
[x,fval,exitflag,output,lambda,grad,hessian]= fmincon(@f,x0,[],[],[],[],lb,ub,nonlcon,opts1)
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
Below is the code used to run the optimisation.
%Initial Condition
x0=[8,10];
% Lower bounds
lb=[5,8];
% Upper Bounds
ub=[15,50];
nonlcon=@area;
% Aeq = [M*s1 == M2*s2 == M3*s3];
%
opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter');
opts1 = optimoptions(opts,'MaxIterations',100); % Recommended
[x,fval,exitflag,output,lambda,grad,hessian]= fmincon(@f,x0,[],[],[],[],lb,ub,nonlcon,opts1)
Below shown is the format for writing nonlcon (non-linear constraint) by Mathworks.
Nonlinear constraints, specified as a function handle or function name. nonlcon is a function that accepts a vector or array x and returns two arrays, c(x) and ceq(x).
c(x) is the array of nonlinear inequality constraints at x. fmincon attempts to satisfyc(x) <= 0 for all entries of c.
ceq(x) is the array of nonlinear equality constraints at x. fmincon attempts to satisfyceq(x) = 0 for all entries of ceq.
For example,
x = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon)
where mycon is a MATLAB function such as
function [c,ceq] = mycon(x)
c = ... % Compute nonlinear inequalities at x.
ceq = ... % Compute nonlinear equalities at x.
If the gradients of the constraints can also be computed and the SpecifyConstraintGradient option is true, as set by
options = optimoptions('fmincon','SpecifyConstraintGradient',true)
You need to model your nonlcon function after the mycon(x) example you shared from the doc.
  • It needs to have 2 outputs. Your area function only has one. This is causing the error message
  • Once you fix that, you will get an error that beta1-3 don't exist because they have not been definined inside the function.
  • Your logical comparisons serve no purpose currently since the results are not assigned to anything.
function [c,ceq] = area(M)
M=5;
x0=[8 10]
M2=m(M,x0(1));
M3=m(M2,x0(2));
% M4=m(M3,(x0(1)+x0(2)));
beta1=b(M,(x0(1)));
beta2=b(M2,(x0(2)));
beta3=b(M3,((x0(1)+x0(2))));
M*sin(beta1)==M2*sin(beta2) & M2*sin(beta2)==M3*sin(beta3)
c=[];
ceq = [ ];
end
This code is running and even the area function is assigned to nonlcon=[ ] function. After running the code, it is converging at the same older point (i.e., at the lower bounds) when there were no constraints given.
Also it is showing logical '0' .

Iniciar sesión para comentar.

Más respuestas (2)

function y=f(x0)
You are defining a function named f that expects zero or one parameter
x0=[8,10];
any supplied parameter is ignored and 8 10 is used instead
[x,fval,exitflag,output]= fmincon(f,x0,Aeq,beq,lb,ub,nonlcon,opts1);
The current function, f, wants to call fmincon. The first parameter to fmincon needs to be a function handle, possibly to an anonymous function.
MATLAB evaluates all parameters before calling the function. MATLAB does not say "oh, this is fmincon, interpret the first parameter as a function name." Always in MATLAB, parameters are evaluated before the function is called, and in the case of fmincon, one of the first things that fmincon will do with the received first parameter is to check that it is a function handle. But that is not even begun until the first parameter is finished evaluating.
What is the first parameter in the call? It is f which is the name of a function (not a variable that holds a function handle.) In MATLAB when you name a function with no @ before it and no () after it, that is treated as a request to evaluate the function with no parameters, same as if you had coded f() at that point. So the function f will be executed with no parameters and it needs to return a function handle.
But f is the very function you are defining. And that function unconditionally executes the fmincon call. Which requires that f be executed and returns a function handle. But f is the very function you are defining and it unconditionally tries to call fmincon passing the value of the function f as the first parameter... and so on. It never stops.

2 comentarios

your function f must not ignore its input x0. And it needs to end after the assignment to y. You can take that section of code through to the assignment to y, and write it to f.m. Then change the fmincon left behind in the other file so that the first parameter is @f instead of f
Thank you for the response sir!
"Then change the fmincon left behind in the other file so that the first parameter is @f instead of f"
As you said I tried making a separate function file for objective function and running it. It is running only for 1 iteration and converging at the initial search point saying that it is the best point as the OF is non-decreasing in feasible directions.

Iniciar sesión para comentar.

I want to add more than two constraints which are linear, nonlinear and with both equality and inequality signs. As per the MATLAB documentation the examples shown are mostly of same variables which are written as array Examples.
I am using the following shown constraint for the optimisation. The constraint function is named as "area", this same function is called for the 'nonlcon' (default function for constraints).
#1 The equations commented are also to be considered as Constraints for the Optimisation. Do MATLAB optimisation considers this kind of constraint equations?
#2 How to check whether all constraints are satisfied.
function [c,ceq]= area(x0)
M=5;
x0=[5,8];
M2=m(M,x0(1));
M3=m(M2,x0(2));
M4=m(M3,(x0(1)+x0(2)));
beta1=b(M,(x0(1)));
beta2=b(M2,(x0(2)));
beta3=b(M3,((x0(1)+x0(2))));
% M*sin(beta1)==M2*sin(beta2) & M2*sin(beta2)==M3*sin(beta3)
% (M4/M) <= 0.38
% beta1,beta2,beta3 < 62 degrees (This can be written in terms of Thetas)
% Some more constraints will be added
c=[];
ceq = log(2.4/((2.8*M*(sin(beta1)^2))-0.4)) + log(2.4/((2.8*M2*(sin(beta2)^2))-0.4)) + log(P(x0))
end
Below code shows the use of constraint function 'area' in the optimisation algorithm as "nonlcon".
%Initial Condition
% x0=[5,5] ;
% Lower bounds
lb=[1,1];
% Upper Bounds
ub=[30,40];
%constraint
nonlcon=@area
opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter')
opts1 = optimoptions(opts,'MaxIterations',50,'StepTolerance',1e-9,'ConstraintTolerance',1e-9)
[x,fval,exitflag,output,lambda,grad,hessian]= fmincon(@f,x0,[],[],[],[],lb,ub,nonlcon,opts1)
The optimisation is stopped between saying the below shown statement and then after enabling Feasibility Mode also the error is shown which is also mentioned in the end.
Converged to an infeasible point.
fmincon stopped because the size of the current step is less than
the value of the step size tolerance but constraints are not
satisfied to within the value of the constraint tolerance.
<stopping criteria details>
Consider enabling the interior point method feasibility mode.
%Initial Condition
x0=[5,5] ;
% Lower bounds
lb=[1,1];
% Upper Bounds
ub=[30,40];
%constraint
nonlcon=@area
opts = optimoptions('fmincon','PlotFcn',["optimplotx","optimplotfunccount","optimplotfvalconstr","optimplotfval"],'Display','iter', ...
'Algorithm','interior-point','EnableFeasibilityMode',true)
opts1 = optimoptions(opts,'MaxIterations',50,'StepTolerance',1e-9,'ConstraintTolerance',1e-9); % Recommended
[x,fval,exitflag,output,lambda,grad,hessian]= fmincon(@f,x0,[],[],[],[],lb,ub,nonlcon,opts1);
Converged to an infeasible point.
fmincon stopped because it is unable to find a point locally that satisfies
the constraints within the value of the constraint tolerance.
<stopping criteria details>
Even after changing the Constraint Tolerance it is not helping. Tried in the range 1e-9 to 1e-3
Thank you!

Productos

Versión

R2022a

Preguntada:

el 15 de Nov. de 2022

Comentada:

el 31 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by