Chapter 17
18 min read
Section 147 of 353

Functions of Several Variables

Partial Derivatives

Learning Objectives

By the end of this section, you will be able to:

  1. Define functions of two or more variables and understand their notation
  2. Determine the domain and range of multivariable functions
  3. Visualize functions of two variables as surfaces in 3D space
  4. Interpret level curves (contour maps) and understand what they reveal about a function
  5. Describe level surfaces for functions of three variables
  6. Connect multivariable functions to applications in physics, economics, and machine learning

The Big Picture: Beyond Single Variables

"The real world rarely depends on just one quantity. Temperature varies with position and time. Profit depends on price, marketing, and production costs. Machine learning models have thousands of parameters."

Until now, we have studied functions of a single variable: y=f(x)y = f(x). But most real phenomena depend on multiple independent variables:

🌡️ Temperature Distribution

The temperature T(x,y,z,t)T(x, y, z, t) at a point in a room depends on three spatial coordinates and time — four independent variables!

📈 Economics: Cobb-Douglas

Production output Q(L, K) = AL^\\alpha K^\\beta depends on labor (L) and capital (K). This fundamental model guides resource allocation decisions.

🎯 Optimization

Finding the best solution to engineering problems often means minimizing a cost function C(x1,x2,ldots,xn)C(x_1, x_2, \\ldots, x_n) that depends on many design parameters.

🤖 Neural Networks

A neural network's loss function L(theta1,theta2,ldots,thetan)L(\\theta_1, \\theta_2, \\ldots, \\theta_n) depends on millions of parameters. Understanding multivariable calculus is essential for training these models.

The Jump to Multiple Variables

Moving from one variable to two is conceptually huge — we go from curves to surfaces, from derivatives to gradients, from integrals to double integrals. But the underlying intuition of rate of change and accumulation remains the same.


Historical Context: The Need for Higher Dimensions

The study of functions of several variables emerged from pressing practical needs and deep mathematical questions.

The Heat Equation (1822)

Joseph Fourier developed the heat equation to describe how temperatureT(x,y,z,t)T(x, y, z, t) evolves in a solid body. This partial differential equation became one of the first major applications of multivariable calculus:

fracpartialTpartialt=alphaleft(fracpartial2Tpartialx2+fracpartial2Tpartialy2+fracpartial2Tpartialz2right)\\frac{\\partial T}{\\partial t} = \\alpha \\left( \\frac{\\partial^2 T}{\\partial x^2} + \\frac{\\partial^2 T}{\\partial y^2} + \\frac{\\partial^2 T}{\\partial z^2} \\right)

Lagrange and Optimization

