Solution function fminunc (linear and nonlinear) in matlab minimization function

1. Unconstrained (unconditional) optimization

fminunc function
  • It can be used to find the minimum value of any function
  • Unified minimum problem
  • To find the maximum value:

Taking the opposite number of a function becomes the problem of finding the minimum value. Finally, taking the opposite value of a function is the maximum value of a function.

  • The format is as follows
    1. The function must be stored in a program in advance. (the program must have only one parameter. If it is a multivariate function, then x(1),x(2),x(3) Each independent variable is represented separately);
    2.fval is the minimum value of the function, x0 is the initial vector of the independent variable, which generally does not affect the result (if there are n variables (i.e. n-ary function), then there are n elements in x0);
    3.exitflag is the exit flag. When it is greater than 0, it means that the function converges to x, when it is equal to 0, it means that the number of iterations is more than, when it is less than 0, it means that the function does not converge (therefore, it is necessary to judge whether the value of exitflag is greater than 0 after solving the problem, so as to determine the correctness / validity of the result)

x=fminunc('Program name ', x0)
[x,fval]=fminunc()
[x,fval,exitflag]=fminunc()
Functions can use the inline function inline('expression ')

  • Example 1: minimum value of y= x^2+4x+5

    %Defining functions
    function f=fu(x)
    f=x^2+4*x+5; %Better not.*	.^	./Because it is not an operation of a vector (a batch of numbers), the initial x0 Is the number of variables
    %Function call(4Methods)
    [x,fval,exitflag]=fminunc(@fu,1)
    [x,fval,exitflag]=fminunc(inline('x^2+4*x+5'),2)
    [x,fval,exitflag]=fminunc(@(x)x^2+4*x+5,1)
    [x,fval,exitflag]=fminunc('x^2+4*x+5',2)
    %Result output:
    % x =-2.0000
    % fval =1.0000
    % exitflag = 1
    
  • Example 2: maximum value of y= 2x-x^4+1

    %Function definition
    function f=fu(x)
    f=2*x-x^4+1;
    %function call
    [x,fval,exitflag]=fminunc(@fu,1)
    [x,fval,exitflag]=fminunc(inline('2*x-x^4+1'),2)
    [x,fval,exitflag]=fminunc(@(x)2*x-x^4+1,1)  %Initial value x0=1,Does not affect the result output
    [x,fval,exitflag]=fminunc('2*x-x^4+1',2)  %Initial value x0=2
     %Result output:
     x = 597872
     fval =  -1.2777e+23
     exitflag =-3
    
    about exitflag matlab Help document description

    1
    Magnitude of gradient is smaller than the OptimalityTolerance tolerance.
    2
    Change in x was smaller than the StepTolerance tolerance.
    3
    Change in the objective function value was less than the FunctionTolerance tolerance.
    5
    Predicted decrease in the objective function was less than the FunctionTolerance tolerance.
    0
    Number of iterations exceeded MaxIterations or number of function evaluations exceeded MaxFunctionEvaluations.
    -1
    Algorithm was terminated by the output function.
    -3
    Objective function at current iteration went below ObjectiveLimit.

  • Example 3: the minimum value of F (x, y) = E2x (x + Y2 + 2Y) f (x, y) = e ^ {2 x} \ left (x + y ^ {2} + 2Y \ right) f (x, y) = E2x (x + Y2 + 2Y)

    %Function definition
    function f=fu(a)  %Only one parameter is required,Can be transferred in matrix form
    x=a(1);y=a(2);
    f=exp(2*x)*(x+y^2+2*y);
    %function call
    [x,fval,exitflag]=fminunc('fu',[2,1])
    %Function result
    x =0.5000   -1.0000
    fval = -1.3591
    exitflag =1
    
  • fminbnd('Program name ', x1,xn) finds the minimum value of the function in the interval [x1,xn]

2. Optimization with constraints

fminunc function

(condition order: (linear) inequality - (linear) equality - upper and lower limits - nonlinear condition)
  • The left side can be:

    x=
    [x,fval]=
    [x,fval,exitflag]=

  • On the right
    (1) fmincon('Program name ', x0,A,b)
    For linear inequality constraints, ax < = b, a is coefficient matrix, b is constant term column vector, x0 is initial vector*
    (2) fmincon('Program name ', x0,A,b,Aeq,beq)
    For linear inequality and linear equality constraint, the linear equation is Aeq*x=beq, where Aeq is coefficient matrix and beq column vector
    (3) fmincon('Program name ', x0, A,b,Aeq,beq, l,u)
    Where l and u are the upper and lower limits of the solution (i.e. the range of solution l < = x < = u)
    (if it is a multivariate function, then l=[x0,y0,z0,...) .], u=[xn,yn,zn,… )
    (4)fmincon('Program name ', x0, A,b,Aeq,beq, l,u,' program 2 ')
    Where 'program 2' is used for nonlinear constraints, and its format is: C (x) < 0 CEQ (x) = 0
    The procedure form is:

    function [c,ceq]=fu(x)
     c=......;ceq=......;
    
  • be careful:
    1 if not used, a null vector [] must be used
    2. After solving the problem, you must also judge whether the value of exitflag is greater than 0 to determine the correctness of the result - so it's better to return three results and look at exitflag. If it's invalid, change to an initial vector x0

  • Example 1: min ⁡ f (x, y, z) = x 2 / 2 + XY + XZ + Y2 + 3yZ + Z3 − 6x − 7y − 8z s. t ⋅ x + y + 3Z < = 5x + 2Y + Z < = 63x + y + 2Z < = 13 \begin{aligned} &\min \cdot f(x, y, z)=x^{2} / 2+x y+x z+y^{2}+3 y z+z^{3}-6 x-7 y-8 z\\ &\text { s. } t \cdot x+y+3 z<=5\\ &\begin{array}{l} x+2 y+z<=6 \\ 3 x+y+2 z<=13 \end{array} \end{aligned}​min⋅f(x,y,z)=x2/2+xy+xz+y2+3yz+z3−6x−7y−8z s. t⋅x+y+3z<=5x+2y+z<=63x+y+2z<=13​​

Tags: less MATLAB

Posted on Fri, 26 Jun 2020 02:38:59 -0400 by hermes