• No results found

Minimization and Maximization

In document Mathematica Book.pdf (Page 151-155)

Minimize@expr,8x1,x2,…<D minimize expr

Minimize@8expr,cons<,8x1,x2,…<D minimize expr subject to the constraints cons Maximize@expr,8x1,x2,…<D maximize expr

Maximize@8expr,cons<,8x1,x2,…<D maximize expr subject to the constraints cons Minimization and maximization.

Minimize and Maximize yield lists giving the value attained at the minimum or maximum, together with rules specifying where the minimum or maximum occurs.

This finds the minimum of a quadratic function.

In[1]:= Minimize@x ^ 2 - 3 x + 6, xD

Out[1]= :15 4 , :x Ø3

2>>

Applying the rule for x gives the value at the minimum.

In[2]:= x ^ 2 - 3 x + 6 ê. Last@%D

Out[2]= 15 4

This maximizes with respect to x and y.

In[3]:= Maximize@5 x y - x ^ 4 - y ^ 4, 8x, y<D

Out[3]= :25

8 , :x Ø - 5

2 , y Ø - 5 2

>>

Minimize@expr, xD minimizes expr allowing x to range over all possible values from -¶ to +¶.

Minimize@8expr, cons<, xD minimizes expr subject to the constraints cons being satisfied. The constraints can consist of any combination of equations and inequalities.

This finds the minimum subject to the constraint x ¥ 3.

In[4]:= Minimize@8x ^ 2 - 3 x + 6, x >= 3<, xD

Out[4]= 86, 8x Ø 3<<

This finds the maximum within the unit circle.

This finds the maximum within an ellipse. The result is fairly complicated.

In[6]:= Maximize@85 x y - x ^ 4 - y ^ 4, x ^ 2 + 2 y ^ 2 <= 1<, 8x, y<D

Out[6]= 9-RootA-811 219 + 320 160 Ò1 + 274 624 Ò12- 170 240 Ò13+ 25 600 Ò14&, 1E, 9x Ø RootA25 - 102 Ò12+ 122 Ò14- 70 Ò16+ 50 Ò18&, 2E,

y Ø RootA25 - 264 Ò12+ 848 Ò14- 1040 Ò16+ 800 Ò18&, 1E==

This finds the maximum along a line.

In[7]:= Maximize@85 x y - x ^ 4 - y ^ 4, x + y == 1<, 8x, y<D

Minimize and Maximize can solve any linear programming problem in which both the objective function expr and the constraints cons involve the variables xi only linearly.

Here is a typical linear programming problem.

In[8]:= Minimize@8x + 3 y, x - 3 y <= 7 && x + 2 y >= 10<, 8x, y<D function and the constraints involve arbitrary polynomial functions of the variables. There are many important geometrical and other problems that can be formulated in this way.

This solves the simple geometrical problem of maximizing the area of a rectangle with fixed perimeter.

This finds the maximal volume of a cuboid that fits inside the unit sphere.

In[10]:= Maximize@88 x y z, x ^ 2 + y ^ 2 + z ^ 2 <= 1<, 8x, y, z<D maxima. Often functions will have various local minima and maxima at which derivatives van-ish. But Minimize and Maximize use global methods to find absolute minima or maxima, not just local extrema.

An important feature of Minimize and Maximize is that they always find global minima and maxima. Often functions will have various local minima and maxima at which derivatives van-ish. But Minimize and Maximize use global methods to find absolute minima or maxima, not just local extrema.

Here is a function with many local maxima and minima.

In[11]:= Plot@x + 2 Sin@xD, 8x, - 10, 10<D

Out[11]=

Maximize finds the global maximum.

In[12]:= Maximize@8x + 2 Sin@xD, - 10 <= x <= 10<, xD

Out[12]= : 3 +8 p 3

, :x Ø8 p 3

>>

If you give functions that are unbounded, Minimize and Maximize will return -¶ and +¶ as the minima and maxima. And if you give constraints that can never be satisfied, they will return +¶

and -¶ as the minima and maxima, and Indeterminate as the values of variables.

One subtle issue is that Minimize and Maximize allow both nonstrict inequalities of the form x <= v, and strict ones of the form x < v. With nonstrict inequalities there is no problem with a minimum or maximum lying exactly on the boundary x -> v. But with strict inequalities, a mini-mum or maximini-mum must in principle be at least infinitesimally inside the boundary.

