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

x3+xy3y2

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 fx and fy. 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

(3x2+y, x6y)

We can set both equal to zero and solve those equations simultaneously as follows. In other words, SymPy will solve 3x2+y=0 and x6y=0 simultaneously for us.

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

[{x:118, y:1108}, {x:0, y:0}]

That output indicates two critical numbers, one at (118,1108) 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=fxxfyyfxy2.

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

36x1

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 fxx<0 then the critical point is a maximum.
  • If D>0 and fxx>0 then the critical point is a minimum.

Let’s begin by checking (118,1108).

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

1.0

Since D>0 we must check fxx.

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

0.333333333333333

Because fxx<0, the point (118,1108) is a maximum. Now we check (0,0).

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

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)