Search code examples
matlabmodelingdata-fitting

Finding global minima using matlab?


I am trying to find the global minima for the peaks function using the following code:

    opts = optimoptions(@fmincon,'Algorithm','interior-point');
problem = createOptimProblem('fmincon','objective',...
     @(x) ...
     3*(1-x(1)).^2.*exp(-(x(1).^2) - (x(2)+1).^2) ... 
   - 10*(x(1)/5 - x(1).^3 - x(2).^5).*exp(-x(1).^2-x(2).^2) ... 
   - 1/3*exp(-(x(1)+1).^2 - x(2).^2),...
     'x0',[0,0],'lb',[-Inf,-Inf],'ub',[Inf,Inf],'options',opts);
gs = GlobalSearch;
[x,f] = run(gs,problem);
x

I got :

x =

    4.2454    2.3345

which should not be the correct answer. (according to the tutorial online) enter image description here

What might me wrong? Thanks.


Solution

  • Matlab's example using a bound constraint in the region [-3,3], [-3,3]. If you change your code to following you get Matlab's answer

        problem = createOptimProblem('fmincon','objective',...
         @(x) ...
         3*(1-x(1)).^2.*exp(-(x(1).^2) - (x(2)+1).^2) ...
       - 10*(x(1)/5 - x(1).^3 - x(2).^5).*exp(-x(1).^2-x(2).^2) ...
       - 1/3*exp(-(x(1)+1).^2 - x(2).^2),...
         'x0',[0,0],'lb',[-3,-3],'ub',[3,3],'options',opts);
    

    Btw, if you want to solve an unconstrained problem, then use fminunc instead.