Functions of Several Variables
A function of two variables assigns one number to each point of a region in the plane; a function of three variables does the same for points in space. This lesson makes domains, ranges, graphs, level curves and level surfaces precise, and shows how to look at such functions with contour plots and NumPy grids.
For a first look at the idea, see Functions of Several Variables in the survey course.
Definitions
A function of two variables is a rule that assigns to each ordered pair in a set exactly one real number .
| Term | Meaning |
|---|---|
| domain | the set of input pairs where the rule makes sense (or that we choose) |
| range | the set |
| graph | the surface in |
| level curve | the set of points of where takes the value |
| level surface | the same idea one dimension up |
When a formula is given without a domain, the natural domain is every point where the formula is defined as a real number. Typical restrictions:
Example: . The logarithm needs (the open disk of radius 3), and the root in the denominator needs . The domain is the part of that open disk strictly above the line . Its range is : near the line the denominator goes to while the numerator can be positive or negative, so values of every size and sign occur.
Graphs, traces and level curves
The graph of is a surface, which is hard to draw by hand. Two tools tame it:
- Traces: fix (or ) and look at the curve . This turns a surface into a family of ordinary one-variable graphs.
- Level curves (contours): the curves drawn in the -plane for equally spaced values . Where contours crowd together, the surface is steep; where they spread out, it is flat. A topographic map is exactly this.
| level curves | shape of graph | |
|---|---|---|
| parallel lines | a plane | |
| circles of radius | paraboloid opening up | |
| circles of radius , equally spaced | cone | |
| hyperbolas (and two crossing lines at ) | saddle | |
| circles, crowded at mid radius | a "bump" |
Compare the paraboloid and the cone: both have circular contours, but for the paraboloid the circles at have radii 1, 1.41, 1.73, 2 (getting closer, so steeper outward), while for the cone the radii are 1, 2, 3, 4 (constant slope).
import numpy as np
import matplotlib.pyplot as plt
f = lambda x, y: (x**2 - y**2) * np.exp(-(x**2 + y**2) / 2)
x = np.linspace(-3, 3, 241)
X, Y = np.meshgrid(x, x)
Z = f(X, Y)
i, j = np.unravel_index(np.argmax(Z), Z.shape)
print(f"grid max {Z.max():.4f} at ({X[i, j]:.2f}, {Y[i, j]:.2f})")
i, j = np.unravel_index(np.argmin(Z), Z.shape)
print(f"grid min {Z.min():.4f} at ({X[i, j]:.2f}, {Y[i, j]:.2f})")
plt.figure(figsize=(6, 4.5))
cs = plt.contour(X, Y, Z, levels=np.linspace(-0.7, 0.7, 15), cmap="RdBu_r")
plt.clabel(cs, fontsize=7)
plt.gca().set_aspect("equal")
plt.title("Level curves of (x² − y²)·e^(−(x²+y²)/2)")grid max 0.7357 at (-1.42, 0.00)
grid min -0.7357 at (0.00, -1.42)
The grid finds a peak near and a pit near ; by symmetry there are also a peak at and a pit at , all with . The grid only sees its nearest sample ( instead of , value 0.7357); calculus will find the exact points in Maximum and Minimum Values.
Three or more variables
For the graph lives in , so we cannot draw it. Instead we draw level surfaces . For they are concentric spheres; for they are hyperboloids of one sheet (), a cone () or hyperboloids of two sheets (), the quadric surfaces of the previous section.
With variables we write and . Linear functions and quadratic forms are the two families you will meet constantly; the rest of this chapter extends derivatives to all of them.
import numpy as np
# Temperature in a room, a function of three variables (°C)
def T(x, y, z):
return 20 + 3 * np.exp(-((x - 1)**2 + (y - 2)**2 + z**2)) - 0.5 * z
pts = [(1, 2, 0), (0, 0, 0), (1, 2, 2), (4, 4, 1)]
for p in pts:
print(p, round(float(T(*p)), 4))
# Vectorised over a grid: how much of the box [0,4]x[0,4]x[0,2] is above 21 °C?
g = np.linspace(0, 4, 81)
X, Y, Zs = np.meshgrid(g, g, np.linspace(0, 2, 41), indexing="ij")
print("fraction of grid points above 21 °C:", round(float(np.mean(T(X, Y, Zs) > 21)), 4))(1, 2, 0) 23.0
(0, 0, 0) 20.0202
(1, 2, 2) 19.0549
(4, 4, 1) 19.5
fraction of grid points above 21 °C: 0.0558
In software
- Loss functions. A neural network's loss is a function of millions of variables. People visualise it by restricting it to a plane through the trained weights, , and drawing level curves in : exactly the contour plots of this lesson.
- Heatmaps and terrain. A heightmap stores on a grid; a game's terrain shader or a GIS tool draws its contours. Signed distance functions in ray marchers define shapes as the level surface .
- Vectorised evaluation.
np.meshgrid(or broadcasting withx[:, None]andy[None, :]) evaluates on a whole grid at once, the standard way to tabulate and plot multivariable functions; see NumPy broadcasting.
Exercises
Exercise 1: Natural domain
Find and describe the domain of .
Answer
The part of the filled ellipse with (the right half, without the -axis).
Solution
Both conditions must hold, so the domain is the right half of the closed elliptical region, minus the segment of the -axis.
Exercise 2: Range
Find the range of on the disk .
Answer
.
Solution
takes every value in on the disk, so takes every value from (on the boundary circle) to (at the origin).
Exercise 3: Reading contours
The level curves of are ellipses. Write the semi-axes of the curve and say whether the surface gets steeper or flatter as you move away from the origin.
Answer
Semi-axes (along ) and (along ). The ellipses get closer together as grows, so the surface gets steeper.
Solution
The gaps between consecutive -semi-axes are 0.414, 0.318, 0.268: shrinking, so equal rises in height need shorter horizontal steps.
Exercise 4: Count grid points (code)
Using a grid on , estimate the area of the part of that square where (), rounded to 2 decimals.
Answer
About 4.32. (The exact area is ; the grid over-counts slightly at the curved edges.)
Solution
import numpy as np
g = np.linspace(-2, 2, 201)
X, Y = np.meshgrid(g, g)
frac = np.mean(X**2 - Y**2 > 1)
print(round(float(frac * 16), 2))4.32
Exactly: the right piece has area , and the left piece is its mirror image.
Test yourself
What is the natural domain of ?
- All of
- All points except the parabola
- Points with
- Points with
Contours of drawn at are very close together in one area. What does that mean?
- is nearly constant there
- is undefined there
- The graph of is steep there
- has a maximum there
Which function has level curves that are equally spaced concentric circles (for equally spaced k)?