MATLAB: Fmincon not working for script

curve fittingMATLAB

I have created a function that takes my system of ODEs and gets the difference from the real data, but when using fmincon, the returned values are not right and I get the error term "local min possible". How can I get it to return the correct parameters?
% function difference = objfun( k )
%t_data = [ .1 .2 .3 .4 .5 .6 .7 .8 ];
%P_data = [ 2.5 5 7.2 8.7 9.5 9.8 9.9 10 ];
P_data = [57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 57 56 56 56 56 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 54 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53 53];
t_data = [0:89];
Beta = k(1); %.005; %40;
Gamma= k(2); %.162; % 25;
Delta = k(3); %.001; % 15;
S0 = 63; % 2;
I0 = 1;
V0 = 57;
time = 89;
h = 1;
[t,x]=ode15s(@(t,x) [-Beta*x(1)*x(2);
Beta*x(1)*x(2)-Gamma*x(2)+Delta*x(4)*x(2);
Gamma*x(2);
-Delta*x(4)*x(2)], [0:h:time], [S0;I0;0;V0] );
difference = 0;
for i = 1:size(t_data,2)
ind = t == t_data(i);
difference = difference + ( x(ind,4) - P_data(i) )^2;
end
*in command window: opt= fmincon(@objfun, [.01,.03,.05], [],[])

Best Answer

You are getting this warning because you function is non-convex and have several local minima. The minima you will reach depends on the starting point x0. One way is to try select several different starting points x0 and choose the point which produces the least value for objfun. The other and more efficient solution is to use the Global Optimization Toolbox to find the global minima. You can follow this example to learn how to use it for your problem.