Calculus · Chapter 14 · Partial Derivatives
Calculus · Chapter 14

Partial Derivatives

Single-variable calculus handles quantities that depend on one input. Most physical quantities depend on several. This chapter extends limits, derivatives, and optimization to functions of two or more variables — the foundation for fields, surfaces, gradients, and constrained optimization throughout engineering and mathematics.

Instructor Note

This chapter is the bridge from 1-D calculus to the multivariable tools you'll use constantly in thermodynamics, fluid mechanics, and structural analysis. Don't rush 14.1–14.3: a solid mental picture of surfaces, traces, and level curves makes every later section easier. The gradient (14.6) and Lagrange multipliers (14.8) are the two ideas you will reuse the most in design optimization.

14.1

Functions of Several Variables


Functions can take more than one input. We study them verbally, numerically, algebraically, and visually.

Function of Two Variables

A rule that assigns to each ordered pair \((x,y)\) in a domain \(D\) exactly one real number \(f(x,y)\). Here \(x\) and \(y\) are the independent variables and \(z=f(x,y)\) is the dependent variable. The domain is a subset of the plane; the range is the set of output values.

Domain Convention

When a function is given by a formula with no stated domain, the domain is taken to be every pair \((x,y)\) for which the expression returns a well-defined real number — for example, excluding inputs that produce division by zero or the square root of a negative number.

Graph and Surface

The graph of \(f\) is the set of points \((x,y,z)\) in space with \(z=f(x,y)\) and \((x,y)\) in \(D\). It forms a surface sitting directly above (or below) the domain in the \(xy\)-plane, the two-variable analogue of a curve.

Level Curves

The level curves of \(f\) are the plane curves \(f(x,y)=k\) for chosen constants \(k\). They show where the surface has a fixed height, exactly like contour lines on a topographic map. Curves bunched close together signal a steep surface; widely spaced curves signal a gentle one. Isobars (pressure) and isothermals (temperature) are everyday examples.

Functions of Three or More Variables

A function of three variables assigns a number \(f(x,y,z)\) to each triple in its domain. Its graph would need four dimensions, so we visualize it instead through level surfaces \(f(x,y,z)=k\). The idea generalizes to any number of variables \(n\), where inputs are \(n\)-tuples and can be written compactly with vector notation.

Cobb–Douglas Production Function
\[ P(L,K)=bL^{\alpha}K^{1-\alpha} \]

Here \(P\) is total production, \(L\) is labor, \(K\) is capital, and \(b,\alpha\) are positive constants. The fitted historical model used \(P(L,K)=1.01\,L^{0.75}K^{0.25}\), with domain restricted to \(L\ge 0,\ K\ge 0\) since labor and capital can't be negative.

x y k=4 k=8
FIGURE 14.1-A Redrawn schematic: nested level curves of an elliptic paraboloid. Each curve marks a constant height \(k\); tighter spacing means a steeper surface.
EXAMPLE 1 Evaluating and Finding a Domain
For \(f(x,y)=\dfrac{\sqrt{x+y+1}}{x-1}\), evaluate \(f(3,2)\) and describe the domain.
Solution
Substitute: \(f(3,2)=\dfrac{\sqrt{3+2+1}}{3-1}=\dfrac{\sqrt6}{2}\).
The expression is real when the radicand is nonnegative and the denominator is nonzero: \(x+y+1\ge 0\) and \(x\ne 1\).
Domain: \(\{(x,y)\mid x+y+1\ge 0,\ x\ne 1\}\) — the region on or above the line \(y=-x-1\), with the vertical line \(x=1\) removed.
EXAMPLE 4 Domain and Range of a Hemisphere
Find the domain and range of \(g(x,y)=\sqrt{9-x^2-y^2}\).
Solution
Need \(9-x^2-y^2\ge 0\), i.e. \(x^2+y^2\le 9\): the disk of radius \(3\) centered at the origin.
Since the square root is nonnegative and \(9-x^2-y^2\le 9\), the outputs satisfy \(0\le z\le 3\).
Domain: disk \(x^2+y^2\le 9\). Range: the interval \([0,3]\). The graph is the top half of a sphere of radius 3.
Instructor Note

When you're asked for a domain, always ask two questions in order: "what makes a square root invalid?" and "what makes a denominator zero?" Sketch the boundary lines/curves first, then shade. A clean domain sketch is worth more partial-credit than a rushed answer.

14.2

Limits and Continuity