With a strict inequality, Mathematica prints a warning, then returns the point on the boundary.

In[13]:= Minimize@8x ^ 2 - 3 x + 6, x > 3<, xD

Minimize::wksol : Warning: There is no minimum in the

region described by the contraints; returning a result on the boundary.

Out[13]= 86, 8x Ø 3<<

Minimize and Maximize normally assume that all variables you give are real. But by giving a constraint such as x œ Integers you can specify that a variable must in fact be an integer.

-10 -5 5 10

-10 -5 5 10

This does maximization only over integer values of x and y.

In[14]:= Maximize@8x y, x ^ 2 + y ^ 2 < 120 && Hx yL œ Integers<, 8x, y<D

Out[14]= 856, 8x Ø -8, y Ø -7<<

Minimize and Maximize can compute maxima and minima of linear functions over the integers in bounded polyhedra. This is known as integer linear programming.

This does maximization over integer values of x and y in a triangle.

In[15]:= Maximize@85 + 3 y + 7 x, x >= 0 && y >= 0 && 3 x + 4 y <= 100 && Hx yL œ Integers<, 8x, y<D

Out[15]= 8236, 8x Ø 33, y Ø 0<<

Minimize and Maximize can produce exact symbolic results for polynomial optimization prob-lems with parameters.

This finds the minimum of a general quadratic polynomial.

In[16]:= Minimize@a x ^ 2 + b x + c, xD

Out[16]=

MinValue@8 f ,cons<,8x,y,…<D give the minimum value of f subject to the constraints cons MaxValue@8 f ,cons<,8x,y,…<D give the maximum value of f subject to the constraints cons ArgMin@8 f ,cons<,8x,y,…<D give a position at which f is minimized subject to the

constraints cons

ArgMax@8 f ,cons<,8x,y,…<D give a position at which f is maximized subject to the constraints cons

Computing values and positions of minima and maxima.

Maximize gives both the value and the position of a maximum.

In[17]:= Maximize@8x + 2 y, x ^ 2 + y ^ 2 § 1<, 8x, y<D

Out[17]= : 5 , :x Ø - 4 5

+ 5 , y Ø 2 5

>>

Use MaxValue if you only need the maximum value.

In[18]:= MaxValue@8x + 2 y, x ^ 2 + y ^ 2 § 1<, 8x, y<D

Out[18]= 5

For strict polynomial inequality constraints computing only the maximum value may be much faster.

In[19]:= TimeConstrained@

Maximize@8- x ^ 2 + 2 x y - z - 1, x ^ 2 y < z ^ 3 && x - z ^ 2 > y ^ 2 + 2<, 8x, y, z<D, 300D

Out[19]= $Aborted

In[20]:= MaxValue@8- x ^ 2 + 2 x y - z - 1, x ^ 2 y < z ^ 3 && x - z ^ 2 > y ^ 2 + 2<, 8x, y, z<D êê Timing

Out[20]= 90.312, 1 RootA6 674 484 057 677 824 + 27 190 416 613 703 680 Ò1

-9 845 871 213 2-97 -967 104 Ò12+ 30 310 812 947 042 320 384 Ò13- 38 968 344 650 849 575 680 Ò14+ 27 943 648 095 748 511 616 Ò15- 13 622 697 129 083 140 957 Ò16+ 5 905 344 357 450 294 480 Ò17 -2 87-2 859 681 1-27 -251 4-24 Ò18+ 1 484 592 492 626 145 792 Ò19- 567 863 224 101 551 360 Ò110+ 100 879 538 475 737 088 Ò111+ 303 891 741 605 888 Ò112- 2 224 545 911 472 128 Ò113+

70 301 735 976 960 Ò114+ 25 686 756 556 800 Ò115+ 1 786 706 395 136 Ò116+ 73 014 444 032 Ò117&, 1E=

ArgMax gives a position at which the maximum value is attained.

In[21]:= ArgMax@8x + 2 y, x ^ 2 + y ^ 2 § 1<, 8x, y<D

Out[21]= :- 4 5

+ 5 , 2

5

>

In document Mathematica Book.pdf (Page 151-155)