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

25 views (last 30 days)
Vivek on 15 Nov 2022
Commented: Cris LaPierre on 31 Jan 2023
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);
Cris LaPierre on 15 Nov 2022
Edited: Cris LaPierre on 15 Nov 2022
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
Vivek on 19 Nov 2022
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.

Cris LaPierre on 19 Nov 2022
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
Cris LaPierre on 19 Dec 2022
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.
Vivek on 25 Dec 2022
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' .

Walter Roberson on 30 Nov 2022
Moved: Walter Roberson on 30 Nov 2022
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.
Walter Roberson on 30 Nov 2022
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
Vivek on 1 Dec 2022
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.

Vivek on 31 Jan 2023
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)
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
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!
Cris LaPierre on 31 Jan 2023

### Categories

Find more on Solver-Based Nonlinear Optimization in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!