In two dimensions a point can be approached from infinitely many directions, which changes how limits behave.

Limit of a Function of Two Variables

We write \(\lim_{(x,y)\to(a,b)}f(x,y)=L\) if \(f(x,y)\) can be made arbitrarily close to \(L\) by taking \((x,y)\) sufficiently close to \((a,b)\) — along every path in the domain. Unlike the 1-D case with only left and right, here the point may approach from any direction whatsoever.

Formal (ε–δ) Definition
\[ \forall\,\varepsilon>0\ \exists\,\delta>0:\ 0<\sqrt{(x-a)^2+(y-b)^2}<\delta \ \Rightarrow\ |f(x,y)-L|<\varepsilon \]
Two-path test for nonexistence. If \(f\) approaches different values along two different paths into \((a,b)\), the limit does not exist. This is the workhorse for proving a multivariable limit fails.

Continuity

\(f\) is continuous at \((a,b)\) if \(\lim_{(x,y)\to(a,b)}f(x,y)=f(a,b)\) — the limit exists, the function is defined there, and the two agree. Polynomials are continuous everywhere; rational functions are continuous on their domains. Sums, products, quotients, and compositions of continuous functions are continuous.

EXAMPLE 1 A Limit That Fails (Two Axes)
Show that \(\displaystyle\lim_{(x,y)\to(0,0)}\dfrac{x^2-y^2}{x^2+y^2}\) does not exist.
Solution
Along the \(x\)-axis (\(y=0\)): \(f(x,0)=\dfrac{x^2}{x^2}=1\), so the limit appears to be \(1\).
Along the \(y\)-axis (\(x=0\)): \(f(0,y)=\dfrac{-y^2}{y^2}=-1\), so the limit appears to be \(-1\).
Two paths give two different values, so the limit does not exist.
EXAMPLE 2 A Limit That Fails (Diagonal Path)
Does \(\displaystyle\lim_{(x,y)\to(0,0)}\dfrac{xy}{x^2+y^2}\) exist?
Solution
Along each axis the function is \(0\), tempting us to guess the limit is \(0\).
Along \(y=x\): \(f(x,x)=\dfrac{x^2}{2x^2}=\tfrac12\) for all \(x\ne 0\).
The axes give \(0\) but the diagonal gives \(\tfrac12\); different paths disagree, so the limit does not exist.
Instructor Note

To prove a limit doesn't exist, you only need two disagreeing paths — try the axes first, then \(y=x\), then \(y=x^2\). To prove a limit does exist, paths are never enough; switch to polar coordinates or the Squeeze Theorem. Confusing these two jobs is the single most common exam mistake here.

14.3

Partial Derivatives


Differentiate with respect to one variable while holding the others fixed.

Partial Derivative

The partial derivative of \(f\) with respect to \(x\) measures the rate of change of \(f\) as \(x\) varies while every other variable is held constant. Geometrically it is the slope of the tangent line to the trace cut by holding the other variable fixed.

Partial Derivatives by Definition
\[ f_x(a,b)=\lim_{h\to0}\frac{f(a+h,b)-f(a,b)}{h},\qquad f_y(a,b)=\lim_{h\to0}\frac{f(a,b+h)-f(a,b)}{h} \]
Rule for finding partials. To get \(f_x\), treat \(y\) as a constant and differentiate normally with respect to \(x\). To get \(f_y\), treat \(x\) as a constant and differentiate with respect to \(y\). Notations: \(f_x=\dfrac{\partial f}{\partial x}=\dfrac{\partial z}{\partial x}\).

Higher and Mixed Partials

Differentiating a partial again gives second-order partials \(f_{xx},\ f_{yy}\) and the mixed partials \(f_{xy},\ f_{yx}\). The notation \(f_{xy}\) means differentiate first with respect to \(x\), then \(y\).

Clairaut's Theorem

If \(f\) is defined on a disk containing \((a,b)\) and both mixed partials \(f_{xy}\) and \(f_{yx}\) are continuous there, then they are equal:

\[ f_{xy}(a,b)=f_{yx}(a,b) \]

In practice the order of mixed differentiation does not matter for the functions you will normally encounter.

