Matlab fminsearch options/restrictions

thomashs87 picture thomashs87 · Jan 29, 2013 · Viewed 8.5k times · Source

I have this function in Matlab which is supposed to find the smallest value possible for minValuePossible, by varying the two initial set values of inValues. How can I set the fmin search function to NOT try negative values while trying to find the minimum? Also how can I set the number of different variations the fminsearch function performs while trying to find the minimum? Because currently it tries somewhere around 20 different combinations of the two inValues and then completes. Maybe define the amount by which it changes each value? How would I do that?

            function Valueminimiser

            inValues = [50,50];

            minValuePossible = fminsearch(@minimiser, inValues); 

                function result = minimiser(inValues)

                x=inValues(1);
                y=inValues(2);

                RunMode = 2;
                ValueOne = x;
                ValueTwo = y;

                [maxSCRAout] = main(RunMode,ValueOne,ValueTwo);
                result = minValuePossible;

              end

            end

Answer

Tomas picture Tomas · Feb 9, 2013

How can I set the fmin search function to NOT try negative values while trying to find the minimum?

Add the constrains of the values of your minimiser function at its beginning. If you meet this constrains then return a huge function value of minimizer. This will prevent fminsearch consider numbers which are not in your interest:

function result = minimiser(inValues)
            if (sum(inValues < 0) > 1) % check if there is any negative number in input variable
                result = hugeValue;    % give a big value to the result
                return;                % return to fminsearch - do not execute the rest of the code
            end

            x=inValues(1);
            y=inValues(2);

            RunMode = 2;
            ValueOne = x;
            ValueTwo = y;

            [maxSCRAout] = main(RunMode,ValueOne,ValueTwo);
            result = minValuePossible;

Also how can I set the number of different variations the fminsearch function performs while trying to find the minimum?

You can define options of fminsearch by using optimset function. The parameter of optimset 'MaxFunEvals' is the maximum number of evaluations -- notice that this cout even the values you constrained, so maybe setting 'TolX' as advised by @slayton might be better if you are concerned about the accuarcy.

options = optimset('MaxFunEvals',numberOfVariations);
minValuePossible = fminsearch(@minimiser, inValues,options);