Error using barrier (line 22) Objective function is undefined at initial point. Fmincon cannot continue.

27 次查看(过去 30 天)
Plz help to encounter error..
function stm2
clc; close all; clear all
A = [[1 1];[1 1];[1 1]];
b = [1;1;1];
X0 = [0.5 0.5];
options = optimset('Algorithm','interior-point');
X = fmincon(@obj,X0,A,b,[],[],[],[],[],options)
extent(X);
e1 = X(1);
e2 = X(2);
r=0;
t= 1+r+8*e1+4*e2;
yC4H10O = (1 - e1 - e2)/t;
yH2O = (r - 3*e1)/t;
yCO = (4*e1+e2)/t;
yH2 = (8*e1+e2)/t;
yCH4 = 2*e2/t;
yC = e2/t;
function G = obj(X)
e1 = X(1);
e2 = X(2);
T = 500; % Temperature in K
K1 = exp((-60902/T)+25.4390*log(T)-0.015*T-1.8541e-6*(T^2)+1.9970e-9*(T^3)-3.9585e-13*(T^4)-51.9643);
K2
=
exp((978.7948/T)+(15.1311*log(T))-(0.0266*T)+(1.1597*(10^-5)*(T^2))-(3.4760*(10^-9)*(T^3))+(4.7165*(10^-13)*(T^4))-46.8726);
P = 1;
r=0;
t= 1+r+8*e1+4*e2;
yC4H10O = (1 - e1 - e2)/t;
yH2O = (r - 3*e1)/t;
yCO = (4*e1+e2)/t;
yH2 = (8*e1+e2)/t;
yCH4 = 2*e2/t;
yC = e2/t;
G = -(e1*log(K1) + e2*log(K2)) + ...
t*log(P) + yC4H10O*t*log(yC4H10O) + yH2O*t*log(yH2O)+...
yCO*t*log(yCO) + yH2*t*log(yH2) + yCH4*t*log(yCH4) + yC*t*log(yC);
end
end
it is giving massage after running code....
Error using barrier (line 22)
Objective function is undefined at initial point.
Fmincon cannot continue.
Error in fmincon (line 900)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn,
...
Error in stm2 (line 7)
X = fmincon(@obj,X0,A,b,[],[],[],[],[],options)
How can we resolve this issue?
thanks

回答(7 个)

Walter Roberson
Walter Roberson 2015-8-24
In the code you posted, you have K2 split over 3 lines without using the ... continuation marks between "K2" and the "=" and the next line. You need to bring that on to one line or use ... as appropriate.
The X0 you use is 0.5 0.5 which adds up to 1. In your obj, yC4H10O = (1 - e1 - e2)/t; and since e1 and e2 add up to 1, yC4H10O will be 0. You take log() of that which gives -Inf and you multiply that by the 0 that is yC4H10O so you have 0 * -Inf which is NaN. That NaN is going to taint the entire rest of the calculation giving NaN as the real portion of the result. fmincon cannot minimize an expression with NaN
You have yH2O = (r - 3*e1)/t but your r is 0. If your e1 is positive then yH2O is going to be negative (unless t is negative, but t is computed strictly through addition.) In constructing G you take log(yH2O) and since yH2O is negative you are going to get a complex result. Therefore even if your 0.5 0.5 did not happen to add up to 1.0, you would get a complex result.
You should be considering setting the FunValCheck option 'on' to help you debug these problems in the future.
  2 个评论
Shawn Miller
Shawn Miller 2016-2-4
Can you explain how FunValCheck works? Does a program just gracefully end with this option on? Can you have a look at this link?
Walter Roberson
Walter Roberson 2016-2-4
When FunValCheck is on, if there is a violation, an infinity or nan, then error() will be used to terminate the optimization. You could use try/catch to respond smoothly. It is not the case that you would get some specific exitflag : because of the error() there is no result at all.

请先登录,再进行评论。


Bartlomiej Mroczek
Bartlomiej Mroczek 2020-11-1
编辑:Walter Roberson 2020-11-4
Has anyone faced a similar problem?
Error using barrier
Objective function is undefined at initial point. Fmincon cannot continue.
Error in fmincon (line 813)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in test (line 40)
[V, fval]= fmincon(fun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options)
Line of code:
fun = @(V) ((-YI12-YII12)*V(1)+YI12*V(2)+YII12*V(3))/conj(V(1))
x0 = [1+0i; 1+0i; 1+0i];
A = [;-YI12,YI12,0; -YII12,0,YII12];
b = [;-(Sg1-Sl1)/conj(V(2));-(Sg2-Sl2)/conj(V(3))];
Aeq = [];
beq= [];
lb = [0.9, 0.9, 0.9];
ub = [1.1, 1.1, 1.1];
nonlcon = [];
options = optimoptions('fmincon','Display','iter' );
[V, fval]= fmincon(fun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options)
  1 个评论
Walter Roberson
Walter Roberson 2020-11-1
b cannot be a function and you use variables to define it that are not defined in the code and which context suggests that you intend to be function arguments

请先登录,再进行评论。