EXAMPLE 1 First Partials of a Polynomial
For \(f(x,y)=x^3+x^2y^3-2y^2\), find \(f_x(2,1)\) and \(f_y(2,1)\).
Solution
Hold \(y\) fixed: \(f_x=3x^2+2xy^3\), so \(f_x(2,1)=3(4)+2(2)(1)=16\).
Hold \(x\) fixed: \(f_y=3x^2y^2-4y\), so \(f_y(2,1)=3(4)(1)-4(1)=8\).
EXAMPLE 4 Implicit Partial Differentiation
Find \(\partial z/\partial x\) and \(\partial z/\partial y\) if \(z\) is defined implicitly by \(x^3+y^3+z^3+6xyz=1\).
Solution
Differentiate with respect to \(x\), treating \(y\) constant and \(z=z(x,y)\): \(3x^2+3z^2 z_x+6yz+6xy\,z_x=0\).
Solve: \(\dfrac{\partial z}{\partial x}=-\dfrac{x^2+2yz}{z^2+2xy}\).
By symmetry, \(\dfrac{\partial z}{\partial y}=-\dfrac{y^2+2xz}{z^2+2xy}\).

Partial Differential Equations

Partial derivatives appear in equations expressing physical laws. Solutions of Laplace's equation are called harmonic functions and arise in heat conduction, fluid flow, and electric potential. The wave equation models vibrating strings and traveling sound, light, and water waves.

Laplace's Equation
\[ \frac{\partial^2 u}{\partial x^2}+\frac{\partial^2 u}{\partial y^2}=0 \]
Wave Equation
\[ \frac{\partial^2 u}{\partial t^2}=a^2\,\frac{\partial^2 u}{\partial x^2} \]
Instructor Note

Laplace and the wave equation are not just abstract curiosities — you'll meet Laplace in steady-state heat and potential flow, and the wave equation in vibrations and acoustics. Memorize their shape now so you recognize them instantly later. A function being "harmonic" simply means it satisfies Laplace's equation.

14.4

Tangent Planes and Linear Approximations


Zoom in on a smooth surface and it looks flat — that flat piece is the tangent plane, and it gives the best linear approximation.

Tangent Plane

If \(f\) has continuous first partials, the tangent plane to \(z=f(x,y)\) at a point is the plane containing the two tangent lines to the traces through that point. It is the plane that best approximates the surface nearby.

Equation of the Tangent Plane
\[ z-z_0=f_x(x_0,y_0)(x-x_0)+f_y(x_0,y_0)(y-y_0) \]

Linearization and Linear Approximation

The linearization \(L(x,y)\) of \(f\) at \((a,b)\) is the right-hand side of the tangent-plane equation. Near \((a,b)\), \(f(x,y)\approx L(x,y)\). This is the two-variable version of approximating a curve by its tangent line.

Linearization at (a, b)
\[ L(x,y)=f(a,b)+f_x(a,b)(x-a)+f_y(a,b)(y-b) \]
Sufficient Condition for Differentiability

If the partial derivatives \(f_x\) and \(f_y\) exist near \((a,b)\) and are continuous at \((a,b)\), then \(f\) is differentiable at \((a,b)\).

Caution: the mere existence of both partials is not enough; continuity of the partials is what guarantees a good linear approximation.

Differentials

For \(z=f(x,y)\), the total differential \(dz\) estimates the change in \(z\) caused by small changes \(dx,dy\) in the inputs. It is the change in height of the tangent plane and closely matches the true change \(\Delta z\) when the increments are small. The idea extends to three or more variables.

Total Differential
\[ dz=\frac{\partial z}{\partial x}\,dx+\frac{\partial z}{\partial y}\,dy \]
EXAMPLE 1 Tangent Plane to a Paraboloid
Find the tangent plane to \(z=2x^2+y^2\) at \((1,1,3)\).
Solution
Partials: \(f_x=4x,\ f_y=2y\), so \(f_x(1,1)=4,\ f_y(1,1)=2\).
Plug into the tangent-plane formula: \(z-3=4(x-1)+2(y-1)\).
Simplify: \(z=4x+2y-3\).
EXAMPLE 5 Error Estimation with Differentials
A cone has base radius 10 cm and height 25 cm, each measured to within 0.1 cm. Estimate the maximum error in the computed volume \(V=\tfrac{\pi}{3}r^2h\).
Solution
Differential: \(dV=\dfrac{2\pi rh}{3}\,dr+\dfrac{\pi r^2}{3}\,dh\).
Use largest errors \(dr=dh=0.1\) with \(r=10,\ h=25\): \(dV=\dfrac{2\pi(10)(25)}{3}(0.1)+\dfrac{\pi(10)^2}{3}(0.1)\).
This gives \(dV=\dfrac{500\pi}{3}(0.1)+\dfrac{100\pi}{3}(0.1)=20\pi\approx 63\ \text{cm}^3\) maximum error.
14.5

