Failure in initial objective function evaluation. FMINCON cannot continue.

54 visualizaciones (últimos 30 días)
Hello there, I have tried to look at the previous answers to this error, but I was still not able to resolve it with the advice given. I am trying to do a design optimization problem, but this is the error it returns. My code is shown below:
%% Parameters
l_0 = 3;
R = 5;
B = 4;
% fun = @(a,b) (8/((l_i)^2))*b*(a-1)*F*(l_i)^3;
a = 0.5;
b = 1;
C = [];
D = [];
% n=20;
LB = [0,0];
UB = [1,2];
x0 = (LB+UB)/2;
r=0.15*R:0.0425*R:R;
%% objective
nvars = 2;
LB = [4 5];
UB = [5 6];
aoa = 15; %need to make a matrix out of this for the baseline blade
x = [4 5];
%% Loop
for i = 1:length(r)
l_i = (l_0 * r(i)) / R;
% disp(l_i)
phi = atan(((1 - a) / (1 + b)) * (1/l_i(i)));
% disp(phi)
f = B / 2 * ((R - r(i)) / (R * sin(phi(i))));
% disp(f)
F = 2 / pi * acos(exp(1).^(-f(i)));
% disp(F)
twist_angle = phi(i) - aoa(i);
% disp(twist_angle)
obj = @(x)(8/((l_i).^2))*x(2)*(x(1)-1)*F*(l_i).^3;
% disp(obj)
[x,fvals] = fmincon(obj,nvars,[],[],[],[],LB,UB,@constraint);
end
with the constraint function being
function[c,ceq] = constraint(x)
c = (x(1)^2) * F + x(2) * l_i.^2 - x(2) * x(1) * l_i.^2 - x(1);
ceq = [];
end
-------------------------------------------------------------------------------------------------------------------------
and the error returned:
------------------------------------------------------------------------------------------------------------------------
  1 comentario
John D'Errico
John D'Errico el 16 de Mzo. de 2022
I closed your exact duplicate question, since it adds nothing to the problem, and since I gave you a complete answer to your problem here.

Iniciar sesión para comentar.

Respuesta aceptada