Joseph-Louis Lagrange developed methods for optimizing functions subject to constraints — a problem that naturally involves multiple variables. His method of Lagrange multipliers (which we'll study later) remains a cornerstone of constrained optimization.

Modern Applications

Today, functions of many variables are everywhere: from weather prediction to financial modeling, from computer graphics to machine learning. The mathematical framework developed over centuries now powers our digital world.


Defining Functions of Several Variables

Definition: Function of Two Variables

A function of two variables is a rule that assigns to each ordered pair (x,y)(x, y) in a set DD (the domain) a unique real number denoted f(x,y)f(x, y).

We write:

z=f(x,y)z = f(x, y)

where xx and yy are independent variables and zz is the dependent variable.

Notation and Terminology

NotationMeaning
f(x, y)The value of f at the point (x, y)
z = f(x, y)z is the output when inputs are x and y
D or dom(f)The domain - set of valid (x, y) pairs
Range(f)The set of all output values f(x, y)
f: D → ℝf maps points in D to real numbers

Example: Evaluating Multivariable Functions

Consider f(x,y)=x2+xy+y23f(x, y) = x^2 + xy + y^2 - 3. Let's evaluate it at several points:

f(1, 2): 12+(1)(2)+223=1+2+43=41^2 + (1)(2) + 2^2 - 3 = 1 + 2 + 4 - 3 = 4

f(0, 0): 0+0+03=30 + 0 + 0 - 3 = -3

f(-1, 1): (1)2+(1)(1)+123=11+13=2(-1)^2 + (-1)(1) + 1^2 - 3 = 1 - 1 + 1 - 3 = -2

f(a, a): a2+acdota+a23=3a23a^2 + a \\cdot a + a^2 - 3 = 3a^2 - 3

Functions of Three or More Variables

Definition: Function of n Variables

A function of n variables is a rule that assigns to each n-tuple (x1,x2,ldots,xn)(x_1, x_2, \\ldots, x_n) in a domain DsubseteqmathbbRnD \\subseteq \\mathbb{R}^n a unique real number:

w=f(x1,x2,ldots,xn)w = f(x_1, x_2, \\ldots, x_n)

Examples:

  • Volume of a box: V(l,w,h)=lwhV(l, w, h) = lwh (3 variables)
  • Distance formula: d(x,y,z)=sqrtx2+y2+z2d(x, y, z) = \\sqrt{x^2 + y^2 + z^2} (3 variables)
  • Neural network loss: L(theta1,ldots,thetan)L(\\theta_1, \\ldots, \\theta_n) (millions of variables)

Domain and Range

Finding the Domain

The domain of a multivariable function consists of all input points where the function is defined. We apply the same rules as for single-variable functions, but now our domain is aregion in the plane (for two variables) or higher-dimensional space.

Common domain restrictions:

  • Division: Denominator ≠ 0
  • Square roots: Radicand ≥ 0
  • Logarithms: Argument > 0
  • Inverse trig: Argument in proper range

Example: Domain Analysis

Example 1: Find the domain of f(x,y)=sqrt9x2y2f(x, y) = \\sqrt{9 - x^2 - y^2}.

Solution: We need 9x2y2geq09 - x^2 - y^2 \\geq 0, which means x2+y2leq9x^2 + y^2 \\leq 9.

Domain: The closed disk of radius 3 centered at the origin (including the boundary circle).

Geometrically, this is all points at distance ≤ 3 from the origin.

Example 2: Find the domain of g(x,y)=ln(xy)g(x, y) = \\ln(x - y).

Solution: We need xy>0x - y > 0, which means x>yx > y or y<xy < x.

Domain: The half-plane below the line y=xy = x.

Range of Multivariable Functions

The range is the set of all output values. Finding the range can be more challenging than finding the domain.

Example: Find the range of f(x,y)=x2+y2f(x, y) = x^2 + y^2.

Solution: Since x2geq0x^2 \\geq 0 and y2geq0y^2 \\geq 0 for all real x and y, we have f(x,y)geq0f(x, y) \\geq 0.

The minimum value 0 is achieved at (0,0)(0, 0).

As we move away from the origin, ff can be made arbitrarily large.

Range: [0,infty)[0, \\infty)


Graphs as Surfaces in 3D

For a function of two variables z=f(x,y)z = f(x, y), the graph is the set of all points (x,y,z)(x, y, z) in three-dimensional space where z=f(x,y)z = f(x, y):

Graph of z = f(x, y)

textGraph(f)=(x,y,z)inmathbbR3:z=f(x,y),(x,y)inD\\text{Graph}(f) = \\{(x, y, z) \\in \\mathbb{R}^3 : z = f(x, y), (x, y) \\in D\\}

This graph is typically a surface in 3D space. The shape of the surface reveals important properties of the function:

FunctionSurface TypeKey Features
z = x² + y²ParaboloidBowl opening upward, minimum at origin
z = x² - y²Saddle (hyperbolic paraboloid)Curves up in x, down in y
z = √(1 - x² - y²)HemisphereUpper half of unit sphere
z = sin(x)cos(y)Egg-crate surfacePeriodic peaks and valleys
z = 1/(x² + y²)FunnelApproaches infinity at origin

Interactive: Explore 3D Surfaces

Use the visualization below to explore how different functions create different surfaces. Rotate, zoom, and probe specific points to understand how the surface encodes the function's behavior.

Loading 3D visualization...

Reading 3D Surfaces

The height of the surface above (or below) the xy-plane at any point (x,y)(x, y) gives the value f(x,y)f(x, y). The shape of the surface tells us about rates of change, extrema, and other properties.


Level Curves and Contour Maps

While 3D surfaces are intuitive, they can be hard to work with. A powerful alternative is the contour map or level curve representation.

Definition: Level Curves

The level curves of a function f(x,y)f(x, y) are the curves in the xy-plane where the function has a constant value:

f(x,y)=cf(x, y) = c

for various constants cc.

The Topographic Map Analogy

Level curves work exactly like contour lines on a topographic map. Each contour line represents a constant elevation. The closer the lines, the steeper the terrain.

Close Contours = Steep Slope

When level curves are close together, the function value changes rapidly. This indicates a steep slope on the surface.

Perpendicular Direction = Steepest Change

Moving perpendicular to a level curve gives the direction of steepest ascent. This is the gradient direction (to be studied soon).

Examples of Level Curves

Example 1: Paraboloid f(x,y)=x2+y2f(x, y) = x^2 + y^2

Setting x2+y2=cx^2 + y^2 = c for various c>0c > 0 gives circles of radius sqrtc\\sqrt{c} centered at the origin.

As cc increases, the circles grow larger. The level curves are concentric circles.

Example 2: Saddle Surface f(x,y)=x2y2f(x, y) = x^2 - y^2

Setting x2y2=cx^2 - y^2 = c:

  • For c>0c > 0: hyperbolas opening left-right
  • For c<0c < 0: hyperbolas opening up-down
  • For c=0c = 0: the lines y=pmxy = \\pm x (the asymptotes)

Example 3: Linear Function f(x,y)=2x+3yf(x, y) = 2x + 3y

Setting 2x+3y=c2x + 3y = c gives parallel lines with slope 2/3-2/3.

For a linear function, all level curves are parallel lines. The gradient (direction of steepest ascent) is the same everywhere.

Interactive: Level Curve Explorer

Explore how different functions produce different level curve patterns. Move your cursor over the plot to see exact function values.

Loading visualization...

Level Curves and Optimization

Level curves are essential for understanding optimization. Local maxima and minima occur where level curves form closed loops (or single points). Saddle points occur where level curves cross or meet.


Level Surfaces (Three Variables)

For functions of three variables w=f(x,y,z)w = f(x, y, z), we cannot draw the graph (it would require 4D). Instead, we study level surfaces.

Definition: Level Surfaces

The level surfaces of a function f(x,y,z)f(x, y, z) are the surfaces in 3D space where the function has a constant value:

f(x,y,z)=cf(x, y, z) = c

Examples of Level Surfaces

FunctionLevel Surface f = cDescription
f(x,y,z) = x² + y² + z²Spheres of radius √cConcentric spheres
f(x,y,z) = x² + y² - z²HyperboloidsOne-sheet (c<0) or two-sheet (c>0)
f(x,y,z) = x + 2y + 3zParallel planesAll with normal vector (1,2,3)
f(x,y,z) = x² + y²CylindersCircular cylinders around z-axis

Temperature Distribution

If T(x,y,z)T(x, y, z) represents temperature at each point in a room, then level surfaces T=cT = c are called isothermal surfaces — surfaces of constant temperature. Heat flows perpendicular to these surfaces.


Real-World Applications

1. Thermodynamics: Ideal Gas Law

The ideal gas law PV=nRTPV = nRT relates pressure PP, volume VV, and temperature TT. For a fixed amount of gas, we can express pressure as a function of two variables:

P(V,T)=fracnRTVP(V, T) = \\frac{nRT}{V}

The level curves P=textconstantP = \\text{constant} are called isobars, and they show how volume and temperature must change together to maintain constant pressure.

2. Economics: Production Functions

The Cobb-Douglas production function models output as a function of labor and capital:

Q(L, K) = AL^\\alpha K^\\beta

Level curves Q=textconstantQ = \\text{constant} are called isoquants — they show different combinations of labor and capital that produce the same output.

3. Electrostatics: Electric Potential

The electric potential V(x,y,z)V(x, y, z) due to a point charge at the origin is:

V(x,y,z)=frackqsqrtx2+y2+z2V(x, y, z) = \\frac{kq}{\\sqrt{x^2 + y^2 + z^2}}

Level surfaces are spheres centered at the origin. The electric field points perpendicular to these surfaces — in the direction of steepest potential decrease.


Machine Learning Applications

In machine learning, functions of many variables appear constantly. Understanding their geometry is essential for training models effectively.

1. Loss Functions as Surfaces

When training a neural network, the loss function L(boldsymboltheta)L(\\boldsymbol{\\theta}) depends on all model parameters boldsymboltheta=(theta1,theta2,ldots,thetan)\\boldsymbol{\\theta} = (\\theta_1, \\theta_2, \\ldots, \\theta_n). Even for a simple linear regression y=wx+by = wx + b, the mean squared error loss is:

L(w,b)=frac1nsumi=1n(wxi+byi)2L(w, b) = \\frac{1}{n}\\sum_{i=1}^{n}(wx_i + b - y_i)^2

This is a function of two variables (w and b). Gradient descent navigates this surface to find the minimum.

2. High-Dimensional Landscapes

Real neural networks have millions of parameters, creating loss surfaces in million-dimensional space. Key insights from multivariable calculus:

  • Saddle points are extremely common in high dimensions (more common than local minima)
  • The gradient points toward the direction of steepest ascent
  • Level sets help visualize the loss landscape in lower dimensions

3. Feature Spaces

In classification, data points live in high-dimensional feature spaces. Decision boundaries are level surfaces of the form f(mathbfx)=0f(\\mathbf{x}) = 0, separating different classes.

The Curse of Dimensionality

As dimension increases, the volume of a fixed-radius ball relative to the enclosing cube shrinks exponentially. This has profound implications for data density and distance-based algorithms. Understanding multivariable functions is the first step toward understanding these phenomena.


Python Implementation

Creating and Visualizing Multivariable Functions

Functions of Two Variables in Python
🐍multivariable_functions.py
5The Paraboloid

f(x, y) = x² + y² defines a paraboloid opening upward. Every point on the surface has z equal to the sum of squared distances from the x and y axes.

EXAMPLE
At (1, 2): f(1, 2) = 1 + 4 = 5, so the point (1, 2, 5) is on the surface.
17The Saddle Surface

f(x, y) = x² - y² has different behavior in different directions. Moving along the x-axis, z increases (bowl shape). Moving along the y-axis, z decreases (inverted bowl).

27The 2D Gaussian

The Gaussian function creates a bell-shaped surface with maximum at the origin. The parameter σ controls the width - larger σ means a flatter, wider bell.

37Creating the Grid

meshgrid creates a 2D array of (x, y) coordinate pairs, enabling us to evaluate the function at every point simultaneously using vectorized operations.

74 lines without explanation
1import numpy as np
2import matplotlib.pyplot as plt
3from mpl_toolkits.mplot3d import Axes3D
4
5def f(x, y):
6    """
7    A function of two variables: f(x, y) = x² + y²
8
9    This is the classic paraboloid - a bowl-shaped surface
10    that appears everywhere in optimization (it's the simplest
11    convex function in 2D with a unique minimum at the origin).
12
13    In machine learning, this shape represents an ideal loss
14    surface where gradient descent will always find the minimum.
15    """
16    return x**2 + y**2
17
18def saddle(x, y):
19    """
20    The saddle surface: f(x, y) = x² - y²
21
22    This hyperbolic paraboloid has a saddle point at the origin -
23    it's a local minimum in the x-direction but a local maximum
24    in the y-direction. Saddle points are critical in understanding
25    why optimization in high dimensions is challenging.
26    """
27    return x**2 - y**2
28
29def gaussian(x, y, sigma=1.0):
30    """
31    The 2D Gaussian: f(x, y) = exp(-(x² + y²)/(2σ²))
32
33    This bell-shaped surface is fundamental to:
34    - Probability (normal distribution in 2D)
35    - Machine learning (RBF kernels, Gaussian processes)
36    - Signal processing (smoothing filters)
37    - Heat diffusion (the heat kernel)
38    """
39    return np.exp(-(x**2 + y**2) / (2 * sigma**2))
40
41# Create a grid of (x, y) points
42x = np.linspace(-3, 3, 100)
43y = np.linspace(-3, 3, 100)
44X, Y = np.meshgrid(x, y)
45
46# Evaluate functions on the grid
47Z_paraboloid = f(X, Y)
48Z_saddle = saddle(X, Y)
49Z_gaussian = gaussian(X, Y)
50
51# Visualize all three surfaces
52fig = plt.figure(figsize=(15, 5))
53
54ax1 = fig.add_subplot(131, projection='3d')
55ax1.plot_surface(X, Y, Z_paraboloid, cmap='viridis', alpha=0.8)
56ax1.set_title('Paraboloid: f(x,y) = x² + y²')
57ax1.set_xlabel('x'); ax1.set_ylabel('y'); ax1.set_zlabel('z')
58
59ax2 = fig.add_subplot(132, projection='3d')
60ax2.plot_surface(X, Y, Z_saddle, cmap='coolwarm', alpha=0.8)
61ax2.set_title('Saddle: f(x,y) = x² - y²')
62ax2.set_xlabel('x'); ax2.set_ylabel('y'); ax2.set_zlabel('z')
63
64ax3 = fig.add_subplot(133, projection='3d')
65ax3.plot_surface(X, Y, Z_gaussian, cmap='plasma', alpha=0.8)
66ax3.set_title('Gaussian: f(x,y) = exp(-(x²+y²)/2)')
67ax3.set_xlabel('x'); ax3.set_ylabel('y'); ax3.set_zlabel('z')
68
69plt.tight_layout()
70plt.show()
71
72# Evaluate at specific points
73print("\n=== Function Evaluation ===")
74print(f"f(1, 2) = 1² + 2² = {f(1, 2)}")
75print(f"f(-1, -1) = (-1)² + (-1)² = {f(-1, -1)}")
76print(f"saddle(1, 1) = 1² - 1² = {saddle(1, 1)}")
77print(f"saddle(2, 1) = 2² - 1² = {saddle(2, 1)}")
78print(f"gaussian(0, 0) = {gaussian(0, 0):.4f}")  # Maximum at origin

Creating Contour Plots

Level Curves with Matplotlib
🐍contour_plots.py
4Contour Plot Purpose

Contour plots project a 3D surface onto 2D by drawing curves of constant height. This is exactly how topographic maps work - each contour line represents a specific elevation.

30Filled Contours

contourf fills the regions between level curves with colors, creating a heat map effect. The color indicates the function value at each point.

32Contour Line Labels

clabel adds numeric labels to contour lines showing the function value. This helps identify specific level curves like f(x,y) = 0 or f(x,y) = 5.

61 lines without explanation
1import numpy as np
2import matplotlib.pyplot as plt
3
4def create_contour_plots():
5    """
6    Contour plots (level curves) are 2D representations of 3D surfaces.
7
8    Each curve connects points where f(x, y) = c for some constant c.
9    These are like topographic maps where contour lines show elevation.
10
11    Key insights from contour plots:
12    - Closer contours = steeper slope (faster rate of change)
13    - Perpendicular to contours = direction of steepest ascent
14    - Closed contours often indicate local extrema
15    """
16    x = np.linspace(-3, 3, 200)
17    y = np.linspace(-3, 3, 200)
18    X, Y = np.meshgrid(x, y)
19
20    # Define several functions
21    funcs = {
22        'Paraboloid': X**2 + Y**2,
23        'Saddle': X**2 - Y**2,
24        'Linear': 2*X + 3*Y,
25        'Waves': np.sin(X) + np.sin(Y)
26    }
27
28    fig, axes = plt.subplots(2, 2, figsize=(12, 12))
29    axes = axes.flatten()
30
31    for ax, (name, Z) in zip(axes, funcs.items()):
32        # Filled contour plot
33        cf = ax.contourf(X, Y, Z, levels=20, cmap='viridis')
34        # Contour lines
35        cs = ax.contour(X, Y, Z, levels=20, colors='white', linewidths=0.5)
36        ax.clabel(cs, inline=True, fontsize=8, fmt='%.1f')
37
38        ax.set_title(name)
39        ax.set_xlabel('x')
40        ax.set_ylabel('y')
41        ax.set_aspect('equal')
42        plt.colorbar(cf, ax=ax, shrink=0.8)
43
44    plt.tight_layout()
45    plt.show()
46
47    # Analyze the level curves
48    print("=== Level Curve Analysis ===")
49    print("\nParaboloid (x² + y²):")
50    print("  - Level curves: x² + y² = c → circles of radius √c")
51    print("  - All level curves are concentric circles")
52    print("  - Closer circles near origin = steeper near minimum")
53
54    print("\nSaddle (x² - y²):")
55    print("  - Level curves: x² - y² = c → hyperbolas")
56    print("  - c = 0 gives the lines y = ±x (asymptotes)")
57    print("  - Curves open left/right for c > 0, up/down for c < 0")
58
59    print("\nLinear (2x + 3y):")
60    print("  - Level curves: 2x + 3y = c → parallel lines")
61    print("  - All have slope -2/3")
62    print("  - Equal spacing = constant gradient magnitude")
63
64create_contour_plots()

Loss Surfaces in Machine Learning

Visualizing ML Loss Landscapes
🐍loss_surface.py
4Loss as Multivariable Function

In machine learning, the loss function L(θ₁, θ₂, ..., θₙ) depends on all model parameters. For simple linear regression with y = wx + b, this means L(w, b) is a function of two variables.

19MSE Loss Definition

Mean Squared Error measures the average squared difference between predictions and actual values. For each (w, b) pair, we get a single loss value - this defines the surface.

53Gradient Descent

Gradient descent computes the gradient ∇L = (∂L/∂w, ∂L/∂b) and moves in the opposite direction. The path traces a curve on the loss surface, descending toward the minimum.

93 lines without explanation
1import numpy as np
2import matplotlib.pyplot as plt
3
4def loss_surface_visualization():
5    """
6    In machine learning, loss functions are functions of model parameters.
7
8    For a simple linear regression y = wx + b:
9    - The loss L(w, b) measures how well the model fits the data
10    - This is a function of TWO variables (w and b)
11    - Gradient descent navigates this surface to find minimum
12
13    Understanding loss surfaces as functions of several variables
14    is essential for understanding optimization in ML.
15    """
16    # Generate synthetic data: y = 2x + 1 + noise
17    np.random.seed(42)
18    X_data = np.random.randn(50)
19    y_data = 2 * X_data + 1 + 0.5 * np.random.randn(50)
20
21    def mse_loss(w, b, X=X_data, y=y_data):
22        """Mean Squared Error loss function"""
23        predictions = w * X + b
24        return np.mean((predictions - y)**2)
25
26    # Create parameter grid
27    w_range = np.linspace(-1, 5, 100)
28    b_range = np.linspace(-2, 4, 100)
29    W, B = np.meshgrid(w_range, b_range)
30
31    # Compute loss at each point
32    L = np.zeros_like(W)
33    for i in range(W.shape[0]):
34        for j in range(W.shape[1]):
35            L[i, j] = mse_loss(W[i, j], B[i, j])
36
37    # Find minimum
38    min_idx = np.unravel_index(np.argmin(L), L.shape)
39    w_opt, b_opt = W[min_idx], B[min_idx]
40
41    # Visualize
42    fig, axes = plt.subplots(1, 2, figsize=(14, 6))
43
44    # 3D surface
45    ax1 = fig.add_subplot(121, projection='3d')
46    ax1.plot_surface(W, B, L, cmap='viridis', alpha=0.8)
47    ax1.scatter([w_opt], [b_opt], [L[min_idx]], color='red', s=100,
48                label=f'Minimum at w={w_opt:.2f}, b={b_opt:.2f}')
49    ax1.set_xlabel('Weight (w)')
50    ax1.set_ylabel('Bias (b)')
51    ax1.set_zlabel('Loss')
52    ax1.set_title('MSE Loss Surface')
53    ax1.legend()
54
55    # Contour plot with gradient descent path
56    ax2 = axes[1]
57    cf = ax2.contourf(W, B, L, levels=30, cmap='viridis')
58    ax2.contour(W, B, L, levels=30, colors='white', linewidths=0.3)
59    ax2.plot(w_opt, b_opt, 'r*', markersize=15, label='Minimum')
60
61    # Simulate gradient descent
62    w, b = 4.0, 3.0  # Starting point
63    learning_rate = 0.1
64    path_w, path_b = [w], [b]
65
66    for _ in range(50):
67        # Compute gradients (analytically for linear regression)
68        dL_dw = 2 * np.mean((w * X_data + b - y_data) * X_data)
69        dL_db = 2 * np.mean(w * X_data + b - y_data)
70
71        # Update parameters
72        w -= learning_rate * dL_dw
73        b -= learning_rate * dL_db
74        path_w.append(w)
75        path_b.append(b)
76
77    ax2.plot(path_w, path_b, 'r.-', linewidth=2, markersize=5,
78             label='Gradient Descent Path')
79    ax2.set_xlabel('Weight (w)')
80    ax2.set_ylabel('Bias (b)')
81    ax2.set_title('Loss Contours with Optimization Path')
82    ax2.legend()
83    plt.colorbar(cf, ax=ax2, shrink=0.8)
84
85    plt.tight_layout()
86    plt.show()
87
88    print("=== ML Loss Surface Analysis ===")
89    print(f"\nTrue parameters: w=2, b=1")
90    print(f"Found minimum: w={w_opt:.3f}, b={b_opt:.3f}")
91    print(f"Final GD result: w={path_w[-1]:.3f}, b={path_b[-1]:.3f}")
92    print(f"\nThe loss L(w, b) is a function of TWO variables.")
93    print("Gradient descent navigates this surface by following")
94    print("the direction opposite to the gradient (steepest descent).")
95
96loss_surface_visualization()

Domain Analysis

Visualizing Function Domains
🐍domain_analysis.py
4Domain Restrictions

The domain is determined by the requirements of all operations in the function. Multiple restrictions create composite domain conditions.

27Visualizing Domains

We use contourf to shade valid regions. This makes it easy to see which (x, y) pairs are in the domain and which are excluded.

80 lines without explanation
1import numpy as np
2import matplotlib.pyplot as plt
3
4def analyze_domain():
5    """
6    The domain of a function f(x, y) is the set of all (x, y)
7    pairs where f is defined. Common restrictions:
8
9    1. Division by zero: denominators must be nonzero
10    2. Square roots: radicand must be non-negative
11    3. Logarithms: argument must be positive
12    4. Inverse trig: argument in appropriate interval
13
14    Understanding domains is crucial for:
15    - Defining valid input regions
16    - Identifying singularities and boundaries
17    - Setting up integration limits in calculus
18    """
19
20    # Example 1: f(x, y) = 1/√(9 - x² - y²)
21    # Domain: 9 - x² - y² > 0, i.e., x² + y² < 9 (inside circle)
22
23    # Example 2: f(x, y) = ln(xy)
24    # Domain: xy > 0, i.e., quadrants I and III
25
26    x = np.linspace(-4, 4, 400)
27    y = np.linspace(-4, 4, 400)
28    X, Y = np.meshgrid(x, y)
29
30    fig, axes = plt.subplots(1, 3, figsize=(15, 5))
31
32    # Domain 1: x² + y² < 9
33    domain1 = X**2 + Y**2 < 9
34    axes[0].contourf(X, Y, domain1.astype(float), levels=[0.5, 1.5],
35                     colors=['white', 'lightblue'], alpha=0.7)
36    axes[0].add_patch(plt.Circle((0, 0), 3, fill=False, color='blue', linewidth=2))
37    axes[0].set_title('Domain of f(x,y) = 1/√(9-x²-y²)\nx² + y² < 9')
38    axes[0].set_xlabel('x'); axes[0].set_ylabel('y')
39    axes[0].set_aspect('equal')
40    axes[0].set_xlim(-4, 4); axes[0].set_ylim(-4, 4)
41
42    # Domain 2: xy > 0 (quadrants I and III)
43    domain2 = X * Y > 0
44    axes[1].contourf(X, Y, domain2.astype(float), levels=[0.5, 1.5],
45                     colors=['white', 'lightgreen'], alpha=0.7)
46    axes[1].axhline(0, color='black', linewidth=0.5)
47    axes[1].axvline(0, color='black', linewidth=0.5)
48    axes[1].set_title('Domain of f(x,y) = ln(xy)\nxy > 0')
49    axes[1].set_xlabel('x'); axes[1].set_ylabel('y')
50    axes[1].set_aspect('equal')
51    axes[1].set_xlim(-4, 4); axes[1].set_ylim(-4, 4)
52
53    # Domain 3: y > x² (above parabola)
54    domain3 = Y > X**2
55    axes[2].contourf(X, Y, domain3.astype(float), levels=[0.5, 1.5],
56                     colors=['white', 'lightyellow'], alpha=0.7)
57    x_parabola = np.linspace(-2, 2, 100)
58    axes[2].plot(x_parabola, x_parabola**2, 'orange', linewidth=2)
59    axes[2].set_title('Domain of f(x,y) = √(y-x²)\ny > x²')
60    axes[2].set_xlabel('x'); axes[2].set_ylabel('y')
61    axes[2].set_aspect('equal')
62    axes[2].set_xlim(-4, 4); axes[2].set_ylim(-4, 4)
63
64    plt.tight_layout()
65    plt.show()
66
67    print("=== Domain Analysis ===")
68    print("\n1. f(x,y) = 1/√(9 - x² - y²)")
69    print("   Requires: 9 - x² - y² > 0")
70    print("   Domain: open disk of radius 3 centered at origin")
71    print("   Boundary: circle x² + y² = 9 (excluded)")
72
73    print("\n2. f(x,y) = ln(xy)")
74    print("   Requires: xy > 0")
75    print("   Domain: {(x,y) : x>0 and y>0} ∪ {(x,y) : x<0 and y<0}")
76    print("   This is quadrants I and III")
77
78    print("\n3. f(x,y) = √(y - x²)")
79    print("   Requires: y - x² ≥ 0")
80    print("   Domain: all points on or above the parabola y = x²")
81
82analyze_domain()

Test Your Understanding


Summary

Functions of several variables extend single-variable calculus to handle the complexity of the real world. They provide the foundation for partial derivatives, gradients, and optimization — tools that power modern science and machine learning.

Key Concepts

ConceptDescription
Function of n variablesf(x₁, x₂, ..., xₙ) maps n inputs to one output
DomainSet of all valid input points (region in ℝⁿ)
RangeSet of all output values
Graph (2 variables)Surface z = f(x, y) in 3D space
Level curvesCurves where f(x, y) = c (contour lines)
Level surfacesSurfaces where f(x, y, z) = c

Key Takeaways

  1. Multivariable functions take multiple inputs and produce a single output value
  2. The domain is a region in the plane (or higher-dimensional space) where the function is defined
  3. For two variables, the graph is a surface in 3D space
  4. Level curves show where the function has constant value — like contour lines on a map
  5. Closely spaced level curves indicate rapid change (steep slope)
  6. In machine learning, loss functions are functions of many variables, and understanding their geometry is crucial for optimization
The Essence of Multivariable Functions:
"A function of several variables maps points in space to numbers, creating a landscape that we navigate through contour maps, gradients, and optimization."
Coming Next: In the next section, we'll explore limits and continuity for functions of several variables. The concepts are natural extensions of single-variable ideas, but the geometry becomes richer — we can approach a point from infinitely many directions!
Loading comments...