The Chain Rule


When variables depend on other variables, derivatives multiply along every path and add across paths.

Chain Rule, Case 1

If \(z=f(x,y)\) is differentiable and both \(x=x(t)\) and \(y=y(t)\) are differentiable functions of a single variable \(t\), then \(z\) is a differentiable function of \(t\). Each input contributes one term.

Chain Rule — One Independent Variable
\[ \frac{dz}{dt}=\frac{\partial f}{\partial x}\frac{dx}{dt}+\frac{\partial f}{\partial y}\frac{dy}{dt} \]

Chain Rule, Case 2

If \(x\) and \(y\) each depend on two variables \(s\) and \(t\), then \(z\) depends on \(s\) and \(t\) as well. You compute \(\partial z/\partial s\) and \(\partial z/\partial t\) separately, summing one term per intermediate variable.

Chain Rule — Two Independent Variables
\[ \frac{\partial z}{\partial s}=\frac{\partial z}{\partial x}\frac{\partial x}{\partial s}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial s},\qquad \frac{\partial z}{\partial t}=\frac{\partial z}{\partial x}\frac{\partial x}{\partial t}+\frac{\partial z}{\partial y}\frac{\partial y}{\partial t} \]
Tree-diagram rule. Draw a branch for each dependency. To find a derivative, multiply the partials along each path from the top variable down to the variable of interest, then add the products from all such paths. One term per path.
Implicit Differentiation (two variables)
\[ \frac{dy}{dx}=-\frac{F_x}{F_y}\quad\text{for } F(x,y)=0 \]
Implicit Differentiation (three variables)
\[ \frac{\partial z}{\partial x}=-\frac{F_x}{F_z},\qquad \frac{\partial z}{\partial y}=-\frac{F_y}{F_z}\quad\text{for } F(x,y,z)=0 \]
EXAMPLE 1 Chain Rule with One Parameter
If \(z=x^2y+3xy^4\) with \(x=\sin 2t\) and \(y=\cos t\), find \(dz/dt\) at \(t=0\).
Solution
Apply Case 1: \(\dfrac{dz}{dt}=(2xy+3y^4)(2\cos 2t)+(x^2+12xy^3)(-\sin t)\).
At \(t=0\): \(x=\sin 0=0,\ y=\cos 0=1\).
Substitute: \(\dfrac{dz}{dt}\big|_{0}=(0+3)(2)+(0)(0)=6\).
EXAMPLE 8 Implicit Derivative
Find \(dy/dx\) for \(x^3+y^3=6xy\).
Solution
Write \(F(x,y)=x^3+y^3-6xy=0\).
Then \(F_x=3x^2-6y\) and \(F_y=3y^2-6x\).
So \(\dfrac{dy}{dx}=-\dfrac{F_x}{F_y}=-\dfrac{3x^2-6y}{3y^2-6x}=\dfrac{2y-x^2}{y^2-2x}\).
14.6

Directional Derivatives and the Gradient Vector


Partials measure change along the axes. The directional derivative measures change in any direction — and the gradient points the steepest way uphill.

Directional Derivative

The directional derivative \(D_{\mathbf u}f\) gives the rate of change of \(f\) at a point in the direction of a unit vector \(\mathbf u\). Choosing \(\mathbf u=\mathbf i\) or \(\mathbf u=\mathbf j\) recovers the partial derivatives \(f_x\) and \(f_y\), so partials are special cases.

Directional Derivative (computational form)
\[ D_{\mathbf u}f(x,y)=f_x(x,y)\,a+f_y(x,y)\,b,\qquad \mathbf u=\langle a,b\rangle \]

The Gradient Vector

The gradient \(\nabla f\) collects all the first partials into a vector. The directional derivative is then simply the dot product of the gradient with the direction — the scalar projection of \(\nabla f\) onto \(\mathbf u\).

Gradient and Directional Derivative
\[ \nabla f=\langle f_x,\,f_y\rangle,\qquad D_{\mathbf u}f=\nabla f\cdot\mathbf u \]
Maximizing the Directional Derivative

The maximum value of the directional derivative \(D_{\mathbf u}f\) equals \(|\nabla f|\), and it occurs in the direction of the gradient vector \(\nabla f\). In other words, \(f\) increases fastest in the direction of \(\nabla f\), at a rate equal to the gradient's length; it decreases fastest in the opposite direction.