Bartlomiej Mroczek
Bartlomiej Mroczek 2020-11-1
Nice, but the equation doesn't allow it.
I don't know how to get around this.
Maybe there is some other function for such a case?
Case:
First equation:
A1 * V (1) + A2 * V (2) - b / V (2) = 0
Second equation:
A1 * V (1) + A3 * V (3) - b / V (3) = 0
Maybe I should use nonlcon?
  1 个评论
Walter Roberson
Walter Roberson 2020-11-1
Those are nonlinear conditions. If you multiply through by V(2) then you get rid of the division, but you end up multiplying two variables together which is a nonlinear constraint.

请先登录,再进行评论。


Bartlomiej Mroczek
Bartlomiej Mroczek 2020-11-1
I'll check, let you know
Thanx ROB.

Bartlomiej Mroczek
Bartlomiej Mroczek 2020-11-1
Continuous Success Story :)
I built the function nonlcon name: grid - anather script
function [c, ceq] = grid (V)
c = [-YI12 * V (1) * V (2) + YI12 * V (2) * conj (V (2)) - (Sg1-Sl1);
-YII12 * V (1) * V (3) + YII12 * V (3) * conj (V (3)) - (Sg2-Sl2)]
ceq = [];
end
new code form in main file:
fun = @ (V) ((-YI12-YII12) * V (1) + YI12 * V (2) + YII12 * V (3)) / conj (V (1));
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0.9, 0.9, 0.9];
ub = [1.1, 1.1, 1.1];
nonlcon = @grid;
x0 = [1 + 0i, 1 + 0i, 1 + 0i];
options = optimoptions ('fmincon', 'Display', 'iter');
[V, fval] = fmincon (fun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options)
An error appears:
Error: File: grid.m Line: 29 Column: 21
Function with duplicate name "grid" cannot be defined.
Any idea, I've tried everything.
  1 个评论
Walter Roberson
Walter Roberson 2020-11-1
You might have a file named grid.m that has two "function" lines that define function grid (look on line 29)
You might have a file named grid.m that defines two functions, with the first one being named something else and the one on line 29 named grid. However in MATLAB, the first function in a function file is automatically renamed to the same as the file name so the first one would not be know as whatever is in the function line and instead would be known by the file name.
You might have a script file named grid.m that also defines a function named grid. The name of any function in a script file must be different than the name of the script file.

请先登录,再进行评论。


Bartlomiej Mroczek
Bartlomiej Mroczek 2020-11-2
编辑:Walter Roberson 2020-11-4
Hi
OKAY. I got it. I also made an option in one file with the grid function - this fragment also compiles.
There iś another problem.
First.
In a file with a different name, I created the grid function
function [c, ceq] = grid (V)
c = [-YI12 * V (1) * V (2) + YI12 * V (2) * conj (V (2)) - (Sg1-Sl1)];
ceq = [-YII12 * V (1) * V (3) + YII12 * V (3) * conj (V (3)) - (Sg2-Sl2)];
end
Compiles !!!!. It work's.
Back to the main file. In the main file: I have an error:
Error using grid Too many output arguments.
Main Code:
fun = @ (V) ((-YI12-YII12) * V (1) + YI12 * V (2) + YII12 * V (3)) * conj (V (1));
A = [];
b = [];
Aeq = [];
beq = [];
lb = [0.9, 0.9, 0.9];
ub = [1.1, 1.1, 1.1];
nonlcon = @grid;
x0 = [1 + 0i, 1 + 0i, 1 + 0i];
options = optimoptions ('fmincon', 'Display', 'iter');
[V, fval] = fmincon (fun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options)
Both functions have the same variable range from V (1), V (2), V (3).
Now I don't know what's going on?
Where is the error in thinking, a request for help.
  1 个评论
Walter Roberson
Walter Roberson 2020-11-2
"In a file with a different name, I created the grid function"
The first function in a file is known by its file name. The other functions in the file cannot be directly called (unless they are static methods of a class.)
Therefore the grid() you are invoking is the Mathworks grid() function for turning on or off grid lines in an axes.

请先登录,再进行评论。


Bartlomiej Mroczek
Bartlomiej Mroczek 2020-11-2
Is it correct:
The function file is called grid and the function is mygrid.
File: grid.m
function [c, ceq] = mygrid (V)
% c, ceq are output varibles grid
% V is input of function
V = [V (1), V (2), V (3)];
c = [-YI12 * V (1) * V (2) + YI12 * V (2) * conj (V (2)) - (Sg1-Sl1)];
ceq = [-YII12 * V (1) * V (3) + YII12 * V (3) * conj (V (3)) - (Sg2-Sl2)];
end
What next? If yes, what should I put in nonlcon = @ ??????
Because if I type:
1. @grid - master script doesn't work - doesn't know what it is
2. @mygrid - error
Attempt to execute SCRIPT grid as a function:
/Users/bartlomiejmroczek/Documents/MATLAB/grid.m
Error in fmincon (line 633)
[ctmp, ceqtmp] = feval (confcn {3}, X, varargin {:});
Error in test (line 39)
[V, fval] = fmincon (fun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options)
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot
continue.

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by