I am getting the error as "Too many Input arguments", " Failure in Initial Objective function evaluation" .Also unable to run the function vivek1
18 views (last 30 days)
Show older comments
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);
Accepted Answer
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);
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
More Answers (2)
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!