This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Advanced Mathematics in Mathematica / Manipulating Equations and Inequalities /

3.4.12 Minimization and Maximization

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 - 3x + 6, x]


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

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


This maximizes with respect to x and y.

In[3]:= Maximize[5 x y - x^4 - y^4, {x, y}]


Minimize[expr, x] minimizes expr allowing x to range over all possible values from to . 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 .

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


This finds the maximum within the unit circle.

In[5]:= 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.

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


This finds the maximum along a line.

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


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

Here is a typical linear programming problem.

In[8]:= 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.

In[9]:= Maximize[{x y, x + y == 1}, {x, y}]


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

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


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 vanish. 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[x], {x, -10, 10}]


Maximize finds the global maximum.

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


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 non-strict inequalities of the form x <= v, and strict ones of the form x < v. With non-strict 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, Mathematica prints a warning, then returns the point on the boundary.

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


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.

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

In[14]:= Maximize[{x y, x^2 + y^2 < 120 &&
(x | y) Element Integers}, {x, y}]