Geometric meaning of the gradient. \(\nabla f\) is perpendicular to the level curve (or level surface) through a point, and it points toward fastest increase. On a topographic map, the gradient points straight up the steepest slope, perpendicular to the contour lines.
Tangent Plane to a Level Surface F(x,y,z)=k
\[ F_x(x_0,y_0,z_0)(x-x_0)+F_y(\cdots)(y-y_0)+F_z(\cdots)(z-z_0)=0 \]
EXAMPLE 4 Directional Derivative via the Gradient
Find the directional derivative of \(f(x,y)=x^2y^3-4y\) at \((2,-1)\) in the direction of \(\mathbf v=2\mathbf i+5\mathbf j\).
Solution
Gradient: \(\nabla f=\langle 2xy^3,\ 3x^2y^2-4\rangle\), so \(\nabla f(2,-1)=\langle -4,\ 8\rangle\).
Normalize \(\mathbf v\): \(|\mathbf v|=\sqrt{29}\), so \(\mathbf u=\left\langle \tfrac{2}{\sqrt{29}},\ \tfrac{5}{\sqrt{29}}\right\rangle\).
Dot product: \(D_{\mathbf u}f=\langle-4,8\rangle\cdot\mathbf u=\dfrac{-8+40}{\sqrt{29}}=\dfrac{32}{\sqrt{29}}\).
EXAMPLE 6 Direction of Fastest Increase
For \(f(x,y)=xe^{y}\) at \(P(2,0)\), find the maximum rate of change and the direction in which it occurs.
Solution
Gradient: \(\nabla f=\langle e^{y},\ xe^{y}\rangle\), so \(\nabla f(2,0)=\langle 1,\ 2\rangle\).
Fastest increase is in the direction \(\langle 1,2\rangle\).
Maximum rate equals \(|\nabla f(2,0)|=\sqrt{1^2+2^2}=\sqrt5\).
Instructor Note

The gradient is the most reusable idea in this chapter. Three facts to lock in: (1) it points toward steepest increase, (2) its magnitude is the steepest slope, (3) it is normal to level sets. Heat flows down the temperature gradient; optimization climbs it. If you remember only one diagram from Chapter 14, make it the gradient perpendicular to a contour line.

14.7

Maximum and Minimum Values


Find peaks, valleys, and saddle points by locating critical points and classifying them with second derivatives.

Local Extrema and Critical Points

\(f\) has a local maximum at \((a,b)\) if its value there is at least as large as at all nearby points; a local minimum is defined analogously. A critical point is one where both first partials vanish (\(f_x=0\) and \(f_y=0\)) or where a partial fails to exist. Local extrema can occur only at critical points.

Fermat's Condition (necessary)

If \(f\) has a local maximum or minimum at \((a,b)\) and the first partials exist there, then \(f_x(a,b)=0\) and \(f_y(a,b)=0\). Geometrically, the tangent plane at an interior extremum is horizontal.

Saddle Point

A critical point that is neither a max nor a min. The surface rises in one direction and falls in another — like a horse's saddle. The classic example is \(z=y^2-x^2\) at the origin.

Second Derivatives Test

At a critical point \((a,b)\), let \(D=f_{xx}f_{yy}-(f_{xy})^2\) evaluated at \((a,b)\). Then:

(a) If \(D>0\) and \(f_{xx}>0\): local minimum.
(b) If \(D>0\) and \(f_{xx}<0\): local maximum.
(c) If \(D<0\): saddle point.
If \(D=0\), the test is inconclusive.

Discriminant (Hessian determinant)
\[ D=\begin{vmatrix} f_{xx} & f_{xy}\\ f_{yx} & f_{yy} \end{vmatrix}=f_{xx}\,f_{yy}-\big(f_{xy}\big)^2 \]
saddle point up along one axis, down along the other
FIGURE 14.7-A Redrawn schematic of a saddle: along one direction the critical point is a minimum, along the perpendicular direction a maximum. Here \(D<0\).
EXAMPLE 3 Classifying Critical Points
Find and classify the critical points of \(f(x,y)=x^4+y^4-4xy+1\).
Solution
Set \(f_x=4x^3-4y=0\) and \(f_y=4y^3-4x=0\). Substituting \(y=x^3\) gives \(x^9-x=0\), so \(x=0,\pm1\). Critical points: \((0,0),(1,1),(-1,-1)\).
Second partials: \(f_{xx}=12x^2,\ f_{yy}=12y^2,\ f_{xy}=-4\), so \(D=144x^2y^2-16\).
At \((0,0)\): \(D=-16<0\) → saddle point. At \((\pm1,\pm1)\): \(D=128>0\) and \(f_{xx}=12>0\) → local minima with value \(-1\).

