Function Repository Resource:

LocalExtrema

Source Notebook

Compute the local extrema of an expression with respect to the given variables

Contributed by: Wolfram|Alpha Math Team

ResourceFunction["LocalExtrema"][expr,x]

computes the local maxima and minima of expr with respect to x.

ResourceFunction["LocalExtrema"][expr,{x,y,}]

computes the local maxima and minima of expr with respect to multiple variables.

ResourceFunction["LocalExtrema"][{expr,const},{x,y,}]

computes the local maxima and minima of expr subject to the constraint const.

Details and Options

ResourceFunction["LocalExtrema"] returns an association of the form <|"Maxima"{fmax,{xxmax,yymax,}},"Minima"{fmin,{xxmin,yymin,}}|>.
The const can contain equations, inequalities or logical combinations of these.
ResourceFunction["LocalExtrema"] only returns results when there is a bounded extremum.

Examples

Basic Examples (2) 

Compute the local extrema of a curve:

In[1]:=
ResourceFunction["LocalExtrema"][x (x - 1) (x + 1), x]
Out[1]=

Plot them:

In[2]:=
Plot[x (x - 1) (x + 1), {x, -2, 2}, Epilog -> {
Directive[Red, 
PointSize[Large]], 
Point[{1/Sqrt[3], -(2/(3 Sqrt[3]))}], 
Point[{-(1/Sqrt[3]), 2/(3 Sqrt[3])}]}]
Out[2]=

Use a constraint in order to reduce the domain upon which extrema can be found:

In[3]:=
ResourceFunction["LocalExtrema"][{x (x - 1) (x + 1), 0 < x < 2}, x]
Out[3]=

Scope (2) 

Compute the extrema of a function of two variables:

In[4]:=
ResourceFunction["LocalExtrema"][(4 - x^2 - y^2)^2, {x, y}]
Out[4]=

Compute the extrema of a piecewise function:

In[5]:=
ResourceFunction["LocalExtrema"][
 Piecewise[{{x, x <= 0}, {(x - 1)^2 - 4, x > 0}}], x]
Out[5]=

Plot the result:

In[6]:=
Plot[Piecewise[{{x, x <= 0}, {(x - 1)^2 - 4, x > 0}}], {x, -1, 2}, Epilog -> {Directive[PointSize[Large], Red], Point[{1, -4}], Point[{0, 0}]}]
Out[6]=

Possible Issues (1) 

LocalExtrema may return duplicate results for periodic functions:

In[7]:=
ResourceFunction["LocalExtrema"][Sin[x], x]
Out[7]=

Publisher

Wolfram|Alpha Math Team

Version History

  • 2.0.0 – 23 March 2023
  • 1.0.0 – 22 September 2020

Related Resources

Author Notes

To view the full source code for LocalExtrema, run the following code:

In[1]:=
FileNameJoin[
  ReplacePart[
   FileNameSplit[FindFile["ResourceFunctionHelpers`"]], -1 -> "FindExtrema.wl"]] // SystemOpen

License Information