Failure in initial objective function evaluation. FMINCON cannot continue. error in fmincon line(536)

I am trying to optimize my Gibbs energy function for a mixture of gases with fmincon and I keep getting 3 errors. my function looks like this.
function G=myGfunc(nj)
Enj=sum(nj);
G=sum(nj.*(Gjo/R/T+ log(nj/Enj*(p/po))));
end
and I call the fmincon as follows:
options=optimset('Algorithm', 'interior-point');
x=fmincon(@myGfunc,x0,[],[],Aeq,beq,LB,[],[],options);
the errors I get are:
Undefined function or variable 'Gjo'.
Error in myGfunc (line 3)
G=sum(nj.*(Gjo/R/T+ log(nj/Enj*(p/po))));
Error in fmincon (line 536)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in myMeth (line 70)
x=fmincon(@myGfunc,x0,[],[],Aeq,beq,LB,[],[],options);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
It's weird since my Gjo is well defined and it gave solid values, before not defining the parameters needed for fmincon.
any help would by greatly appreciated

 Akzeptierte Antwort

Your Gjo is initialized to 8 x 1, but in your function you use Gjo' which makes that into 1 x 8. You did not happen to notice that because you are using R2016b or later, which has "implicit expansion", and treated the addition of an 8x1 with a 1x8 by doing the equivalent of bsxfun(@plus, 8x1, 1x8) giving an 8 x 8 result.
Files attached. The one to run is abs_entrop

Weitere Antworten (3)

I suspect there is an argument for the log-function which is negative or undefined.
Best wishes
Torsten.
Almost certainly, you are not passing the value of Gjo that is in your workspace. See Passing Extra Parameters.
Alan Weiss
MATLAB mathematical toolbox documentation

3 Kommentare

that's true. I defined it as global , but still I am getting a series of new errors!
here's my function now:
function G=myGfunc(nj)
global T R P Po Gjo
Enj = sum(nj);
h = P/Po*nj/Enj;
w = log(h);
w(h==0) = 0;
G = sum(nj.*(Gjo'+ R*T* w));
end
and I my scrip:
Aeq = [0 1 2 0 1 0 2 0 % oxygen balance
4 0 0 2 2 0 0 0 % hydrogen balance
1 1 1 0 0 0 0 1 % carbon balance
0 0 0 0 0 2 0 0]; % nitrogen balance
%the mole values in the feed
beq = [8 % moles of oxygen atoms coming in
32 % moles of hydrogen atoms coming in
4 % moles of carbon atoms coming in
2.2]; % moles of nitrogen atoms coming in
%Constraints on bounds
LB=[0 0 0 0];
%solve the minimization problem
x0 = [1e-1; 1e-1; 1e-1; 1e-1; 1e-1; 1e-1; 1e-1; 1e-1]; % initial guess
options=optimset('Algorithm', 'interior-point');
x=fmincon(@myGfunc,x0,[],[],Aeq,beq,LB,[],[],options);
and finally the errors:
Warning: Length of lower bounds is < length(x); filling in missing lower bounds with -Inf.
> In checkbounds (line 33)
In fmincon (line 308)
In myMeth_081017 (line 66)
Error using fmincon (line 609)
Supplied objective function must return a scalar value.
Error in myMeth_081017 (line 66)
x=fmincon(@myGfunc,x0,[],[],Aeq,beq,LB,[],[],options);
I truly appreciate ur help.
You did not initialize your global variables. All of those global variables are going to be [] because they have not been initialized. Your routine is going to be returning [] rather than a scalar.
oh yes I did. I just did not post them here. here is how I initialized them:
global T R P Po Gjo
T=1000; %K
R=8.314e-3;
P=6; %bar,
Po=1; %bar,
species= {'CH4' 'CO' 'CO2' 'H2' 'H2O' 'N2' 'O_2' 'C'};
Hf298= [-74; -110.53; -393.51; 0.0; -241.826; 0.0; 0.0; 0.0];
WB = [-0.703029 108.4773 -42.52157 5.862788 0.678565 -76.84376 158.7163 -74.87310 %CH4
25.56759 6.096130 4.054656 -2.671301 0.131021 -118.0089 227.3665 -110.5271 % CO
24.99735 55.18696 -33.69137 7.948387 -0.136638 -403.6075 228.2431 -393.5224 % CO2
33.066178 -11.363417 11.432816 -2.772874 -0.158558 -9.980797 172.707974 0.0 %H2
30.09200 6.832514 6.793435 -2.534480 0.082139 -250.8810 223.3967 -241.8264 % H2O
19.50583 19.88705 -8.598535 1.369784 0.527601 -4.935202 212.3900 0.0 %N2
31.32234 -20.23531 57.86644 -36.50624 -0.007374 -8.903471 246.7945 0.0 %O2
21.17510 -0.812428 0.448537 -0.043256 -0.013103 710.3470 183.8734 716.6690];
t=T/1000;
T_H = [t; t^.2/2; t.^3/3; t.^4/4; -1./t; 1; 0; -1];
T_S = [log(t); t; t.^2/2; t.^3/3; -1./(2*t.^2); 0; 1; 0];
H=WB*T_H; %(H-H_298.15)
S=WB*T_S/1000; %absolute entropy kj/mol/k
Gjo= Hf298+H-T*S;

Melden Sie sich an, um zu kommentieren.

I suggest that you use the debugger and see what the size of the returned value G is from your objective function. I suspect that you are returning a vector, not a scalar.
Alan Weiss
MATLAB mathematical toolbox documentation

Kategorien

Gefragt:

am 5 Okt. 2017

Kommentiert:

am 11 Okt. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by