3
votes

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
2

2 Answers

5
votes

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);
1
votes

The docs for fminsearch don't describe a way to restrict the domain of the function you want to minimize.

If you want to restrict the range to all non-negative numbers then you can simply wrap your function in a call to abs, depending on the syntax .

minValuePossible = fminsearch( @(x)(minimiser( abs(x) ) ), inValues); 

If you are worried about it constantly converging to the same minima then try a variety of different initial values.

Lastly you can alter the termination tolerances for X and minValuePossible using the TolX and TolFun input parameters. This is done with standard param value syntax: function(...., 'Param', value)

fminsearch( @(x)(minimiser(abs(x))), inValues, 'TolX', x_tolerance);