I am getting the error as "Too many Input arguments", " Failure in Initial Objective function evaluation" .Also unable to run the function vivek1
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
Cris LaPierre
2022-11-15
编辑:Cris LaPierre
2022-11-15
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
采纳的回答
Cris LaPierre
2022-11-19
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
15 个评论
Cris LaPierre
2022-12-19
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.
更多回答(2 个)
Walter Roberson
2022-11-30
移动:Walter Roberson
2022-11-30
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 个评论
Walter Roberson
2022-11-30
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!