searches for a global maximum in f numerically with respect to x.


searches for a global maximum in f numerically with respect to x, y, .


searches for a global maximum in f numerically subject to the constraints cons.


constrains x to be in the region or domain rdom.

Details and Options

  • NMaximize is also known as global optimization (GO).
  • NMaximize always attempts to find a global maximum of f subject to the constraints given.
  • NMaximize is typically used to find the largest possible values given constraints. In different areas, this may be called the best strategy, best fit, best configuration and so on.
  • NMaximize returns a list of the form {fmax,{x->xmax,y->ymax,}}.
  • If f is linear or concave and cons are linear or convex, the result given by NMaximize will be the global maximum, over both real and integer values; otherwise, the result may sometimes only be a local maximum.
  • If NMaximize determines that the constraints cannot be satisfied, it returns {Infinity,{x->Indeterminate,}}.
  • NMaximize supports a modeling language where the objective function f and constraints cons are given in terms of expressions depending on scalar or vector variables. f and cons are typically parsed into very efficient forms, but as long as f and the terms in cons give numerical values for numerical values of the variables, NMaximize can often find a solution.
  • The constraints cons can be any logical combination of:
  • lhs==rhsequations
    lhs>rhs, lhsrhs, lhs<rhs, lhsrhsinequalities (LessEqual, )
    lhsrhs, lhsrhs, lhsrhs, lhsrhsvector inequalities (VectorLessEqual, )
    {x,y,}rdomregion or domain specification
  • NMaximize[{f,cons},xrdom] is effectively equivalent to NMaximize[{f,cons&&xrdom},x].
  • For xrdom, the different coordinates can be referred to using Indexed[x,i].
  • Possible domains rdom include:
  • Realsreal scalar variable
    Integersinteger scalar variable
    Vectors[n,dom]vector variable in
    Matrices[{m,n},dom]matrix variable in
    vector variable restricted to the geometric region
  • By default, all variables are assumed to be real.
  • The following options can be given:
  • AccuracyGoalAutomaticnumber of digits of final accuracy sought
    EvaluationMonitor Noneexpression to evaluate whenever f is evaluated
    MaxIterationsAutomaticmaximum number of iterations to use
    Method Automaticmethod to use
    PrecisionGoalAutomaticnumber of digits of final precision sought
    StepMonitor Noneexpression to evaluate whenever a step is taken
    WorkingPrecision MachinePrecisionthe precision used in internal computations
  • The settings for AccuracyGoal and PrecisionGoal specify the number of digits to seek in both the value of the position of the maximum, and the value of the function at the maximum.
  • NMaximize continues until either of the goals specified by AccuracyGoal or PrecisionGoal is achieved.
  • The methods for NMaximize fall into two classes. The first class of guaranteed methods uses properties of the problem so that, when the method converges, the maximum found is guaranteed to be global. The second class of heuristic methods uses methods that may include multiple local searches, commonly adjusted by some stochasticity, to home in on a global maximum. These methods often do find the global maximum, but are not guaranteed to do so.
  • Methods that are guaranteed to give a global maximum when they converge to a solution include:
  • "Convex"use only convex methods
    "MOSEK"use the commercial MOSEK library for convex problems
    "Gurobi"use the commercial Gurobi library for convex problems
    "Xpress"use the commercial Xpress library for convex problems
  • Heuristic methods include:
  • "NelderMead"simplex method of Nelder and Mead
    "DifferentialEvolution"use differential evolution
    "SimulatedAnnealing"use simulated annealing
    "RandomSearch"use the best local minimum found from multiple random starting points
    "Couenne"use the Couenne library for non-convex mixed-integer nonlinear problems


open allclose all

Basic Examples  (3)

Find the global maximum of an unconstrained problem:

Extract the maximizing argument:

Find the global maximum of problems with constraints:

Maximize a function over a geometric region:

Plot it:

Scope  (35)

Basic Uses  (11)

Maximize subject to constraints :

Several linear inequality constraints can be expressed with VectorGreaterEqual:

Use v>= or \[VectorGreaterEqual] to enter the vector inequality sign :

An equivalent form using scalar inequalities:

Use a vector variable :

The inequality may not be the same as due to possible threading in :

To avoid unintended threading in , use Inactive[Plus]:

Use constant parameter equations to avoid unintended threading in :

VectorGreaterEqual represents a conic inequality with respect to the "NonNegativeCone":

To explicitly specify the dimension of the cone, use {"NonNegativeCone",n}:

Find the solution:

Maximize subject to the constraint :

Specify the constraint using a conic inequality with "NormCone":

Find the solution:

Maximize the function subject to the constraint :

Use Indexed to access components of a vector variable, e.g. TemplateBox[{x, 1}, IndexedDefault]:

Use Vectors[n,dom] to specify the dimension and domain of a vector variable when it is ambiguous:

Specify non-negative constraints using NonNegativeReals (TemplateBox[{}, NonNegativeReals]):

An equivalent form using vector inequality :

Specify non-positive constraints using NonPositiveReals (TemplateBox[{}, NonPositiveReals]):