Absolute Extrema on a Closed Region

Extreme Value Theorem (two variables)

A continuous function on a closed, bounded set attains both an absolute maximum and an absolute minimum. To find them: evaluate \(f\) at interior critical points, then examine \(f\) along the boundary, and compare all candidate values. The largest is the absolute max; the smallest is the absolute min.

EXAMPLE 6 Box of Maximum Volume
A rectangular box with no lid is built from \(12\ \text{m}^2\) of cardboard. Find the maximum volume.
Solution
Volume \(V=xyz\) subject to the material constraint \(2xz+2yz+xy=12\). Solve the constraint for \(z\) and substitute to get \(V\) in terms of \(x,y\).
Set both partials of \(V\) to zero. The symmetric critical point gives \(x=y\) and \(x=y=2z\).
Solving yields \(x=2,\ y=2,\ z=1\), so the maximum volume is \(V=2\cdot2\cdot1=4\ \text{m}^3\).
14.8

Lagrange Multipliers


Optimize a function subject to a constraint by aligning gradients — no need to solve the constraint for one variable.

The Core Idea

To extremize \(f(x,y,z)\) on a constraint surface \(g(x,y,z)=k\), the largest value occurs where a level surface of \(f\) is tangent to the constraint surface. At that point the gradients point the same way, so \(\nabla f\) is a scalar multiple of \(\nabla g\). The scalar \(\lambda\) is the Lagrange multiplier.

Lagrange Condition (one constraint)
\[ \nabla f=\lambda\,\nabla g,\qquad g(x,y,z)=k \]
Method. Solve the system \(f_x=\lambda g_x,\ f_y=\lambda g_y,\ f_z=\lambda g_z\) together with the constraint \(g=k\). Evaluate \(f\) at every solution point: the largest output is the constrained maximum, the smallest is the constrained minimum. You rarely need the value of \(\lambda\) itself.

Two Constraints

When the point must satisfy two constraints \(g=k\) and \(h=c\) simultaneously, it is confined to a curve of intersection. There \(\nabla f\) lies in the plane spanned by \(\nabla g\) and \(\nabla h\), so two multipliers are needed.

Lagrange Condition (two constraints)
\[ \nabla f=\lambda\,\nabla g+\mu\,\nabla h \]
g=k tangency ∇f ∥ ∇g
FIGURE 14.8-A Redrawn schematic: the extreme value occurs where a level curve of \(f\) just touches the constraint curve \(g=k\); there the gradients are parallel.
EXAMPLE 2 Extrema on a Circle
Find the extreme values of \(f(x,y)=x^2+2y^2\) on the circle \(x^2+y^2=1\).
Solution
Lagrange equations: \(2x=2\lambda x\) and \(4y=2\lambda y\), with \(x^2+y^2=1\).
From the first, \(x=0\) or \(\lambda=1\). This yields the candidate points \((\pm1,0)\) and \((0,\pm1)\).
Evaluate: \(f(\pm1,0)=1\) (minimum), \(f(0,\pm1)=2\) (maximum).
EXAMPLE 4 Closest and Farthest Points on a Sphere
Find the points on \(x^2+y^2+z^2=4\) closest to and farthest from \((3,1,-1)\).
Solution
Minimize/maximize the squared distance \(f=(x-3)^2+(y-1)^2+(z+1)^2\) subject to \(g=x^2+y^2+z^2=4\).
The Lagrange equations force \((x,y,z)\) to lie on the line through the origin and \((3,1,-1)\); intersecting with the sphere gives \(\lambda=1\pm\tfrac{\sqrt{11}}{2}\).
Closest point: \(\left(\tfrac{6}{\sqrt{11}},\tfrac{2}{\sqrt{11}},-\tfrac{2}{\sqrt{11}}\right)\); farthest point: its negative.
Instructor Note

Lagrange multipliers feel abstract until you see them as "gradients line up at the optimum." Two practical tips: (1) for distance problems, always optimize the squared distance — same answer, cleaner algebra; (2) don't waste time solving for \(\lambda\) unless asked. List your candidate points, plug them into \(f\), and compare. This method scales directly into the constrained optimization you'll do in design and economics.