TExcelTAD
Home › Calculus in Depth › Functions of Several Variables

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 ff is a rule that assigns to each ordered pair (x,y)(x, y) in a set D⊆R2D \subseteq \mathbb{R}^2 exactly one real number f(x,y)f(x, y).

Term Meaning
domain DD the set of input pairs where the rule makes sense (or that we choose)
range the set {f(x,y):(x,y)∈D}\{f(x, y) : (x, y) \in D\}
graph the surface {(x,y,z):z=f(x,y), (x,y)∈D}\{(x, y, z) : z = f(x, y),\ (x, y) \in D\} in R3\mathbb{R}^3
level curve f(x,y)=kf(x, y) = k the set of points of DD where ff takes the value kk
level surface F(x,y,z)=kF(x, y, z) = k 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:

gneeds g≥0ln⁡(g)needs g>01/gneeds g≠0arcsin⁡(g)needs −1≤g≤1\begin{aligned} &\sqrt{g} && \text{needs } g \ge 0 \\ &\ln(g) && \text{needs } g > 0 \\ &1/g && \text{needs } g \ne 0 \\ &\arcsin(g) && \text{needs } -1 \le g \le 1 \end{aligned}

Example: f(x,y)=ln⁡(9−x2−y2)/y−xf(x, y) = \ln(9 - x^2 - y^2) / \sqrt{y - x}. The logarithm needs x2+y2<9x^2 + y^2 < 9 (the open disk of radius 3), and the root in the denominator needs y>xy > x. The domain is the part of that open disk strictly above the line y=xy = x. Its range is R\mathbb{R}: near the line y=xy = x the denominator goes to 0+0^+ while the numerator can be positive or negative, so values of every size and sign occur.

Graphs, traces and level curves

The graph of f(x,y)f(x, y) is a surface, which is hard to draw by hand. Two tools tame it:

  • Traces: fix x=ax = a (or y=by = b) and look at the curve z=f(a,y)z = f(a, y). This turns a surface into a family of ordinary one-variable graphs.
  • Level curves (contours): the curves f(x,y)=kf(x, y) = k drawn in the xyxy-plane for equally spaced values kk. Where contours crowd together, the surface is steep; where they spread out, it is flat. A topographic map is exactly this.
f(x,y)f(x, y) level curves f=kf = k shape of graph
2x−y+12x - y + 1 parallel lines a plane
x2+y2x^2 + y^2 circles of radius k\sqrt{k} paraboloid opening up
x2+y2\sqrt{x^2 + y^2} circles of radius kk, equally spaced cone
x2−y2x^2 - y^2 hyperbolas (and two crossing lines at k=0k = 0) saddle
e−(x2+y2)e^{-(x^2 + y^2)} circles, crowded at mid radius a "bump"

Compare the paraboloid and the cone: both have circular contours, but for the paraboloid the circles at k=1,2,3,4k = 1, 2, 3, 4 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).

python · try it yourself
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 (−2,0)(-\sqrt{2}, 0) and a pit near (0,−2)(0, -\sqrt{2}); by symmetry there are also a peak at (2,0)(\sqrt{2}, 0) and a pit at (0,2)(0, \sqrt{2}), all with ∣f∣=2/e≈0.7358\lvert f \rvert = 2/e \approx 0.7358. The grid only sees its nearest sample (x=−1.42x = -1.42 instead of −1.4142-1.4142, value 0.7357); calculus will find the exact points in Maximum and Minimum Values.

Three or more variables

For f(x,y,z)f(x, y, z) the graph lives in R4\mathbb{R}^4, so we cannot draw it. Instead we draw level surfaces F(x,y,z)=kF(x, y, z) = k. For F=x2+y2+z2F = x^2 + y^2 + z^2 they are concentric spheres; for F=x2+y2−z2F = x^2 + y^2 - z^2 they are hyperboloids of one sheet (k>0k > 0), a cone (k=0k = 0) or hyperboloids of two sheets (k<0k < 0), the quadric surfaces of the previous section.

With nn variables we write x=(x1,…,xn)x = (x_1, \ldots, x_n) and f(x)f(x). Linear functions f(x)=c⋅x+bf(x) = c \cdot x + b and quadratic forms f(x)=xTAxf(x) = x^T A x are the two families you will meet constantly; the rest of this chapter extends derivatives to all of them.

