MATLAB: FMINCON giving incorrect values

fmincon

I am trying to solve the max of the following nonlinear function:
F = (((.8*buyp*1.1+.2*buyp*.95)-buyp)*265000*7.33)/(1+0.00027397)^(distla+distab/(24*v(1)))-(fixc*(distla+distab/(24*v(1)))+ExogParams.HFO*tpd*((v(1)/v_des)^3)*(distla+distab/(24*v(1)))+ fixc*(distab/(24*v(2)))+645*tpd*((v(2)/v_des)^3)*(distab/(24*v(2)))));
I need to pass in the parameters: buyp,fixc,v_des, distla, distab,tpd to the function each time using FMINCON to solve. So for example, the function could look like:
F = ((( 0.8*100*1.1+0.2*100*.95-100)*265000*7.33)/(1+0.00027397)^(22706/(24*v1)) -(23400*(22706/(24*v1))+ ...
645*83*((v1/15.8)^3)*(22706/(24*v1))+ 23400*(11353/(24*v2))+ ...
645*83*((v2/15.8)^3)*(11353/(24*v2))));
My implementation of the general function F returns incorrect values and I can't figure out why (been staring at this for hours now). Call the first M file objspeedfun1.m:
function F = objspeedfun1(v, buyp,fixc,v_des, distla, distab,tpd)
F = (((.8*buyp*1.1+.2*buyp*.95)-... buyp)*265000*7.33)/(1+0.00027397)^(distla+distab/(24*v(1)))-(fixc*(distla+distab/(24*v(1)))+ExogParams.HFO*tpd*((v(1)/v_des)^3)*(distla+distab/(24*v(1)))+ fixc*(distab/(24*v(2)))+645*tpd*((v(2)/v_des)^3)*(distab/(24*v(2)))));
F=-F; %solves for max instead of min
The second M file calls F:
function [v1, v2, fval] = solveOptimalSpeed1(buyp, fixc,v_des, distla, distab,tpd)
%@brief: maximizes objective function objspeedfun1 given parameters
%> v_laden is optimal laden speed
%> v_ballast is optimal ballast speed
%> fval is the value you get from maximisation problem with optimal speed
%params
f = @(v)objspeedfun1(v,buyp, fixc,v_des, distla, distab,tpd);
%Assign parameter values
options = optimset('Algorithm','active-set');
x0 = [13.5; 8]; %Make a starting guess at the solution
lb = [8 8];
ub = [15.8 15.8];
[x,fval] = fmincon(f,x0,[],[],[],[],lb,ub,[],options); %Call solver
v1 = x(1);
v2 = x(2);
When I call the function solveOptimalSpeed1, I get the following result:
v1=8
v2=9.5171
fval=3.4749e+08
which is incorrect. Using trial and error, the solution should be around (or of magnitude) 7966644.13, using the example above.
Your help is really appreciated.

Best Answer

My guess is that the typical amplitudes of your objective function ~10^8 and its derivatives are too large for FMINCON to ever reach the default stopping tolerance TolFun=1e-6. The algorithm therefore runs some default maximum number of iterations and then bails out prematurely. To check, you should call FMINCON with all of its output arguments and look at the exitflag and also output.firstorderopt.
A solution would probably be to rescale your F to take typical values on the order of 1 or else to increase TolFun.
However, unless you will eventually be working with more complicated constraints, it doesn't look like you really even need anything so nearly as complicated as FMINCON to solve this problem. Your function is additively separable in v(1) and v(2), meaning that it is of the form
F=f(v(1)) + g(v(2))
The problem can therefore be solved by minimizing/maximizing f() and g() individually. Since they are 1D functions, they can be optimized more simply using FMINBND.