TExcelTAD
Home › Calculus Essentials › Functions of Several Variables

Functions of Several Variables

Most real quantities depend on more than one input. The temperature on a metal plate depends on xx and yy, a loan payment depends on rate, term and amount, and a neural network's loss depends on millions of weights. This lesson covers how to picture such functions with level curves, and how limits and continuity work when there is more than one direction to approach from.

Inputs, outputs, domain

A function of two variables, f(x,y)f(x, y), takes a point of the plane and returns a number. Its domain is the set of points where the formula makes sense:

Function Domain Why
f(x,y)=x2+3xyf(x, y) = x^2 + 3xy all of R2\mathbb{R}^2 polynomials are defined everywhere
g(x,y)=9−x2−y2g(x, y) = \sqrt{9 - x^2 - y^2} x2+y2≤9x^2 + y^2 \le 9 (a disc) square root needs a non-negative input
h(x,y)=ln⁡(x−y)h(x, y) = \ln(x - y) x>yx > y (a half-plane) log needs a positive input
k(x,y)=1/(x2+y2)k(x, y) = 1 / (x^2 + y^2) everything except (0,0)(0, 0) no division by zero

With three inputs, f(x,y,z)f(x, y, z) assigns a number to each point of space, like the air temperature in a room. With nn inputs we write f(x1,…,xn)f(x_1, \ldots, x_n) or f(x)f(\mathbf{x}) with a vector x\mathbf{x}.

Graphs and level curves

The graph of f(x,y)f(x, y) is the surface z=f(x,y)z = f(x, y) from Surfaces in 3D. A flat picture is often more useful: the level curves (contours) f(x,y)=cf(x, y) = c for several values of cc. This is how a hiking map shows a mountain: closely spaced contours mean steep ground, widely spaced ones mean gentle slopes.

f(x,y)f(x, y) Level curves f=cf = c
x2+y2x^2 + y^2 circles centred at the origin
x+2yx + 2y parallel straight lines
x2−y2x^2 - y^2 hyperbolas (and two crossing lines at c=0c = 0)
x2+4y2x^2 + 4y^2 ellipses, squashed in yy

For f(x,y,z)f(x, y, z) the analogous objects are level surfaces, for example the spheres x2+y2+z2=cx^2 + y^2 + z^2 = c.

A loss surface

In machine learning, a model's loss is a function of its parameters. For a straight-line model y^=w⋅x+b\hat{y} = w \cdot x + b fitted to data, the mean squared error is a function of two variables, L(w,b)L(w, b). Its contour plot shows a valley whose lowest point is the best fit.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

x = np.array([0.0, 1.0, 2.0, 3.0, 4.0])
y = np.array([1.1, 2.9, 5.2, 7.1, 8.8])

def loss(w, b):
    pred = w * x[:, None, None] + b          # broadcast over a grid of (w, b)
    return np.mean((pred - y[:, None, None]) ** 2, axis=0)

W, B = np.meshgrid(np.linspace(0, 4, 121), np.linspace(-2, 4, 121))
L = loss(W, B)
i = np.unravel_index(np.argmin(L), L.shape)
print(f"grid minimum near w = {W[i]:.2f}, b = {B[i]:.2f}, loss = {L[i]:.4f}")
print("least squares:", np.round(np.polyfit(x, y, 1), 4))

plt.figure(figsize=(6, 4))
cs = plt.contour(W, B, L, levels=[0.05, 0.3, 1, 3, 8, 20], cmap="viridis")
plt.clabel(cs, fontsize=8)
plt.plot(W[i], B[i], "r*", markersize=12)
plt.xlabel("w (slope)"); plt.ylabel("b (intercept)"); plt.title("Level curves of the loss L(w, b)")
grid minimum near w = 1.97, b = 1.10, loss = 0.0187
least squares: [1.96 1.1 ]

The contours are long thin ellipses: the loss changes slowly along one diagonal direction and quickly across it. That shape is what makes plain gradient descent zig-zag, and it is a good reason to scale features.

Limits in several variables

In one variable, xx can approach aa only from the left or the right. In the plane, (x,y)(x, y) can approach (a,b)(a, b) along infinitely many paths: straight lines, parabolas, spirals. The limit

lim⁡f(x,y)=Las (x,y)→(a,b)\lim f(x, y) = L \qquad \text{as } (x, y) \to (a, b)

exists only if f(x,y)f(x, y) gets close to LL whatever the path. So a quick way to show that a limit does not exist is to find two paths that give different values.

Take f(x,y)=2xy/(x2+y2)f(x, y) = 2xy / (x^2 + y^2) near the origin. Along the line y=m⋅xy = m \cdot x:

f(x,mx)=2mx2x2+m2x2=2m1+m2f(x, mx) = \frac{2mx^2}{x^2 + m^2x^2} = \frac{2m}{1 + m^2}

The value depends on the slope mm: 0 along the x-axis (m=0m = 0), 1 along y=xy = x, −1 along y=−xy = -x. So the limit at (0,0)(0, 0) does not exist, even though every straight-line path gives a limit.

python · try it yourself
import numpy as np

f = lambda x, y: 2 * x * y / (x**2 + y**2)
g = lambda x, y: x**2 * y / (x**2 + y**2)

for name, path in [("y = 0 ", lambda t: (t, 0 * t)),
                   ("y = x ", lambda t: (t, t)),
                   ("y = -x", lambda t: (t, -t)),
                   ("y = x²", lambda t: (t, t**2))]:
    vals_f = [round(float(f(*path(t))), 4) for t in [0.1, 0.01, 0.001]]
    vals_g = [round(float(g(*path(t))), 4) for t in [0.1, 0.01, 0.001]]
    print(f"{name}: f -> {vals_f},  g -> {vals_g}")