An equivalent form using vector inequalities:

Domain Constraints  (5)

Specify integer domain constraints using Integers:

Specify integer domain constraints on vector variables using Vectors[n,Integers]:

Specify non-negative integer domain constraints using NonNegativeIntegers (TemplateBox[{}, NonNegativeIntegers]):

Specify non-positive integer domain constraints using NonPositiveIntegers (TemplateBox[{}, NonPositiveIntegers]):

Or constraints can be specified:

Region Constraints  (5)

Maximize over a region:

Plot it:

Find the maximum possible distance between two points that are constrained to be in two different regions:

Plot it:

Find the maximum such that the triangle and ellipse still intersect:

Plot it:

Find the circle of maximum radius that contains the given three points:

Plot it:

Using Circumsphere gives the same result directly:

Use to specify that is a vector in with TemplateBox[{x}, Norm]=1:

Linear Problems  (5)

With linear objectives and constraints, when a maximum is found it is global:

The constraints can be equality and inequality constraints:

Use Equal to express several equality constraints at once:

An equivalent form using several scalar equalities:

Use VectorLessEqual to express several LessEqual inequality constraints at once:

Use v<= to enter the vector inequality in a compact form:

An equivalent form using scalar inequalities:

Use Interval to specify bounds on variable:

Convex Problems  (3)

Use "NonNegativeCone" to specify linear functions of the form :

Use v>= to enter the vector inequality in a compact form:

Minimize such that is positive semidefinite:

Show the maximizer on a plot of the objective function:

Minimize the concave objective function such that is positive semidefinite and :

Plot the region and the maximizing point:

Transformable to Convex  (3)

Maximize the quasi-concave function subject to inequality and norm constraints. The objective is quasi-concave because it is a product of two non-negative affine functions:

The maximization is solved by minimizing the negative of the objective, , that is quasi-convex. Quasi-convex problems can be solved as a parametric convex optimization problem for the parameter :

Plot the objective as a function of the level-set :

For a level-set value between the interval , the smallest objective is found:

The problem becomes infeasible when the level-set value is increased:

Maximize subject to the constraint . The objective is not convex but can be represented by a difference of convex function where and are convex functions:

Plot the region and the minimizing point:

Maximize subject to the constraints . The constraint is not convex but can be represented by a difference of convex constraint where and are convex functions:

Plot the region and the minimizing point:

General Problems  (3)

Maximize a linear objective subject to nonlinear constraints:

Plot it:

Maximize a nonlinear objective subject to linear constraints:

Plot the objective and the maximizing point:

Maximize a nonlinear objective subject to nonlinear constraints:

Plot it:

Options  (6)

AccuracyGoal & PrecisionGoal  (2)

This enforces convergence criteria and :

This enforces convergence criteria and , which is not achievable with the default machine-precision computation:

Setting a high WorkingPrecision makes the process convergent:

EvaluationMonitor  (1)

Record all the points evaluated during the solution process of a function with a ring of minima:

Plot all the visited points that are close in objective function value to the final solution:

Method  (1)

Some methods may give suboptimal results for certain problems:

The automatically chosen method gives the optimal solution for this problem:

Plot the solution and show the global maxima:

StepMonitor  (1)

Steps taken by NMaximize in finding the maximum of a function:

WorkingPrecision  (1)

With the working precision set to , by default AccuracyGoal and PrecisionGoal are set to :

Applications  (8)

Geometry Problems  (4)

Find the half-lengths of the principal axes that maximize the volume of an ellipsoid with a surface area of at most 1:

The surface area can be approximated by:

Maximize the volume area by minimizing its reciprocal:

This is the sphere. Including additional constraints on the axes lengths changes this:

Plot the resulting ellipsoid:

Find the analytic center of a convex polygon. The analytic center is a point that maximizes the product of distances to the constraints:

Each segment of the convex polygon can be represented as intersections of half-planes . Extract the linear inequalities:

The objective is to maximize . Since the logarithm function is monotonic, this is equivalent to maximizing :

Solve the unconstrained problem:

Visualize the location of the center:

Find the maximum-area ellipse parametrized as that can be fitted into a convex polygon:

Each segment of the convex polygon can be represented as intersections of half-planes . Extract the linear inequalities:

Applying the parametrization to the half-planes gives . The term . Thus, the constraints are:

Maximizing the area is equivalent to maximizing :

Convert the parametrized ellipse into the explicit form as :

Find the largest radius for non-overlapping circles and their centers that can be contained in a square. The box constraint can be represented as :

The circles must not overlap:

Collect the variables:

Find the maximum radius and the circle centers :

Plot the circles and the bounding box:

Compute the fraction of square covered by the circles:

Portfolio Optimization  (1)

Find the distribution of capital to invest in six stocks to maximize return while minimizing risk:

The return is given by , where is a vector of the expected return value of each individual stock:

The risk is given by ; is a risk-aversion parameter and :

The objective is to maximize return while minimizing risk for a specified risk-aversion parameter:

The effect on market prices of stocks due to the buying and selling of stocks is modeled by , which is modeled by a power cone using the epigraph transformation:

