25.3 Introduction to 10 optimization methods in matlab - Nelder-Mead method (matlab program)

 1. Brief description

      

The fminsearch function is used to solve multidimensional unconstrained linear optimization problems

Finding the minimum of a multivariate unconstrained function using a derivative-free method

   

grammar

   x = fminsearch(fun,x0)

   x = fminsearch(fun,x0,options)

   [x,fval] = fminsearch(...)

   [x,fval,exitflag] = fminsearch(...)

   [x,fval,exitflag,output] = fminsearch(...)

    explain

    fminsearch is able to find the minimum of a scalar function starting from an initial value. Often referred to as unconstrained nonlinear optimization

    x = fminsearch(fun,x0) Starting from x0, find the local minimum x in the function fun, x0 can be a scalar, vector, or matrix. fun is a function handle

    x = fminsearch(fun,x0,options) minimizes the function with the structure specified by the optimization parameters, which can be defined with the optimset function. (see matlab help)

[x,fval] = fminsearch(...) returns the function value of the objective function at result x

[x,fval,exitflag] = fminsearch(...) returns the exitflag value to indicate the condition for fminsearch to exit:

1 -- the function finds the result x

0--The maximum number of functional evaluations of the function is reached, or the number of iterations is reached

-1-- The algorithm ends with an external function

[x,fval,exitflag,output] = fminsearch(...) returns a structure output output, which contains the information of the optimization function: output.algorithm The optimization algorithm used output.funcCount The number of function calculations output.iterations The number of
iterations
output
. message exit message

 

in addition

fun is the function to be minimized, its input is input, the output is scalar f, and the objective function is estimated on x, the function can be a handle function of M file (when it is an M file, enclose the file name in single quotes ) :

functionx = fminsearch(@myfun, x0)

Here function f = myfun(x)

f = ... with arguments x

or write directly

asx = fminsearch(@(x)sin(x^2), x0);

 

 

algorithm

fminsearch uses the simplex method, which is a straightforward method that does not use numerical or gradient analysis

If the length of x is n, then there will be n+1 vertices. In two-dimensional space, the simplex is a triangle, and in three-dimensional space, it is a cone. In each step of the search, a point closer to the current simplex will be generated, and the function value at the new point will be compared with the values ​​at each vertex of the simplex. Generally, a fixed point will be replaced, and a new simplex will be generated. Repeat the steps until the size of the simplices is less than the threshold.

limit

fminsearch can deal with discontinuous problems. If the global optimum is not obtained, it will obtain the local optimum

It can only minimize the number of hours, complex numbers are not within the scope of its capabilities, and the return value of f(x) must also be a number of hours, if x is a complex number, it must be decomposed into real and imaginary parts.

 

We can use fminsearch for parameter estimation by making some transformations.

 

 

2. Code

 

Main program:

%% Use the Nelder-Mead method to find the optimal solution
f1203 = inline('x(1)*(x(1)-5-x(2))+x(2)*(x(2)-4)', 'x');
x0 = [0 4];
TolX = 1e-4; 
TolFun = 1e-9;
MaxIter = 100;
[xN,fN] = Opt_Nelder(f1203,x0,TolX,TolFun,MaxIter)
% take the minimum value Point and the minimum value here
[xF,fF] = fminsearch(f1203,x0) % use MATLAB built-in function fminsearch to solve

 

subroutine;

function [xo,fo] =Opt_Nelder(f,x0,TolX,TolFun,MaxIter)
%Nelder-Mead method is used for multi-dimensional variable optimization problem, dimension >=2.
N = length(x0);
if N == 1 % One-dimensional case, use quadratic approximation to calculate
    [xo,fo] = Opt_Quadratic(f,x0,TolX,TolFun,MaxIter);
    return
end
S = eye(N);
for i = 1:N % Independent variable dimension When it is greater than 2, repeat the calculation of each sub-plane
    i1 = i + 1;
    if i1 > N
        i1 = 1;
    end
    abc = [x0; x0 + S(i,:); x0 + S(i1,:)]; % Each directional sub-plane
    fabc = [feval(f,abc(1,:)); feval(f,abc(2,:)); feval(f,abc(3,:))]; [x0,
    fo ] = Nelder0(f,abc,fabc,TolX,TolFun,MaxIter);
    if N < 3 % two-dimensional case does not need to repeat
        break;
    end 
end
xo = x0;

3. Running results

 

02fff0129228438c9b55644fdd68bd51.png

81fd1919a7e04582bfd517f654f1a701.png

 

 

Guess you like

Origin blog.csdn.net/m0_57943157/article/details/131906054