I am getting the error as "Too many Input arguments", " Failure in Initial Objective function evaluation" .Also unable to run the function vivek1
Mostrar comentarios más antiguos
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
Walter Roberson
el 15 de Nov. de 2022
I do not see a definition for v anywhere.
Cris LaPierre
el 15 de Nov. de 2022
Editada: Cris LaPierre
el 15 de Nov. de 2022
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.
Vivek
el 15 de Nov. de 2022
Editada: Walter Roberson
el 15 de Nov. de 2022
Walter Roberson
el 15 de Nov. de 2022
Is obliquerelations the function at https://www.mathworks.com/matlabcentral/fileexchange/28242-oblique-shock-relations-solver ?
Cris LaPierre
el 15 de Nov. de 2022
Editada: Cris LaPierre
el 15 de Nov. de 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
el 19 de Nov. de 2022
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 30 de Nov. de 2022
Movida: Walter Roberson
el 30 de Nov. de 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.
2 comentarios
Walter Roberson
el 30 de Nov. de 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
el 1 de Dic. de 2022
Vivek
el 31 de En. de 2023
1 comentario
Cris LaPierre
el 31 de En. de 2023
Please start a new question.
Categorías
Más información sobre Linear Least Squares en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