John D'Errico
John D'Errico el 16 de Mzo. de 2022
Editada: John D'Errico el 16 de Mzo. de 2022
You need to read the examples. Look at the help for fmincon. Why?
What is nvars?
nvars = 2;
nvars is the scalar 2. I assume you think that means the NUMBER of variables.
How do you use nvars?
[x,fvals] = fmincon(obj,nvars,[],[],[],[],LB,UB,@constraint);
As the second argument to fmincon.
However, READ THE HELP FOR FMINCON! LOOK AT THE EXAMPLES.
help fmincon
FMINCON finds a constrained minimum of a function of several variables. FMINCON attempts to solve problems of the form: min F(X) subject to: A*X <= B, Aeq*X = Beq (linear constraints) X C(X) <= 0, Ceq(X) = 0 (nonlinear constraints) LB <= X <= UB (bounds) FMINCON implements four different algorithms: interior point, SQP, active set, and trust region reflective. Choose one via the option Algorithm: for instance, to choose SQP, set OPTIONS = optimoptions('fmincon','Algorithm','sqp'), and then pass OPTIONS to FMINCON. X = FMINCON(FUN,X0,A,B) starts at X0 and finds a minimum X to the function FUN, subject to the linear inequalities A*X <= B. FUN accepts input X and returns a scalar function value F evaluated at X. X0 may be a scalar, vector, or matrix. X = FMINCON(FUN,X0,A,B,Aeq,Beq) minimizes FUN subject to the linear equalities Aeq*X = Beq as well as A*X <= B. (Set A=[] and B=[] if no inequalities exist.) X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB) defines a set of lower and upper bounds on the design variables, X, so that a solution is found in the range LB <= X <= UB. Use empty matrices for LB and UB if no bounds exist. Set LB(i) = -Inf if X(i) is unbounded below; set UB(i) = Inf if X(i) is unbounded above. X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON) subjects the minimization to the constraints defined in NONLCON. The function NONLCON accepts X and returns the vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. FMINCON minimizes FUN such that C(X) <= 0 and Ceq(X) = 0. (Set LB = [] and/or UB = [] if no bounds exist.) X = FMINCON(FUN,X0,A,B,Aeq,Beq,LB,UB,NONLCON,OPTIONS) minimizes with the default optimization parameters replaced by values in OPTIONS, an argument created with the OPTIMOPTIONS function. See OPTIMOPTIONS for details. For a list of options accepted by FMINCON refer to the documentation. X = FMINCON(PROBLEM) finds the minimum for PROBLEM. PROBLEM is a structure with the function FUN in PROBLEM.objective, the start point in PROBLEM.x0, the linear inequality constraints in PROBLEM.Aineq and PROBLEM.bineq, the linear equality constraints in PROBLEM.Aeq and PROBLEM.beq, the lower bounds in PROBLEM.lb, the upper bounds in PROBLEM.ub, the nonlinear constraint function in PROBLEM.nonlcon, the options structure in PROBLEM.options, and solver name 'fmincon' in PROBLEM.solver. Use this syntax to solve at the command line a problem exported from OPTIMTOOL. [X,FVAL] = FMINCON(FUN,X0,...) returns the value of the objective function FUN at the solution X. [X,FVAL,EXITFLAG] = FMINCON(FUN,X0,...) returns an EXITFLAG that describes the exit condition. Possible values of EXITFLAG and the corresponding exit conditions are listed below. See the documentation for a complete description. All algorithms: 1 First order optimality conditions satisfied. 0 Too many function evaluations or iterations. -1 Stopped by output/plot function. -2 No feasible point found. Trust-region-reflective, interior-point, and sqp: 2 Change in X too small. Trust-region-reflective: 3 Change in objective function too small. Active-set only: 4 Computed search direction too small. 5 Predicted change in objective function too small. Interior-point and sqp: -3 Problem seems unbounded. [X,FVAL,EXITFLAG,OUTPUT] = FMINCON(FUN,X0,...) returns a structure OUTPUT with information such as total number of iterations, and final objective function value. See the documentation for a complete list. [X,FVAL,EXITFLAG,OUTPUT,LAMBDA] = FMINCON(FUN,X0,...) returns the Lagrange multipliers at the solution X: LAMBDA.lower for LB, LAMBDA.upper for UB, LAMBDA.ineqlin is for the linear inequalities, LAMBDA.eqlin is for the linear equalities, LAMBDA.ineqnonlin is for the nonlinear inequalities, and LAMBDA.eqnonlin is for the nonlinear equalities. [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD] = FMINCON(FUN,X0,...) returns the value of the gradient of FUN at the solution X. [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = FMINCON(FUN,X0,...) returns the value of the exact or approximate Hessian of the Lagrangian at X. Examples FUN can be specified using @: X = fmincon(@humps,...) In this case, F = humps(X) returns the scalar function value F of the HUMPS function evaluated at X. FUN can also be an anonymous function: X = fmincon(@(x) 3*sin(x(1))+exp(x(2)),[1;1],[],[],[],[],[0 0]) returns X = [0;0]. If FUN or NONLCON are parameterized, you can use anonymous functions to capture the problem-dependent parameters. Suppose you want to minimize the objective given in the function myfun, subject to the nonlinear constraint mycon, where these two functions are parameterized by their second argument a1 and a2, respectively. Here myfun and mycon are MATLAB file functions such as function f = myfun(x,a1) f = x(1)^2 + a1*x(2)^2; function [c,ceq] = mycon(x,a2) c = a2/x(1) - x(2); ceq = []; To optimize for specific values of a1 and a2, first assign the values to these two parameters. Then create two one-argument anonymous functions that capture the values of a1 and a2, and call myfun and mycon with two arguments. Finally, pass these anonymous functions to FMINCON: a1 = 2; a2 = 1.5; % define parameters first options = optimoptions('fmincon','Algorithm','interior-point'); % run interior-point algorithm x = fmincon(@(x) myfun(x,a1),[1;2],[],[],[],[],[],[],@(x) mycon(x,a2),options) See also OPTIMOPTIONS, OPTIMTOOL, FMINUNC, FMINBND, FMINSEARCH, @, FUNCTION_HANDLE. Documentation for fmincon doc fmincon
What is the second argument to fmincon assumed to be? Answer: a vector of initial estimates of the solution. x0 (the second input argument) is assumed to be a vector of the starting values for the opttimization, not just the number of variables to optimize over. You even try to index into x, inside the objective function. But all you did was pass fmincon a scalar value.
So fmincon assumes this is a ONE variable problem, with a starting value of 2.
You even gave fmincon lower and upper bounds for TWO variables.
LB = [4 5];
UB = [5 6];
Did you read the error message? It told you there were too many bounds provided, although that was only a warning message, trying to be friendly and help you. Again, this is a hint to you there is a problem with the variable x0.
  2 comentarios
Lorenzo Ceresole
Lorenzo Ceresole el 16 de Mzo. de 2022
Thank you very much, I'm sorry, im just very new to this optimization toolbox and a lot of the things I read did not make complete sense
Walter Roberson
Walter Roberson el 16 de Mzo. de 2022
Note that ga() would expect the number of variables in that particular position, but that fmincon() requires the initial position in that particular position.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by