Link Search Menu Expand Document (external link)

How to find the critical points of a multivariate function (in Python, using SymPy)

See all solutions.

Task

When trying to find the maximum and minimum values of a multivariate function, that is a function of multiple real-valued inputs, one of the main techniques in calculus is to use the “critical points” of the function, which are the most important inputs to examine to find maxima and minima. Can we find critical points for a multivariate function using software?

Related tasks:

Solution

This answer assumes you have imported SymPy as follows.

1
2
from sympy import *                   # load all math functions
init_printing( use_latex='mathjax' )  # use pretty math output

Let’s create an example function to work with.

1
2
3
var( 'x y' )
formula = x**3 + x*y - 3*y**2
formula

$\displaystyle x^{3} + x y - 3 y^{2}$

Critical numbers come in two kinds. First, where are all partial derivatives zero? Second, where is a derivative undefined but the function is defined?

Let’s begin by finding where both partial derivatives are zero. Recall that a common notation for the partial derivatives is $f_x$ and $f_y$. We’ll use the same techniques introduced in how to write symbolic equations and how to solve symbolic equations.

1
2
3
f_x = diff( formula, x )
f_y = diff( formula, y )
f_x, f_y

$\displaystyle \left( 3 x^{2} + y, \ x - 6 y\right)$

We can set both equal to zero and solve those equations simultaneously as follows. In other words, SymPy will solve $3x^2+y=0$ and $x-6y=0$ simultaneously for us.

1
solve( [ f_x, f_y ] )   # that is, f_x=0 and f_y=0

$\displaystyle \left[ \left\{ x : - \frac{1}{18}, \ y : - \frac{1}{108}\right\}, \ \left\{ x : 0, \ y : 0\right\}\right]$

That output indicates two critical numbers, one at $\left(-\frac{1}{18},-\frac{1}{108}\right)$ and one at $(0,0)$.

Now where is the derivative defined but the function undefined? Unfortunately, SymPy cannot help us compute the domain of multivariate functions. You will need to do that yourself. (In this case, the function is defined for all real values of $x$ and $y$.)

But SymPy can help us classify the two critical numbers above. Are they maxima, minima, or saddle points? We use the discriminant, built from the second partial derivatives, $D=f_{xx}f_{yy}-f_{xy}^2$.

1
2
3
4
5
f_xx = diff( formula, x, x )
f_yy = diff( formula, y, y )
f_xy = diff( formula, x, y )
D = f_xx*f_yy - f_xy**2
D

$\displaystyle - 36 x - 1$

We can then evaluate the discriminant on our critical points. Recall the following rules from multivariate calculus:

  • If $D<0$ then the critical point is a saddle point.
  • If $D>0$ and $f_xx<0$ then the critical point is a maximum.
  • If $D>0$ and $f_xx>0$ then the critical point is a minimum.

Let’s begin by checking $\left(-\frac{1}{18},-\frac{1}{108}\right)$.

1
D.subs( x, -1/18 ).subs( y, -1/108 )

$\displaystyle 1.0$

Since $D>0$ we must check $f_{xx}$.

1
f_xx.subs( x, -1/18 ).subs( y, -1/108 )

$\displaystyle -0.333333333333333$

Because $f_{xx}<0$, the point $\left(-\frac{1}{18},-\frac{1}{108}\right)$ is a maximum. Now we check $(0,0)$.

1
D.subs( x, 0 ).subs( y, 0 )

$\displaystyle -1$

Because $D<0$, the point $(0,0)$ is a saddle point.

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by Nathan Carter (ncarter@bentley.edu)