Conversion] Matlab learning-Minimum value

Source: Internet
Author: User

 

1. Minimum value of a constrained unary Function
The standard form of a single variable function for minimum value is Min f (x) sub. To X1 & lt; X & lt; x2.
Use the fmin function in MATLAB 5.x to calculate its minimum value.
Function fminbnd
Format x = fminbnd (fun, x1, x2) % returns the value of X when the function fun gets the minimum value in the range of the independent variable X, fun is the expression string of the target function or the function handle of the MATLAB custom function.
X = fminbnd (fun, x1, x2, options) % options specifies the optimization Parameter options
[X, fval] = fminbnd (...) % Fval is the minimum value of the target function.
[X, fval, exitflag] = fminbnd (...) % Xitflag is the condition for terminating Iteration
[X, fval, exitflag, output] = fminbnd (...) % Output indicates optimization information.
If the parameter exitflag & gt; 0, it indicates that the function converges to X. If exitflag = 0, it indicates that it exceeds the function estimate value or the maximum number of iterations. exitflag & lt; 0 indicates that the function does not converge to X. If output = iterations indicates the number of iterations, output = funccount indicates the number of function assignments, and output = algorithm indicates the algorithm used.
Example 5-3 Calculate the minimum value of the following function on []
F (x) = (X-3) ^ 2-1
Solution: first define the function: Create the M file in the MATLAB editor as follows:
Function f = myfun (X)
F = (X-3). ^ 2-1;
Save as myfun. m, and then type the command in the Command window:
& Gt; X = fminbnd (@ myfun, 0, 5)
The result is displayed as follows:
X =
3
2. Unlimited minimum value of a multivariate Function
The standard form of the minimum value of a multivariate function is Min f (x)
X is a vector, such
Use fmins to calculate the minimum value of the value in MATLAB 5.x.
Use the fminsearch function to obtain the minimum value of a non-constrained multivariate function.
Function fminsearch
The format x = fminsearch (fun, X0) % x0 is the initial point, and fun is the expression string of the target function or the function handle of the MATLAB custom function.
X = fminsearch (fun, x0, options) % options query optimset
[X, fval] = fminsearch (...) % The most advantageous function value
[X, fval, exitflag] = fminsearch (...) % Exitflag is consistent with that of a single variable
[X, fval, exitflag, output] = fminsearch (...) % Output is consistent with that of a single variable.
Note: fminsearch adopts the Nelder-Mead simple search method.
The command uses the fminunc function to obtain the minimum value of a multi-variable unrestricted function.
Function fminunc
Format x = fminunc (fun, X0) % returns the minimum function value point for the given initial vertex x0
X = fminunc (fun, x0, options) % options is the specified Optimization Parameter
[X, fval] = fminunc (...) % Fval function value at least X
[X, fval, exitflag] = fminunc (...) % Exitflag is the condition for terminating the iteration.
[X, fval, exitflag, output] = fminunc (...) % Output indicates the output optimization information.
[X, fval, exitflag, output, grad] = fminunc (...) % Grad is the gradient value of the function at solution x.
[X, fval, exitflag, output, grad, Hessian] = fminunc (...) % Hessian value of the target function at solution x
Note: When the order of a function is greater than 2, fminunc is more effective than fminsearch, but fminsearch works better when the selected function is highly discontinuous.
In Example 5-5, evaluate f (x) = 3 * X1 ^ 2 + 2 * X1 * X2 + x2 ^ 2.
& Gt; fun = & #39; 3 * x (1) ^ 2 + 2 * x (1) * X (2) + X (2) ^ 2 & #39 ;;
& Gt; X0 = [1];
& Gt; [x, fval, exitflag, output, grad, Hessian] = fminunc (fun, X0)
Result:
X =
1.0e-008 *
-0.7591 0.2665
Fval =
1.3953e-016
Exitflag =
1
Output =
Iterations: 3
Funccount: 16
Stepsize: 1.2353
Firstorderopt: 1.6772e-007
Algorithm: & #39; medium-scale: quasi-Newton line search & #39;
Grad =
1.0e-006 *
-0.1677
0.0114
Hessian =
6.0000 2.0000
2.0000 2.0000
Or use the following method:
& Gt; fun = inline (& #39; 3 * x (1) ^ 2 + 2 * x (1) * X (2) + X (2) ^ 2 & #39 ;)
Fun =
Inline Function:
Fun (x) = 3 * x (1) ^ 2 + 2 * x (1) * X (2) + X (2) ^ 2
& Gt; X0 = [1];
& Gt; X = fminunc (fun, X0)
X =
1.0e-008 *
-0.7591 0.2665
3. Minimum value of a constrained multivariate Function
The standard form of Non-Linear Constrained multivariate functions is:
Min f (x)
Sub. to C (x) & lt; = 0
CEQ (x) = 0
A * X & lt; = B
Aeq * x = beq
LB & lt; = x & lt; = UB

X, B, beq, LB, and UB are vectors, A and aeq are matrices, C (X) and CEQ (x) are return vector functions, and f (x) is the target function. f (x), C (x), and CEQ (x) can be non-linear functions.
In php5.x, it is implemented by the constr function.
Function fmincon
Format x = fmincon (fun, x0, a, B)
X = fmincon (fun, x0, A, B, aeq, beq)
X = fmincon (fun, x0, A, B, aeq, beq, LB, UB)
X = fmincon (fun, x0, A, B, aeq, beq, LB, UB, nonlcon)
X = fmincon (fun, x0, A, B, aeq, beq, LB, UB, nonlcon, options)
[X, fval] = fmincon (...)
[X, fval, exitflag] = fmincon (...)
[X, fval, exitflag, output] = fmincon (...)
[X, fval, exitflag, output, Lambda] = fmincon (...)
[X, fval, exitflag, output, lambda, grad] = fmincon (...)
[X, fval, exitflag, output, lambda, grad, Hessian] = fmincon (...)
Parameter description: fun is the target function, which can be defined using the previous method;
X0 is the initial value;
A and B satisfy Linear Inequality Constraints. If there is no inequality constraint, A = [], B = [];
Aeq and beq meet the equality constraints. If they do not exist, aeq = [], beq = [];
LB and UB meet the requirements. If no boundaries exist, you can set lB = [], UB = [];
Nonlcon is used to calculate the estimation C and CEQ of Non-Linear Unequal constraints and equality constraints at X through the accepted vector X. It is used by specifying function handles, such as: & gt; & gt; X = fmincon (@ myfun, x0, A, B, aeq, beq, LB, UB, @ mycon). Create a nonlinear constraint function and save it as mycon. m: function [C, CEQ] = mycon (X)
C =... % Calculate the Non-Linear Unequal constraint function value at X.
CEQ =... % Calculate the function value of the nonlinear equality constraint at X.
Lambda is a multiplier of the resource. It indicates which constraint is valid.
Output optimization information;
Grad indicates the gradient of the target function at X;
Hessian indicates the hessiab value of the target function at X.
Example 5-6 find the optimal solution of the following problem at the initial vertex (0, 1)
Min X1 ^ 2 + x2 ^ 2x2 x x2-2 * x1-5 * X2
Sub. To-(x1-1) ^ 2 + X2 & gt; = 0
2 * x1-3 * X2 + 6 & gt; = 0

Solution: the standard form of constraints is
Sub. To (x1-1) ^ 2-x2 & lt; = 0
-2 * X1 + 3 * x2-6 & lt; = 0

First, create a non-linear constraint function file in the MATLAB Editor:
Function [C, CEQ] = mycon (X)
C = (x (1)-1) ^ 2-x (2 );
CEQ = []; % No Equality Constraint
Then, type the following command in the Command window or create an M file:
& Gt; fun = & #39; X (1) ^ 2 + X (2) ^ 2-x (1) * X (2)-2 * x (1) -5 * X (2) & #39; % Target Function
& Gt; X0 = [0 1];
& Gt; A = [-2 3]; % linear inequality constraint
& Gt; B = 6;
& Gt; aeq = []; % wireless Equality Constraints
& Gt; beq = [];
& Gt; lB = []; % x has no lower and upper bounds
& Gt; UB = [];
& Gt; [x, fval, exitflag, output, lambda, grad, Hessian]
= Fmincon (fun, x0, A, B, aeq, beq, LB, UB, @ mycon)
The result is
X =
3 4
Fval =
-13
Exitflag = % solution convergence
1
Output =
Iterations: 2
Funccount: 9
Stepsize: 1
Algorithm: & #39; medium-scale: SQP, quasi-Newton, line-search & #39;
Firstorderopt: []
Cgiterations: []
Lambda =
Lower: [2x1 double] % x lower bound validity, which can be viewed through lambda. Lower.
Upper: [2x1 double] % x upper bound valid. If it is 0, the constraint is invalid.
Eqlin: [0x1 double] % linear equality constraints are valid. If the value is not 0, the constraints are valid.
Eqnonlin: [0x1 double] % Nonlinear Equality Constraint validity.
Ineqlin: 2.5081e-008% linear inequality constraints.
Ineqnonlin: 6.1938e-008% nonlinear inequality constraints.
Grad = % gradient of the target function at the minimum vertex
1.0e-006 *
-0.1776
0
Hessian = % Hessian value of the target function at the minimum value
1.0000-0.0000
-0.0000 1.0000
In Example 5-7, find the optimal solution at the initial point X = (10, 10, 10.
Min f (x) =-X1 * X2 * X3
Sub. to 0 <= X1 + 2 * X2 + 2 * X3 <= 72
Solution: the standard form of constraints is
Sub. To-1 * x1-2 * x2-2 * X3 <= 0
X1 + 2 * X2 + 2 * X3 <= 72

> Fun = '-x (1) * X (2) * X (3 )';
> X0 = [10, 10, 10];
> A = [-1-2-2; 1 2 2];
> B = [0; 72];
> [X, fval] = fmincon (fun, x0, a, B)
Result:
X =
24.0000 12.0000 12.0000
Fval =
-3456

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.