y = 0 : f -> [0.0, 0.0, 0.0],  g -> [0.0, 0.0, 0.0]
y = x : f -> [1.0, 1.0, 1.0],  g -> [0.05, 0.005, 0.0005]
y = -x: f -> [-1.0, -1.0, -1.0],  g -> [-0.05, -0.005, -0.0005]
y = x²: f -> [0.198, 0.02, 0.002],  g -> [0.0099, 0.0001, 0.0]

For g(x,y)=x2y/(x2+y2)g(x, y) = x^2y / (x^2 + y^2) every path heads to 0. Numbers can only suggest that; a proof needs a bound valid for all paths. Here x2≤x2+y2x^2 \le x^2 + y^2, so ∣g∣≤∣y∣\lvert g \rvert \le \lvert y \rvert, which goes to 0. The limit of gg is 0.

Continuity

ff is continuous at (a,b)(a, b) if f(a,b)f(a, b) is defined and equals the limit there. Sums, products and compositions of continuous functions are continuous, and so is a quotient where the denominator is non-zero, exactly as in one variable. The function ff above cannot be made continuous at the origin, whatever value you assign there.

In software

  • Machine learning. Training is the search for a low point of a function of many variables (the loss). Level curves and 2D slices are how people visualise it.
  • Graphics. A fragment shader is a function of (x,y)(x, y) (and time) returning a colour. Signed distance fields are functions of (x,y,z)(x, y, z).
  • Finance and engineering. A spreadsheet model with several input cells is a function of several variables; a "data table" in Excel scans it over a grid like the one above.

Exercises

Exercise 1: Domains

Describe the domain of (a) f(x,y)=y−x2f(x, y) = \sqrt{y - x^2} and (b) g(x,y)=1/(x−2y)g(x, y) = 1 / (x - 2y).

Answer

(a) y≥x2y \ge x^2, the region on and above the parabola. (b) every point except the line x=2yx = 2y.

Solution

(a) The square root needs y−x2≥0y - x^2 \ge 0, so y≥x2y \ge x^2. (b) The denominator must be non-zero: x−2y≠0x - 2y \ne 0, so remove the line y=x/2y = x/2.

Exercise 2: Level curves

Describe the level curves of f(x,y)=y−x2f(x, y) = y - x^2 for c=−1c = -1, 0 and 2.

Answer

Parabolas y=x2+cy = x^2 + c: the same upward parabola shifted to vertex (0,−1)(0, -1), (0,0)(0, 0) and (0,2)(0, 2).

Solution

f(x,y)=cf(x, y) = c means y−x2=cy - x^2 = c, so y=x2+cy = x^2 + c. Each level curve is the parabola y=x2y = x^2 moved up by cc.

Exercise 3: A limit that fails

Show that the limit of f(x,y)=(x2−y2)/(x2+y2)f(x, y) = (x^2 - y^2) / (x^2 + y^2) at (0,0)(0, 0) does not exist.

Answer

Along the x-axis f→1f \to 1, along the y-axis f→−1f \to -1, so there is no single limit.

Solution
y=0:f(x,0)=x2/x2=1→1x=0:f(0,y)=−y2/y2=−1→−1\begin{aligned} y = 0{:}\quad f(x, 0) &= x^2 / x^2 = 1 && \to 1 \\ x = 0{:}\quad f(0, y) &= -y^2 / y^2 = -1 && \to -1 \end{aligned}

Two paths give different values, so the limit does not exist.

Exercise 4: Locate the minimum on a grid (code)

Evaluate f(x,y)=(x−1)2+2(y+0.5)2+0.5xyf(x, y) = (x - 1)^2 + 2(y + 0.5)^2 + 0.5xy on a grid over [−3,3]×[−3,3][-3, 3] \times [-3, 3] with step 0.01 and report where the smallest value occurs.

Answer

Near (x,y)=(1.16,−0.65)(x, y) = (1.16, -0.65), with f≈−0.306f \approx -0.306.

Solution

Exact solution, for reference: setting the partial derivatives to zero (next lessons) gives 2(x−1)+0.5y=02(x - 1) + 0.5y = 0 and 4(y+0.5)+0.5x=04(y + 0.5) + 0.5x = 0, whose solution is x=36/31≈1.161x = 36/31 \approx 1.161, y=−20/31≈−0.645y = -20/31 \approx -0.645, with minimum value ≈−0.3065\approx -0.3065.

python · try it yourself
import numpy as np
g = np.round(np.arange(-3, 3.0001, 0.01), 2)
X, Y = np.meshgrid(g, g)
F = (X - 1)**2 + 2 * (Y + 0.5)**2 + 0.5 * X * Y
i = np.unravel_index(np.argmin(F), F.shape)
print(X[i], Y[i], round(float(F[i]), 4))
1.16 -0.65 -0.3064

Test yourself

What are the level curves of f(x,y)=x2+y2f(x, y) = x^2 + y^2?

  1. Parallel lines
  2. Circles centred at the origin
  3. Parabolas
  4. Hyperbolas

To prove that a limit at (a,b)(a, b) does NOT exist, it is enough to…

  1. find one path where the function has no limit or two paths that give different limits
  2. check the x-axis only
  3. show that f(a,b)f(a, b) is undefined
  4. show that the function is a fraction

On a contour map, closely spaced level curves mean…

  1. the function is constant there
  2. the function changes rapidly there
  3. a minimum
  4. a discontinuity