python · try it yourself
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 L(w1,…,wn)L(w_1, \ldots, w_n) is a function of millions of variables. People visualise it by restricting it to a plane through the trained weights, L(w∗+αu+βv)L(w^* + \alpha u + \beta v), and drawing level curves in (α,β)(\alpha, \beta): exactly the contour plots of this lesson.
  • Heatmaps and terrain. A heightmap stores z=f(x,y)z = f(x, y) on a grid; a game's terrain shader or a GIS tool draws its contours. Signed distance functions F(x,y,z)F(x, y, z) in ray marchers define shapes as the level surface F=0F = 0.
  • Vectorised evaluation. np.meshgrid (or broadcasting with x[:, None] and y[None, :]) evaluates ff 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 f(x,y)=4−x2−4y2+ln⁡(x)f(x, y) = \sqrt{4 - x^2 - 4y^2} + \ln(x).

Answer

The part of the filled ellipse x2/4+y2≤1x^2/4 + y^2 \le 1 with x>0x > 0 (the right half, without the yy-axis).

Solution
4−x2−4y2needs x2+4y2≤4  ⟺  x2/4+y2≤1(ellipse, boundary included)ln⁡(x)needs x>0\begin{aligned} &\sqrt{4 - x^2 - 4y^2} && \text{needs } x^2 + 4y^2 \le 4 \iff x^2/4 + y^2 \le 1 && \text{(ellipse, boundary included)} \\ &\ln(x) && \text{needs } x > 0 \end{aligned}

Both conditions must hold, so the domain is the right half of the closed elliptical region, minus the segment of the yy-axis.

Exercise 2: Range

Find the range of g(x,y)=5−x2+y2g(x, y) = 5 - \sqrt{x^2 + y^2} on the disk x2+y2≤16x^2 + y^2 \le 16.

Answer

[1,5][1, 5].

Solution

r=x2+y2r = \sqrt{x^2 + y^2} takes every value in [0,4][0, 4] on the disk, so g=5−rg = 5 - r takes every value from 5−4=15 - 4 = 1 (on the boundary circle) to 5−0=55 - 0 = 5 (at the origin).

Exercise 3: Reading contours

The level curves f=1,2,3,4f = 1, 2, 3, 4 of f(x,y)=x2+4y2f(x, y) = x^2 + 4y^2 are ellipses. Write the semi-axes of the curve f=kf = k and say whether the surface gets steeper or flatter as you move away from the origin.

Answer

Semi-axes k\sqrt{k} (along xx) and k/2\sqrt{k}/2 (along yy). The ellipses get closer together as kk grows, so the surface gets steeper.

Solution
x2+4y2=k  ⟺  x2k+y2k/4=1semi-axis along x: kalong y: k/2k=1,2,3,4→x-semi-axes 1,1.414,1.732,2\begin{aligned} &x^2 + 4y^2 = k \iff \frac{x^2}{k} + \frac{y^2}{k/4} = 1 \\ &\text{semi-axis along } x\text{: } \sqrt{k} \qquad \text{along } y\text{: } \sqrt{k}/2 \\ &k = 1, 2, 3, 4 \to x\text{-semi-axes } 1, 1.414, 1.732, 2 \end{aligned}

The gaps between consecutive xx-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 201×201201 \times 201 grid on [−2,2]2[-2, 2]^2, estimate the area of the part of that square where x2−y2>1x^2 - y^2 > 1 (fraction of grid points×area of the square\text{fraction of grid points} \times \text{area of the square}), rounded to 2 decimals.

Answer

About 4.32. (The exact area is 43−2ln⁡(2+3)≈4.2944\sqrt{3} - 2\ln(2 + \sqrt{3}) \approx 4.294; the grid over-counts slightly at the curved edges.)

Solution
python · try it yourself
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 ∫122x2−1 dx=23−ln⁡(2+3)≈2.147\int_1^2 2\sqrt{x^2 - 1}\,dx = 2\sqrt{3} - \ln(2 + \sqrt{3}) \approx 2.147, and the left piece is its mirror image.

Test yourself

What is the natural domain of f(x,y)=1/(x−y2)f(x, y) = 1/(x - y^2)?

  1. All of R2\mathbb{R}^2
  2. All points except the parabola x=y2x = y^2
  3. Points with x>y2x > y^2
  4. Points with x≥0x \ge 0

Contours of ff drawn at k=1,2,3,…k = 1, 2, 3, \ldots are very close together in one area. What does that mean?

  1. ff is nearly constant there
  2. ff is undefined there
  3. The graph of ff is steep there
  4. ff has a maximum there

Which function has level curves that are equally spaced concentric circles (for equally spaced k)?

  1. x2+y2x^2 + y^2
  2. x2+y2\sqrt{x^2 + y^2}
  3. x2−y2x^2 - y^2
  4. ex2+y2e^{x^2 + y^2}