Numerical Optimization

FindMinimum[f,{x,x0}]search for a local minimum of f, starting at
FindMinimum[f,x]search for a local minimum of f
FindMinimum[f,{{x,x0},{y,y0},...}]
search for a local minimum in several variables
FindMinimum[{f,cons},{{x,x0},{y,y0},...}]
search for a local minimum subject to the constraints cons starting at , , ...
FindMinimum[{f,cons},{x,y,...}]search for a local minimum subject to the constraints cons
FindMaximum[f,x], etc.search for a local maximum

Searching for local minima and maxima.

This finds the value of which minimizes , starting at .
In[1]:=
Click for copyable input
Out[1]=
The last element of the list gives the value at which the minimum is achieved.
In[2]:=
Click for copyable input
Out[2]=

Like FindRoot, FindMinimum and FindMaximum work by starting from a point, then progressively searching for a minimum or maximum. But since they return a result as soon as they find anything, they may give only a local minimum or maximum of your function, not a global one.

This curve has two local minima.
In[3]:=
Click for copyable input
Out[3]=
Starting at , you get the local minimum on the right.
In[4]:=
Click for copyable input
Out[4]=
This gives the local minimum on the left, which in this case is also the global minimum.
In[5]:=
Click for copyable input
Out[5]=
You can specify variables without initial values.
In[6]:=
Click for copyable input
Out[6]=
You can specify a constraint.
In[7]:=
Click for copyable input
Out[7]=
NMinimize[f,x]try to find the global minimum of f
NMinimize[f,{x,y,...}]try to find the global minimum over several variables
NMaximize[f,x]try to find the global maximum of f
NMaximize[f,{x,y,...}]try to find the global maximum over several variables

Finding global minima and maxima.

This immediately finds the global minimum.
In[8]:=
Click for copyable input
Out[8]=

NMinimize and NMaximize are numerical analogs of Minimize and Maximize. But unlike Minimize and Maximize they usually cannot guarantee to find absolute global minima and maxima. Nevertheless, they typically work well when the function f is fairly smooth, and has a limited number of local minima and maxima.

NMinimize[{f,cons},{x,y,...}]try to find the global minimum of f subject to constraints cons
NMaximize[{f,cons},{x,y,...}]try to find the global maximum of f subject to constraints cons

Finding global minima and maxima subject to constraints.

With the constraint , NMinimize will give the local minimum on the right.
In[9]:=
Click for copyable input
Out[9]=
This finds the minimum of within the unit circle.
In[10]:=
Click for copyable input
Out[10]=
In this case Minimize can give an exact result.
In[11]:=
Click for copyable input
Out[11]=
But in this case it cannot.
In[12]:=
Click for copyable input
Out[12]=
This gives a numerical approximation, effectively using NMinimize.
In[13]:=
Click for copyable input
Out[13]=

If both the objective function f and the constraints cons are linear in all variables, then minimization and maximization correspond to a linear programming problem. Sometimes it is convenient to state such problems not in terms of explicit equations, but instead in terms of matrices and vectors.

LinearProgramming[c,m,b]find the vector which minimizes subject to the constraints and
LinearProgramming[c,m,b,l]use the constraints and

Linear programming in matrix form.

Here is a linear programming problem in equation form.
In[14]:=
Click for copyable input
Out[14]=
Here is the corresponding problem in matrix form.
In[15]:=
Click for copyable input
Out[15]=

You can specify a mixture of equality and inequality constraints by making the list be a sequence of pairs . If is , then the ^(th) constraint is . If is then it is , and if is then it is .

This makes the first inequality use .
In[16]:=
Click for copyable input
Out[16]=

In LinearProgramming[c, m, b, l], you can make be a list of pairs representing lower and upper bounds on the .

In doing large linear programming problems, it is often convenient to give the matrix as a SparseArray object.

New to Mathematica? Find your learning path »
Have a question? Ask support »