Minimization and Maximization

Minimization and maximization.

(RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/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:

Minimize[x^2-3x+6,x]

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

x^2-3x+6/.Last[%]

This maximizes with respect to x and y:

Maximize[5 x y-x^4-y^4,{x,y}]

(RefLink[Minimize, paclet:ref/Minimize])[expr,x] minimizes expr allowing x to range over all possible values from -∞ to +∞. (RefLink[Minimize, paclet:ref/Minimize])[{expr,cons},x] 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:

Minimize[{x^2-3x+6,x>=3},x]

This finds the maximum within the unit circle:

Maximize[{5 x y-x^4-y^4,x^2+y^2<=1},{x,y}]

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

Maximize[{5 x y-x^4-y^4,x^2+2y^2<=1},{x,y}]

This finds the maximum along a line:

Maximize[{5 x y-x^4-y^4,x+y==1},{x,y}]

(RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/Maximize]) can solve any linear programming problem in which both the objective function expr and the constraints cons involve the variables (Subscript[x, i]) only linearly.

Here is a typical linear programming problem:

Minimize[{x+3 y,x-3 y<=7&&x+2y>=10},{x,y}]

They can also in principle solve any polynomial programming problem in which the objective 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:

Maximize[{x y,x+y==1},{x,y}]

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

Maximize[{8 x y z,x^2+y^2+z^2<=1},{x,y,z}]

An important feature of (RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/Maximize]) is that they always find global minima and maxima. Often functions will have various local minima and maxima at which derivatives vanish. But (RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/Maximize]) use global methods to find absolute minima or maxima, not just local extrema.

Here is a function with many local maxima and minima:

Plot[x+2 Sin[x],{x,-10,10}]

(RefLink[Maximize, paclet:ref/Maximize]) finds the global maximum:

Maximize[{x+2 Sin[x],-10<=x<=10},x]

If you give functions that are unbounded, (RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/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 (RefLink[Indeterminate, paclet:ref/Indeterminate]) as the values of variables.

One subtle issue is that (RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/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 minimum or maximum must in principle be at least infinitesimally inside the boundary.

With a strict inequality, the Wolfram Language prints a warning, then returns the point on the boundary:

Minimize[{x^2-3x+6,x>3},x]

(RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/Maximize]) normally assume that all variables you give are real. But by giving a constraint such as x∈(RefLink[Integers, paclet:ref/Integers]) you can specify that a variable must in fact be an integer.

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

Maximize[{x y,x^2+y^2<120&&(x|y) ∈ Integers},{x,y}]

(RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/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:

Maximize[{5+3y+7x,x>=0&&y>=0&&3x+4y<=100&&(x|y) ∈ Integers},{x,y}]

(RefLink[Minimize, paclet:ref/Minimize]) and (RefLink[Maximize, paclet:ref/Maximize]) can produce exact symbolic results for polynomial optimization problems with parameters.

This finds the minimum of a general quadratic polynomial:

Minimize[a x^2+b x+c,x]

Computing values and positions of minima and maxima.

(RefLink[Maximize, paclet:ref/Maximize]) gives both the value and the position of a maximum:

Maximize[{x+2y,x^2+y^2≤1},{x,y}]

Use (RefLink[MaxValue, paclet:ref/MaxValue]) if you only need the maximum value:

MaxValue[{x+2y,x^2+y^2≤1},{x,y}]

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

TimeConstrained[Maximize[{-x^2+2x y-z-1,x^2 y<z^3&&x-z^2>y^2+2},{x,y,z}],300]

MaxValue[{-x^2+2x y-z-1,x^2 y<z^3&&x-z^2>y^2+2},{x,y,z}]//Timing

(RefLink[ArgMax, paclet:ref/ArgMax]) gives a position at which the maximum value is attained:

ArgMax[{x+2y,x^2+y^2≤1},{x,y}]