The weights must all be greater than 0 and the weights plus market impact costs must add to 1:

Compute the returns and corresponding risk for a range of risk-aversion parameters:

The optimal over a range of gives an upper-bound envelope on the tradeoff between return and risk:

Compute the weights for a specified number of risk-aversion parameters:

By accounting for the market costs, a diversified portfolio can be obtained for low risk aversion, but when the risk aversion is high, the market impact cost dominates, due to purchasing a less diversified stock:

Investment Problems  (2)

Find the allocation of a maximum of $250,000 of capital to purchase two stocks and a bond such that the return on investment is maximized. Let be the amount to invest in the two stocks and let be the amount to invest in the bond:

The amount invested in the utilities stock cannot be more than $40,000:

The amount invested in the bond must be at least $70,000:

The total amount invested in the two stocks must be at least half the total amount invested:

The stocks pay an annual dividend of 9% and 4%, respectively. The bond pays a dividend of 5%. The total return on investment is:

The cost with purchasing the stocks and bonds and executing the transactions is :

The optimal amount to be invested can be found by maximizing the profit while minimizing cost:

The total amount invested and the annual dividends received from the investments are:

Find the optimal combination of investments that yields maximum profit. The net present value and the costs associated with each investment are:

Let be a decision variable such that if investment is selected. The objective is to maximize profits while minimizing costs:

There is a maximum $14,000 available for investing:

Solve the maximization problem to get the optimal combination of investments:

Traveling Salesman Problem  (1)

Find the path that a salesman should take through cities such that each city is only visited once, travel cost savings are maximized and distance traveled is minimized. Generate the locations:

Let be the distance between city and city . Let be a decision variable such that if , the path goes from city to city :

The travel budget to go from one city to another is $15. If two cities are within 50 miles, the salesman pays $5; otherwise, he pays a $10 flat rate. The total savings are:

The objective is to maximize savings while minimizing distance:

The salesman can arrive from exactly one city and can depart to exactly one other city:

The salesman cannot arrive at one city and depart to the same city:

The salesman must travel to all the locations in a single tour:

The decision variable is a binary variable and the dummy variable is :

Find the path that minimizes the distance:

Extract the path:

The distance traveled is:

The total savings is:

Properties & Relations  (6)

NMaximize gives the maximum value and rules for the maximizing values of the variables:

NArgMax gives a list of the maximizing values:

NMaxValue gives only the maximum value:

Maximizing a function f is equivalent to minimizing -f:

For convex problems, ConvexOptimization may be used to obtain additional solution properties:

Get the dual solution for the minimization problem:

For convex problems with parameters, using ParametricConvexOptimization gives a ParametricFunction:

The ParametricFunction may be evaluated for values of the parameter:

Define a function for the parametric problem using NMaximize:

Compare the speeds of the two approaches:

Derivatives of the ParametricFunction can also be computed:

For convex problems with parametric constraints, RobustConvexOptimization finds an optimum that works for all possible values of the parameters:

NMaximize may find a larger value for particular values of the parameters:

This maximizer does not satisfy the constraints for all allowed values of and :

The maximum value found for particular values of the parameters is greater than or equal to the robust minimum:

NMaximize aims to find a global maximum, while FindMaximum attempts to find a local maximum:

Maximize finds a global maximum and can work in infinite precision:

Possible Issues  (4)

For nonlinear functions, NMaximize may sometimes find only a local maximum for certain methods:

Plot the local maxima:

Specifying a starting interval can help in achieving a better local maximum:

NMaximize finds a local maximum of a two-dimensional function on a disk for certain methods:

Specifying a starting interval helps in achieving the global maximum:

NMaximize finds the global solution using the automatic method for this problem:

Use RegionBounds to compute the bounding box:

Use NMaximize and NMinimize to compute the same bounds:

Define a function that does numerical integration for a given parameter:

Compute with a parameter value of 2:

Applying the function to a symbolic parameter generates a message from NIntegrate:

This can also lead to warnings when the function is used with other numerical functions like NMaximize:

Define a function that only evaluates when its argument is a numerical value to avoid these messages:

Compute with a numerical value:

The function does not evaluate when its argument is non-numerical:

This function can now be used with other numerical functions such as NMaximize:

Wolfram Research (2003), NMaximize, Wolfram Language function, https://reference.wolfram.com/language/ref/NMaximize.html (updated 2022).


Wolfram Research (2003), NMaximize, Wolfram Language function, https://reference.wolfram.com/language/ref/NMaximize.html (updated 2022).


Wolfram Language. 2003. "NMaximize." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/NMaximize.html.


Wolfram Language. (2003). NMaximize. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NMaximize.html


@misc{reference.wolfram_2024_nmaximize, author="Wolfram Research", title="{NMaximize}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/NMaximize.html}", note=[Accessed: 17-June-2024 ]}


@online{reference.wolfram_2024_nmaximize, organization={Wolfram Research}, title={NMaximize}, year={2022}, url={https://reference.wolfram.com/language/ref/NMaximize.html}, note=[Accessed: 17-June-2024 ]}