Ran Wei/ Calculus/Module 18
中文
Calculus · Part V · Several variables

Module 18Optimisation in several variables

Find the highest and lowest points of a function of two or more variables. You will classify critical points with the Hessian, find absolute extreme values on closed bounded regions, optimise under constraints with Lagrange multipliers, fit a line to data by least squares, and see why gradient descent can zigzag when a function curves much more steeply in one direction than in another.

⏱ 270 minYou need: Module 8 (optimisation in one variable) and Modules 16 and 17 (vectors, partial derivatives and the gradient); the quadratic approximation uses Taylor polynomials from Module 15, and one optional proof uses Taylor’s theorem from the same module

In this module you will

  • Find the critical points of a function of two variables and classify them with the second derivative test
  • Find the absolute maximum and minimum values of a continuous function on a closed bounded region
  • Solve optimisation problems with constraints by Lagrange multipliers, and explain the method with level curves
  • Fit a line to data by least squares, deriving the normal equations from partial derivatives
  • Explain how the step size and the shape of the level curves decide whether, and how fast, gradient descent converges
1

Local extrema and critical points

Picture a function of two variables as a landscape: above the point (x,y)(x, y) of a map the ground is at height z=f(x,y),z = f(x, y)\mathclose{\text{,}} and the level curves f(x,y)=cf(x, y) = c are the contour lines of the map (Module 17). Three kinds of place have level ground underfoot: the top of a hill, the bottom of a hollow, and a mountain pass, the lowest point of a ridge and the highest point of the path across it. This module finds such places and tells them apart. Then it finds the extreme values of a function on a region, optimises under constraints, fits lines to data, and studies how gradient descent, a method used to train machine-learning models, searches for the bottom of a valley. Unless a passage says otherwise, ff is a function of two variables; most of the ideas extend to more.

Definition 18.1Local and absolute extrema

Let ff be a function of two variables with domain D,D\mathclose{\text{,}} and let (a,b)(a, b) be a point of D.D\mathclose{\text{.}}

  • f(a,b)f(a, b) is the absolute maximum value of ff if f(a,b)≥f(x,y)f(a, b) \ge f(x, y) for every point (x,y)(x, y) of D,D\mathclose{\text{,}} and the absolute minimum value if f(a,b)≤f(x,y)f(a, b) \le f(x, y) for every (x,y)(x, y) in D.D\mathclose{\text{.}}
  • f(a,b)f(a, b) is a local maximum value of ff if f(a,b)≥f(x,y)f(a, b) \ge f(x, y) for every (x,y)(x, y) in some open disc centred at (a,b)(a, b) that lies in D,D\mathclose{\text{,}} and a local minimum value if f(a,b)≤f(x,y)f(a, b) \le f(x, y) for every (x,y)(x, y) in such a disc.

The open disc of radius r>0r > 0 centred at (a,b)(a, b) is the set of points (x,y)(x, y) with (x−a)2+(y−b)2<r2:(x - a)^2 + (y - b)^2 < r^2\mathclose{\text{:}} the points whose distance from (a,b)(a, b) is less than r.r\mathclose{\text{.}}

These are the extreme values, or extrema. “Local” means “compared with every nearby point, in every direction”, so a point on the edge of the domain is never a local extremum, as in Module 8.

Theorem 18.1Fermat’s theorem in two variables

If ff has a local maximum or a local minimum at (a,b),(a, b)\mathclose{\text{,}} and the first partial derivatives of ff exist at (a,b),(a, b)\mathclose{\text{,}} then

fx(a,b)=0andfy(a,b)=0,f_x(a, b) = 0 \quad\text{and}\quad f_y(a, b) = 0,

that is, ∇f(a,b)=0.\nabla f(a, b) = \vb{0}\mathclose{\text{.}}

Proof

Suppose that f(a,b)f(a, b) is a local maximum value: f(x,y)≤f(a,b)f(x, y) \le f(a, b) on an open disc of radius rr centred at (a,b)(a, b) that lies in the domain. Walk along the horizontal line through (a,b):(a, b)\mathclose{\text{:}} the function g(x)=f(x,b)g(x) = f(x, b) is defined for ∣x−a∣<r,\abs{x - a} < r\mathclose{\text{,}} since those points (x,b)(x, b) lie in the disc, and g(x)≤g(a)g(x) \le g(a) there. So gg has a local maximum at a.a\mathclose{\text{.}} Its derivative there is g′(a)=fx(a,b),g'(a) = f_x(a, b)\mathclose{\text{,}} which exists, because a partial derivative is the ordinary derivative along such a line (Module 17). By Fermat’s theorem in one variable (Theorem 8.1), g′(a)=0,g'(a) = 0\mathclose{\text{,}} that is, fx(a,b)=0.f_x(a, b) = 0\mathclose{\text{.}} The vertical line through (a,b),(a, b)\mathclose{\text{,}} with h(y)=f(a,y),h(y) = f(a, y)\mathclose{\text{,}} gives fy(a,b)=0f_y(a, b) = 0 in the same way. Finally, a local minimum of ff is a local maximum of −f.-f\mathclose{\text{.}}

∎

Geometrically: where a local extremum has a tangent plane z=f(a,b)+fx(a,b)(x−a)+fy(a,b)(y−b)z = f(a, b) + f_x(a, b)(x - a) + f_y(a, b)(y - b) (Module 17), the plane is horizontal, z=f(a,b).z = f(a, b)\mathclose{\text{.}}

Definition 18.2Critical point

A critical point of ff is a point (a,b)(a, b) of its domain where ∇f(a,b)=0,\nabla f(a, b) = \vb{0}\mathclose{\text{,}} that is, fx(a,b)=fy(a,b)=0,f_x(a, b) = f_y(a, b) = 0\mathclose{\text{,}} or where fxf_x or fyf_y does not exist.

In these words, Theorem 18.1 says: every local extremum is at a critical point. As in one variable, the converse fails. The simplest example is f(x,y)=x2−y2.f(x, y) = x^2 - y^2\mathclose{\text{.}} Its gradient ∇f=⟨2x,−2y⟩\nabla f = \langle 2x, -2y\rangle is 0\vb{0} only at the origin, where f(0,0)=0.f(0, 0) = 0\mathclose{\text{.}} Walk through the origin along the two axes:

ttf(t,0)=t2f(t, 0) = t^2f(0,t)=−t2f(0, t) = -t^2
−1-111−1-1
−0.5-0.50.250.25−0.25-0.25
−0.1-0.10.010.01−0.01-0.01
000000
0.10.10.010.01−0.01-0.01
0.50.50.250.25−0.25-0.25
1111−1-1

Along the xx-axis the origin is the lowest point; along the yy-axis it is the highest. So every disc around the origin contains points where f>0f > 0 and points where f<0,f < 0\mathclose{\text{,}} and f(0,0)=0f(0, 0) = 0 is neither a local maximum nor a local minimum. The surface is shaped like a saddle, or a mountain pass (Figure 18.1).

Left: the surface z = x^2 - y^2. Along the x-axis it follows the parabola z = x^2 (green), lowest at the origin; along the y-axis it follows z = -y^2 (orange), highest at the origin. Right: its level curves x^2 - y^2 = c are hyperbolas, green for c = 1, 2, 3 and orange for c = -1, -2, -3; the level curve c = 0 is the pair of dashed lines y = pm x, which cross at the saddle point.
Figure 18.1

Left: the surface z=x2−y2.z = x^2 - y^2\mathclose{\text{.}} Along the xx-axis it follows the parabola z=x2z = x^2 (green), lowest at the origin; along the yy-axis it follows z=−y2z = -y^2 (orange), highest at the origin. Right: its level curves x2−y2=cx^2 - y^2 = c are hyperbolas, green for c=1,2,3c = 1, 2, 3 and orange for c=−1,−2,−3;c = -1, -2, -3\mathclose{\text{;}} the level curve c=0c = 0 is the pair of dashed lines y=±x,y = \pm x\mathclose{\text{,}} which cross at the saddle point.

Definition 18.3Saddle point

A critical point (a,b)(a, b) in the interior of the domain of ff (so some open disc centred there lies in the domain) is a saddle point if ff has neither a local maximum nor a local minimum there: every open disc centred at (a,b)(a, b) contains domain points where f(x,y)>f(a,b)f(x, y) > f(a, b) and points where f(x,y)<f(a,b).f(x, y) < f(a, b)\mathclose{\text{.}}

Common contour pictures are small closed loops around a hilltop or a hollow, and a level curve crossing itself at a saddle point, like the lines y=±xy = \pm x in Figure 18.1. These pictures are guides, not definitions: a saddle’s level curve need not cross itself, as x2+y3=0x^2 + y^3 = 0 in Example 18.3 will show.

Example 18.1Critical points of a cubic✓ Checked

Find the critical points of f(x,y)=x3+y3−3xy.f(x, y) = x^3 + y^3 - 3xy\mathclose{\text{.}}

ff is a polynomial, so its partial derivatives exist everywhere, and the critical points are the solutions of ∇f=0:\nabla f = \vb{0}\mathclose{\text{:}}

fx=3x2−3y=0,fy=3y2−3x=0.f_x = 3x^2 - 3y = 0, \qquad f_y = 3y^2 - 3x = 0.

The first equation says y=x2,y = x^2\mathclose{\text{,}} the second x=y2.x = y^2\mathclose{\text{.}} Substitute the first into the second: x=(x2)2=x4,x = (x^2)^2 = x^4\mathclose{\text{,}} that is,

x4−x=x(x3−1)=x(x−1)(x2+x+1)=0.x^4 - x = x(x^3 - 1) = x(x - 1)(x^2 + x + 1) = 0.

The quadratic factor has no real zeros, since its discriminant is 1−4=−3<0.1 - 4 = -3 < 0\mathclose{\text{.}} So x=0x = 0 or x=1,x = 1\mathclose{\text{,}} and y=x2y = x^2 gives the two critical points (0,0)(0, 0) and (1,1).(1, 1)\mathclose{\text{.}} Check the second: fx(1,1)=3−3=0f_x(1, 1) = 3 - 3 = 0 and fy(1,1)=3−3=0.f_y(1, 1) = 3 - 3 = 0\mathclose{\text{.}}

Watch out

Three slips with critical points. Calling every critical point an extremum: the origin is a critical point of x2−y2x^2 - y^2 but a saddle point. Forgetting the points where a partial derivative does not exist: the cone f(x,y)=x2+y2f(x, y) = \sqrt{x^2 + y^2} has its absolute minimum 0 at the origin, where fxf_x does not exist, since (f(h,0)−f(0,0))/h=∣h∣/h\big(f(h, 0) - f(0, 0)\big)/h = \abs{h}/h is 1 for h>0h > 0 and −1-1 for h<0.h < 0\mathclose{\text{.}} Losing solutions by dividing: dividing x=x4x = x^4 by xx gives x3=1x^3 = 1 and loses x=0,x = 0\mathclose{\text{,}} the critical point (0,0).(0, 0)\mathclose{\text{.}} Factor instead.

2

The second derivative test

Module 8 told a peak from a valley by the sign of f′′(c).f''(c)\mathclose{\text{.}} In two variables the test uses all three second partial derivatives, and the reason is a quadratic approximation.

The quadratic approximation. Let ff have continuous second partial derivatives near (a,b),(a, b)\mathclose{\text{,}} and look at ff along the line from (a,b)(a, b) towards (a+h,b+k):(a + h, b + k)\mathclose{\text{:}} g(t)=f(a+th,b+tk).g(t) = f(a + th, b + tk)\mathclose{\text{.}} By the chain rule of Module 17, g′(t)=fxh+fyk,g'(t) = f_x h + f_y k\mathclose{\text{,}} and differentiating again, with Clairaut’s theorem fyx=fxy,f_{yx} = f_{xy}\mathclose{\text{,}}

g′′(t)=fxxh2+2fxyhk+fyyk2,g''(t) = f_{xx}h^2 + 2f_{xy}hk + f_{yy}k^2,

where the partial derivatives are taken at (a+th,b+tk).(a + th, b + tk)\mathclose{\text{.}} The Taylor polynomial of degree 2 of gg at 0 (Module 15), evaluated at t=1,t = 1\mathclose{\text{,}} gives

f(a+h,b+k)≈f(a,b)+fxh+fyk+12(fxxh2+2fxyhk+fyyk2),\begin{aligned} &f(a + h, b + k) \\ &\quad\approx f(a, b) + f_x h + f_y k \\ &\qquad + \tfrac12\big(f_{xx}h^2 + 2f_{xy}hk + f_{yy}k^2\big), \end{aligned}

with every partial derivative now taken at (a,b).(a, b)\mathclose{\text{.}} The first three terms are the linear approximation of Module 17. At a critical point fx=fy=0,f_x = f_y = 0\mathclose{\text{,}} so near (a,b)(a, b)

f(a+h,b+k)−f(a,b)≈12 Q(h,k),Q(h,k)=Ah2+2Bhk+Ck2,\begin{gathered} f(a + h, b + k) - f(a, b) \approx \tfrac12\,Q(h, k), \\ Q(h, k) = Ah^2 + 2Bhk + Ck^2, \end{gathered}

where A=fxx(a,b),A = f_{xx}(a, b)\mathclose{\text{,}} B=fxy(a,b)B = f_{xy}(a, b) and C=fyy(a,b).C = f_{yy}(a, b)\mathclose{\text{.}} Near a critical point the graph looks like the graph of the quadratic Q,Q\mathclose{\text{,}} so the question becomes: when is Q(h,k)Q(h, k) positive, when negative, and when both?

The sign of a quadratic. If A≠0,A \ne 0\mathclose{\text{,}} complete the square:

Ah2+2Bhk+Ck2=A(h+BAk)2+AC−B2A k2.\begin{aligned} &Ah^2 + 2Bhk + Ck^2 \\ &\quad= A\Big(h + \frac{B}{A}k\Big)^2 + \frac{AC - B^2}{A}\,k^2. \end{aligned}
  • If AC−B2>0,AC - B^2 > 0\mathclose{\text{,}} both terms have the sign of A,A\mathclose{\text{,}} and both are 0 only when k=0k = 0 and then h=0.h = 0\mathclose{\text{.}} So Q(h,k)Q(h, k) has the sign of AA for every (h,k)≠(0,0):(h, k) \ne (0, 0)\mathclose{\text{:}} a bowl if A>0,A > 0\mathclose{\text{,}} a dome if A<0.A < 0\mathclose{\text{.}}
  • If AC−B2<0,AC - B^2 < 0\mathclose{\text{,}} then Q(1,0)=A,Q(1, 0) = A\mathclose{\text{,}} while along the line h=−BAkh = -\tfrac{B}{A}k the first term vanishes and QQ has the opposite sign: QQ takes both signs, as on a saddle. (If A=0A = 0 and B≠0,B \ne 0\mathclose{\text{,}} Q=k(2Bh+Ck)Q = k(2Bh + Ck) takes both signs too.)
  • If AC−B2=0,AC - B^2 = 0\mathclose{\text{,}} QQ is 0 along a whole line through the origin, and the quadratic cannot decide.

The three models are h2+k2h^2 + k^2 (a bowl), −h2−k2-h^2 - k^2 (a dome) and h2−k2h^2 - k^2 (the saddle of Figure 18.1). The quadratic 2hk,2hk\mathclose{\text{,}} with A=C=0A = C = 0 and B=1,B = 1\mathclose{\text{,}} is the same saddle turned through 45∘:45^\circ\mathclose{\text{:}} with h=(u−v)/2h = (u - v)/\sqrt2 and k=(u+v)/2,k = (u + v)/\sqrt2\mathclose{\text{,}} it becomes u2−v2.u^2 - v^2\mathclose{\text{.}}

Definition 18.4The Hessian and its determinant

The Hessian matrix of ff at (a,b)(a, b) is the square array of its second partial derivatives there,

H=(fxxfxyfyxfyy),H = \begin{pmatrix} f_{xx} & f_{xy} \\ f_{yx} & f_{yy} \end{pmatrix},

and, when fyx=fxy,f_{yx} = f_{xy}\mathclose{\text{,}} its determinant is the number

D=fxxfyy−fxy2.D = f_{xx}f_{yy} - f_{xy}^2.

(A rectangular array of numbers is a matrix; the determinant of a two-by-two matrix (pqrs)\begin{pmatrix} p & q \\ r & s \end{pmatrix} is ps−qr.)ps - qr\mathclose{\text{.)}}

DD is the AC−B2AC - B^2 of the completed square. Mathematical Foundations studies matrices in its linear algebra modules, and in its Module 13 the quadratic forms behind this test in any number of variables.

Theorem 18.2The second derivative test

Suppose that the second partial derivatives of ff are continuous on an open disc centred at (a,b),(a, b)\mathclose{\text{,}} and that fx(a,b)=0f_x(a, b) = 0 and fy(a,b)=0.f_y(a, b) = 0\mathclose{\text{.}} Let D=fxx(a,b)fyy(a,b)−fxy(a,b)2.D = f_{xx}(a, b)f_{yy}(a, b) - f_{xy}(a, b)^2\mathclose{\text{.}}

  • If D>0D > 0 and fxx(a,b)>0,f_{xx}(a, b) > 0\mathclose{\text{,}} then f(a,b)f(a, b) is a local minimum value.
  • If D>0D > 0 and fxx(a,b)<0,f_{xx}(a, b) < 0\mathclose{\text{,}} then f(a,b)f(a, b) is a local maximum value.
  • If D<0,D < 0\mathclose{\text{,}} then (a,b)(a, b) is a saddle point.
  • If D=0,D = 0\mathclose{\text{,}} the test is inconclusive.

When D>0,D > 0\mathclose{\text{,}} fxxfyy>fxy2≥0,f_{xx}f_{yy} > f_{xy}^2 \ge 0\mathclose{\text{,}} so fxxf_{xx} and fyyf_{yy} have the same sign, and either one decides. The continuity of the second partial derivatives is what makes the quadratic QQ describe ff near (a,b);(a, b)\mathclose{\text{;}} the proof below shows where it is used.

Going deeperWhy the second derivative test works

A first reading may skip this proof. Take (h,k)≠(0,0)(h, k) \ne (0, 0) so small that the segment from (a,b)(a, b) to (a+h,b+k)(a + h, b + k) lies in the disc, and let g(t)=f(a+th,b+tk)g(t) = f(a + th, b + tk) for 0≤t≤1,0 \le t \le 1\mathclose{\text{,}} as above. Since the second partial derivatives are continuous, fxf_x and fyf_y are differentiable, so the chain rule applies twice, and g′′(t)=fxxh2+2fxyhk+fyyk2,g''(t) = f_{xx}h^2 + 2f_{xy}hk + f_{yy}k^2\mathclose{\text{,}} with the partial derivatives at (a+th,b+tk).(a + th, b + tk)\mathclose{\text{.}} Taylor’s theorem with n=1n = 1 (Module 15) gives g(1)=g(0)+g′(0)+12g′′(c)g(1) = g(0) + g'(0) + \tfrac12g''(c) for some cc between 0 and 1. Since g′(0)=fx(a,b)h+fy(a,b)k=0,g'(0) = f_x(a, b)h + f_y(a, b)k = 0\mathclose{\text{,}}

f(a+h,b+k)−f(a,b)=12(fxxh2+2fxyhk+fyyk2),(∗)\begin{aligned} &f(a + h, b + k) - f(a, b) \\ &\quad= \tfrac12\big(f_{xx}h^2 + 2f_{xy}hk + f_{yy}k^2\big), \end{aligned} \tag{$*$}

with the partial derivatives at (a+ch,b+ck).(a + ch, b + ck)\mathclose{\text{.}}

Minimum. Suppose D>0D > 0 and fxx>0f_{xx} > 0 at (a,b).(a, b)\mathclose{\text{.}} The functions fxxf_{xx} and fxxfyy−fxy2f_{xx}f_{yy} - f_{xy}^2 are continuous and positive at (a,b),(a, b)\mathclose{\text{,}} so they stay positive on some smaller disc centred at (a,b).(a, b)\mathclose{\text{.}} If (a+h,b+k)(a + h, b + k) lies in that disc, so does (a+ch,b+ck),(a + ch, b + ck)\mathclose{\text{,}} and at that point the completed square shows that the bracket in (∗)(*) is positive. So f(a+h,b+k)>f(a,b)f(a + h, b + k) > f(a, b) for every such (h,k)≠(0,0):(h, k) \ne (0, 0)\mathclose{\text{:}} a local minimum. Maximum. Apply this to −f.-f\mathclose{\text{.}}

Saddle. Suppose D<0D < 0 at (a,b).(a, b)\mathclose{\text{.}} The completed square gives directions (h1,k1)(h_1, k_1) and (h2,k2)(h_2, k_2) with Q(h1,k1)>0Q(h_1, k_1) > 0 and Q(h2,k2)<0Q(h_2, k_2) < 0 for the second partial derivatives at (a,b).(a, b)\mathclose{\text{.}} Put (h,k)=(sh1,sk1)(h, k) = (sh_1, sk_1) in (∗):(*)\mathclose{\text{:}} then f(a+sh1,b+sk1)−f(a,b)=12s2(fxxh12+2fxyh1k1+fyyk12)f(a + sh_1, b + sk_1) - f(a, b) = \tfrac12s^2\big(f_{xx}h_1^2 + 2f_{xy}h_1k_1 + f_{yy}k_1^2\big) at a point between (a,b)(a, b) and (a+sh1,b+sk1).(a + sh_1, b + sk_1)\mathclose{\text{.}} As s→0s \to 0 that point tends to (a,b),(a, b)\mathclose{\text{,}} and by continuity the bracket tends to Q(h1,k1)>0;Q(h_1, k_1) > 0\mathclose{\text{;}} so f>f(a,b)f > f(a, b) at points (a+sh1,b+sk1)(a + sh_1, b + sk_1) with small s≠0.s \ne 0\mathclose{\text{.}} In the same way f<f(a,b)f < f(a, b) at points (a+sh2,b+sk2)(a + sh_2, b + sk_2) with small s≠0.s \ne 0\mathclose{\text{.}} Every disc centred at (a,b)(a, b) contains both kinds of point: a saddle point. Inconclusive. Example 18.3 shows three functions with D=0:D = 0\mathclose{\text{:}} a minimum, and two saddle points.

Example 18.2Classifying the critical points of the cubic✓ Checked

Classify the critical points (0,0)(0, 0) and (1,1)(1, 1) of f(x,y)=x3+y3−3xyf(x, y) = x^3 + y^3 - 3xy (Example 18.1), and decide whether ff has absolute extreme values.

The second partial derivatives are fxx=6x,f_{xx} = 6x\mathclose{\text{,}} fxy=−3f_{xy} = -3 and fyy=6y,f_{yy} = 6y\mathclose{\text{,}} all continuous, so

D=(6x)(6y)−(−3)2=36xy−9.D = (6x)(6y) - (-3)^2 = 36xy - 9.
  • At (0,0):(0, 0)\mathclose{\text{:}} D=−9<0,D = -9 < 0\mathclose{\text{,}} a saddle point. You can see it directly: along the line y=x,y = x\mathclose{\text{,}} f(t,t)=2t3−3t2=t2(2t−3)<0f(t, t) = 2t^3 - 3t^2 = t^2(2t - 3) < 0 for t≠0t \ne 0 near 0, while along y=−x,y = -x\mathclose{\text{,}} f(t,−t)=3t2>0.f(t, -t) = 3t^2 > 0\mathclose{\text{.}}
  • At (1,1):(1, 1)\mathclose{\text{:}} D=36−9=27>0D = 36 - 9 = 27 > 0 and fxx=6>0,f_{xx} = 6 > 0\mathclose{\text{,}} so f(1,1)=1+1−3=−1f(1, 1) = 1 + 1 - 3 = -1 is a local minimum value.

The local minimum is not absolute: along the xx-axis f(x,0)=x3,f(x, 0) = x^3\mathclose{\text{,}} which tends to −∞-\infty as x→−∞x \to -\infty and to ∞\infty as x→∞.x \to \infty\mathclose{\text{.}} So ff has no absolute minimum and no absolute maximum. Figure 18.2 shows the level curves: small closed loops around (1,1),(1, 1)\mathclose{\text{,}} and the level curve f=0f = 0 crossing itself at the saddle point.

Level curves of f(x, y) = x^3 + y^3 - 3xy. Around the local minimum at (1, 1) the level curves f = -0.8 and f = -0.4 (blue) are closed loops; through the saddle point at the origin passes the level curve f = 0 (purple), which crosses itself there. The dashed square and the dots belong to Example 18.4: they mark the candidates for the absolute extreme values of f on the square.
Figure 18.2

Level curves of f(x,y)=x3+y3−3xy.f(x, y) = x^3 + y^3 - 3xy\mathclose{\text{.}} Around the local minimum at (1,1)(1, 1) the level curves f=−0.8f = -0.8 and f=−0.4f = -0.4 (blue) are closed loops; through the saddle point at the origin passes the level curve f=0f = 0 (purple), which crosses itself there. The dashed square and the dots belong to Example 18.4: they mark the candidates for the absolute extreme values of ff on the square.

Example 18.3When D=0D = 0✓ Checked

Show that f1=x2+y4,f_1 = x^2 + y^4\mathclose{\text{,}} f2=x2−y4f_2 = x^2 - y^4 and f3=x2+y3f_3 = x^2 + y^3 each have exactly one critical point, the origin, with D=0D = 0 there, and decide what each has at the origin.

The gradients are ⟨2x,4y3⟩,\langle 2x, 4y^3\rangle\mathclose{\text{,}} ⟨2x,−4y3⟩\langle 2x, -4y^3\rangle and ⟨2x,3y2⟩,\langle 2x, 3y^2\rangle\mathclose{\text{,}} which are 0\vb{0} only at (0,0).(0, 0)\mathclose{\text{.}} At the origin all three have fxx=2,f_{xx} = 2\mathclose{\text{,}} fxy=0f_{xy} = 0 and fyy=0f_{yy} = 0 (it is 12y2,12y^2\mathclose{\text{,}} −12y2-12y^2 and 6y),6y\mathclose{\text{),}} so D=2⋅0−02=0:D = 2 \cdot 0 - 0^2 = 0\mathclose{\text{:}} the test is inconclusive, and you must look at ff itself.

  • f1(x,y)=x2+y4≥0=f1(0,0):f_1(x, y) = x^2 + y^4 \ge 0 = f_1(0, 0)\mathclose{\text{:}} an absolute minimum.
  • f2(x,0)=x2>0f_2(x, 0) = x^2 > 0 for x≠0,x \ne 0\mathclose{\text{,}} but f2(0,y)=−y4<0f_2(0, y) = -y^4 < 0 for y≠0:y \ne 0\mathclose{\text{:}} a saddle point.
  • f3(0,y)=y3f_3(0, y) = y^3 is positive for y>0y > 0 and negative for y<0:y < 0\mathclose{\text{:}} a saddle point, although fxx>0f_{xx} > 0 and no direction makes Q=2h2Q = 2h^2 negative. Its zero level curve x2=−y3x^2 = -y^3 has a cusp at the origin, not a crossing.
Watch out

Three slips with the test. Reading fxxf_{xx} when D<0D < 0: then the point is a saddle point, whatever the sign of fxx.f_{xx}\mathclose{\text{.}} Checking only fxxf_{xx} and fyyf_{yy}: f(x,y)=x2+3xy+y2f(x, y) = x^2 + 3xy + y^2 has fxx=fyy=2>0f_{xx} = f_{yy} = 2 > 0 at its critical point (0,0),(0, 0)\mathclose{\text{,}} but D=4−9=−5<0,D = 4 - 9 = -5 < 0\mathclose{\text{,}} and indeed f(t,−t)=−t2<0:f(t, -t) = -t^2 < 0\mathclose{\text{:}} the mixed term 3xy3xy wins along the line y=−x.y = -x\mathclose{\text{.}} Treating D=0D = 0 as a saddle point: it means only that the test is inconclusive (Example 18.3).

Compute itClassifying critical points with SymPy

sp.hessian builds the Hessian matrix, and solve with a list of equations solves the system ∇f=0.\nabla f = \vb{0}\mathclose{\text{.}} Declare the symbols real, so that only real solutions come back:

Python
import sympy as sp
x, y = sp.symbols('x y', real=True)
f = x**3 + y**3 - 3*x*y
crit = sp.solve([sp.diff(f, x), sp.diff(f, y)], [x, y], dict=True)
H = sp.hessian(f, (x, y))
print(crit)
print(H, ' D =', H.det())
Output
[{x: 0, y: 0}, {x: 1, y: 1}]
Matrix([[6*x, -3], [-3, 6*y]])  D = 36*x*y - 9

The test itself is three comparisons:

Python
def classify(D, fxx):
    if D > 0:
        return 'local minimum' if fxx > 0 else 'local maximum'
    if D < 0:
        return 'saddle point'
    return 'inconclusive'

for p in crit:
    D, fxx = H.det().subs(p), H[0, 0].subs(p)
    print(p, ' D =', D, ' f_xx =', fxx, ':', classify(D, fxx), ' f =', f.subs(p))
Output
{x: 0, y: 0}  D = -9  f_xx = 0 : saddle point  f = 0
{x: 1, y: 1}  D = 27  f_xx = 6 : local minimum  f = -1

Without real=True, solve also returns the points where xx is one of the two complex cube roots of 1 (the zeros of x2+x+1),x^2 + x + 1\mathclose{\text{),}} which have no place on a map of the plane:

Python
u, v = sp.symbols('u v')                  # no assumptions
g = u**3 + v**3 - 3*u*v
print(len(sp.solve([sp.diff(g, u), sp.diff(g, v)], [u, v], dict=True)), 'solutions')
Output
4 solutions
3

Absolute extrema on closed bounded regions

Module 8 found the absolute extreme values of a continuous function on a closed interval [a,b][a, b] by comparing its values at the critical numbers and at the two endpoints. In two variables the interval becomes a region, and we must check its whole boundary, usually made of curves.

Definition 18.5Interior, boundary, closed and bounded

Let RR be a set of points of the plane. A point PP is an interior point of RR if some open disc centred at PP lies in R,R\mathclose{\text{,}} and a boundary point of RR if every open disc centred at PP contains points of RR and points not in R.R\mathclose{\text{.}} The set RR is closed if it contains all its boundary points, and bounded if it lies inside some disc.

The disc x2+y2≤1x^2 + y^2 \le 1 is closed and bounded; its boundary is the circle x2+y2=1.x^2 + y^2 = 1\mathclose{\text{.}} The open disc x2+y2<1x^2 + y^2 < 1 is bounded but not closed, since it leaves out the whole circle. A rectangle with its sides, the set of points with a≤x≤ba \le x \le b and c≤y≤d,c \le y \le d\mathclose{\text{,}} is closed and bounded, and so is a triangle with its sides. The half-plane y≥0y \ge 0 is closed but not bounded.

Theorem 18.3The Extreme Value Theorem in two variables

If ff is continuous on a nonempty, closed, bounded set RR in the plane, then ff has an absolute maximum value f(x1,y1)f(x_1, y_1) and an absolute minimum value f(x2,y2)f(x_2, y_2) on R,R\mathclose{\text{,}} at some points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) of R.R\mathclose{\text{.}}

As for the one-variable theorem of Module 3, the proof belongs to real analysis, and this series uses the theorem without proof; it holds in three or more variables too. Each hypothesis is needed. On the open disc x2+y2<1,x^2 + y^2 < 1\mathclose{\text{,}} f(x,y)=x+yf(x, y) = x + y has no maximum: its values come as close to 2\sqrt2 as you like near (12,12),\big(\tfrac{1}{\sqrt2}, \tfrac{1}{\sqrt2}\big)\mathclose{\text{,}} a point of the missing circle, but never reach it. On the half-plane y≥0y \ge 0 the same ff is not bounded. And on the closed disc, the function equal to x+yx + y except at (12,12),\big(\tfrac{1}{\sqrt2}, \tfrac{1}{\sqrt2}\big)\mathclose{\text{,}} where it is 0, is not continuous and has no maximum.

Theorem 18.4Where the absolute extreme values are

Let ff be continuous on a nonempty, closed, bounded set R.R\mathclose{\text{.}} Then ff has an absolute maximum value and an absolute minimum value on R,R\mathclose{\text{,}} and each of them is taken at a boundary point of RR or at a critical point of ff that is an interior point of R.R\mathclose{\text{.}}

Proof

By Theorem 18.3, the absolute maximum value on RR is f(P)f(P) for some point PP of R.R\mathclose{\text{.}} If PP is a boundary point, there is nothing to prove. Otherwise PP is an interior point: some open disc centred at PP lies in R,R\mathclose{\text{,}} and f(P)≥ff(P) \ge f on that disc, so f(P)f(P) is a local maximum value. If both partial derivatives exist at P,P\mathclose{\text{,}} they are 0 by Theorem 18.1; if not, PP is a critical point by definition. The minimum is the same.

∎
Key idea

The closed region method. To find the absolute extreme values of a continuous function ff on a nonempty, closed, bounded region R:R\mathclose{\text{:}}

  1. Find the critical points of ff inside R,R\mathclose{\text{,}} and the values of ff there.
  2. Find the largest and the smallest values of ff on the boundary of R.R\mathclose{\text{.}} Usually the boundary consists of pieces (the sides of a polygon, arcs of curves) on each of which ff becomes a function of one variable: substitute the equation of a side, such as y=2,y = 2\mathclose{\text{,}} or a parametrisation, such as (cos⁡t,sin⁡t)(\cos t, \sin t) on a circle, and use the closed interval method of Module 8, with the ends of each piece.
  3. The largest of all these values is the absolute maximum value; the smallest is the absolute minimum value.

As in one variable, no test of the critical points is needed: you compare a list of numbers.

Example 18.4The extreme values on a square✓ Checked

Find the absolute maximum and minimum values of f(x,y)=x3+y3−3xyf(x, y) = x^3 + y^3 - 3xy on the square RR of points with −1≤x≤2-1 \le x \le 2 and −1≤y≤2.-1 \le y \le 2\mathclose{\text{.}}

ff is a polynomial, so continuous, and the square is closed and bounded: the extreme values exist (Theorem 18.3).

Step 1: inside. The critical points are (0,0)(0, 0) and (1,1)(1, 1) (Example 18.1), both inside the square, with f(0,0)=0f(0, 0) = 0 and f(1,1)=−1.f(1, 1) = -1\mathclose{\text{.}} The saddle point is a candidate like any other: the method compares values, it does not classify.

Step 2: the boundary. The square has four sides. Since f(x,y)=f(y,x),f(x, y) = f(y, x)\mathclose{\text{,}} the bottom side y=−1y = -1 behaves like the left side x=−1,x = -1\mathclose{\text{,}} and the top side y=2y = 2 like the right side x=2,x = 2\mathclose{\text{,}} with xx and yy swapped.

  • Left side, x=−1x = -1 and −1≤y≤2:-1 \le y \le 2\mathclose{\text{:}} f(−1,y)=y3+3y−1.f(-1, y) = y^3 + 3y - 1\mathclose{\text{.}} Its derivative 3y2+33y^2 + 3 is positive, so it is increasing, and its extreme values are at the ends: f(−1,−1)=−5f(-1, -1) = -5 and f(−1,2)=13.f(-1, 2) = 13\mathclose{\text{.}}
  • Right side, x=2x = 2 and −1≤y≤2:-1 \le y \le 2\mathclose{\text{:}} f(2,y)=y3−6y+8.f(2, y) = y^3 - 6y + 8\mathclose{\text{.}} Its derivative 3y2−63y^2 - 6 is 0 at y=±2,y = \pm\sqrt2\mathclose{\text{,}} and only 2\sqrt2 lies in [−1,2].[-1, 2]\mathclose{\text{.}} The values are f(2,2)=22−62+8=8−42≈2.343f(2, \sqrt2) = 2\sqrt2 - 6\sqrt2 + 8 = 8 - 4\sqrt2 \approx 2.343 and, at the ends, f(2,−1)=−1+6+8=13f(2, -1) = -1 + 6 + 8 = 13 and f(2,2)=8−12+8=4.f(2, 2) = 8 - 12 + 8 = 4\mathclose{\text{.}}
  • Bottom and top sides, by the symmetry: −5-5 at (−1,−1)(-1, -1) and 1313 at (2,−1);(2, -1)\mathclose{\text{;}} 8−428 - 4\sqrt2 at (2,2),(\sqrt2, 2)\mathclose{\text{,}} 1313 at (−1,2)(-1, 2) and 44 at (2,2).(2, 2)\mathclose{\text{.}}

Step 3: compare.

Point Where it comes from ff
(0,0)(0, 0) critical point, saddle 00
(1,1)(1, 1) critical point, local minimum −1-1
(2,2),(2, \sqrt2)\mathclose{\text{,}} (2,2)(\sqrt2, 2) critical points of ff on a side 8−42≈2.3438 - 4\sqrt2 \approx 2.343
(−1,−1)(-1, -1) corner −5-5
(2,−1),(2, -1)\mathclose{\text{,}} (−1,2)(-1, 2) corners 1313
(2,2)(2, 2) corner 44

The absolute maximum value is 13, at (2,−1)(2, -1) and at (−1,2);(-1, 2)\mathclose{\text{;}} the absolute minimum value is −5,-5\mathclose{\text{,}} at the corner (−1,−1).(-1, -1)\mathclose{\text{.}} All three are corners, and the local minimum at (1,1)(1, 1) is not the absolute minimum on the square (Figure 18.2).

Watch out

Three slips. Forgetting the ends of the boundary pieces: in Example 18.4 both extreme values are at corners, which no derivative finds. Classifying instead of comparing: the local minimum −1-1 is not the smallest value on the square, and a saddle point can be a candidate too. Keeping points outside the region: y=−2y = -\sqrt2 solves 3y2−6=0,3y^2 - 6 = 0\mathclose{\text{,}} but (2,−2)(2, -\sqrt2) is not on the square.

4

Lagrange multipliers

Many problems ask for the best point of a curve or a surface: the largest rectangle that fits in an ellipse, the point of a curve closest to a given point, the box with the most volume for a given amount of cardboard. Their variables are tied by an equation g(x,y)=c,g(x, y) = c\mathclose{\text{,}} a constraint. Section 18.3 parametrised the boundary; that works when a parametrisation is easy to find. The method of this section works with the equation itself, and its picture explains every answer.

Take the largest rectangle, with sides parallel to the axes, inside the ellipse x24+y2=1.\tfrac{x^2}{4} + y^2 = 1\mathclose{\text{.}} We can centre it at the origin: moving any such rectangle there decreases or preserves the largest absolute xx-coordinate and the largest absolute yy-coordinate of its corners, so it still fits and has the same area. Then enlarge it until its corners reach the ellipse. Thus we can take the corners to be (±x,±y)(\pm x, \pm y) with (x,y)(x, y) on the ellipse, x≥0x \ge 0 and y≥0,y \ge 0\mathclose{\text{,}} and its area is A=(2x)(2y)=4xy.A = (2x)(2y) = 4xy\mathclose{\text{.}} Numbers first. The points (2cos⁡t,sin⁡t)(2\cos t, \sin t) lie on the ellipse, since 4cos⁡2t4+sin⁡2t=1,\tfrac{4\cos^2 t}{4} + \sin^2 t = 1\mathclose{\text{,}} and 0≤t≤π20 \le t \le \tfrac{\pi}{2} gives the quarter in the first quadrant:

ttx=2cos⁡tx = 2\cos ty=sin⁡ty = \sin tA=4xyA = 4xy
00220000
π12\frac{\pi}{12}1.9321.9320.25880.258822
π6\frac{\pi}{6}1.7321.7320.50.53.4643.464
π4\frac{\pi}{4}1.4141.4140.70710.707144
π3\frac{\pi}{3}110.86600.86603.4643.464
5π12\frac{5 \pi}{12}0.51760.51760.96590.965922
π2\frac{\pi}{2}001100

The largest area in the table is 4, at t=π4,t = \tfrac{\pi}{4}\mathclose{\text{,}} the point (2,22)≈(1.414,0.7071).\big(\sqrt2, \tfrac{\sqrt2}{2}\big) \approx (1.414, 0.7071)\mathclose{\text{.}} In fact A=8cos⁡tsin⁡t=4sin⁡2t,A = 8\cos t\sin t = 4\sin 2t\mathclose{\text{,}} whose largest value is 4.

Now the picture (Figure 18.3). For k>0,k > 0\mathclose{\text{,}} the level curves of the area, 4xy=k,4xy = k\mathclose{\text{,}} are hyperbolas. When 0<k<40 < k < 4 the level curve crosses the quarter of the ellipse twice: two rectangles in the ellipse have that area. As kk grows towards 4, the two crossing points move together, and at k=4k = 4 they merge: the level curve touches the ellipse. For k>4k > 4 it misses the ellipse altogether. Where the two curves touch they have the same tangent line, so the vectors perpendicular to them are parallel. The gradient of a differentiable function is perpendicular to its level curves where it is nonzero (Module 17), and the ellipse is the level curve g=1g = 1 of g(x,y)=x24+y2.g(x, y) = \tfrac{x^2}{4} + y^2\mathclose{\text{.}} Both gradients are nonzero at the best point, so ∇A\nabla A is parallel to ∇g:\nabla g\mathclose{\text{:}} ∇A=λ∇g\nabla A = \lambda\nabla g for some number λ,\lambda\mathclose{\text{,}} read “lambda”.

The ellipse tfrac{x^2}{4} + y^2 = 1 (green) and the level curves A = 2, 4 and 6 of the area A = 4xy. The level curve A = 4 (orange) touches the ellipse at big(sqrt2, tfrac{sqrt2}{2}big), a corner of the largest rectangle (shaded); there nabla A and nabla g point the same way, and nabla A is 4 times as long (both arrows are drawn at a quarter of their length). Where A = 2 crosses the ellipse, at (2cos 75^circ, sin 75^circ), the gradients are not parallel, and moving along the ellipse towards the touching point increases A. A = 6 misses the ellipse.
Figure 18.3

The ellipse x24+y2=1\tfrac{x^2}{4} + y^2 = 1 (green) and the level curves A=2,A = 2\mathclose{\text{,}} 44 and 66 of the area A=4xy.A = 4xy\mathclose{\text{.}} The level curve A=4A = 4 (orange) touches the ellipse at (2,22),\big(\sqrt2, \tfrac{\sqrt2}{2}\big)\mathclose{\text{,}} a corner of the largest rectangle (shaded); there ∇A\nabla A and ∇g\nabla g point the same way, and ∇A\nabla A is 4 times as long (both arrows are drawn at a quarter of their length). Where A=2A = 2 crosses the ellipse, at (2cos⁡75∘,sin⁡75∘),(2\cos 75^\circ, \sin 75^\circ)\mathclose{\text{,}} the gradients are not parallel, and moving along the ellipse towards the touching point increases A.A\mathclose{\text{.}} A=6A = 6 misses the ellipse.

Theorem 18.5Lagrange multipliers

Let ff and gg have continuous first partial derivatives near a point (a,b)(a, b) of the curve g(x,y)=c,g(x, y) = c\mathclose{\text{,}} and suppose that ∇g(a,b)≠0.\nabla g(a, b) \ne \vb{0}\mathclose{\text{.}} If f,f\mathclose{\text{,}} restricted to the curve, has a local maximum or a local minimum at (a,b)(a, b) (that is, f(a,b)≥f(x,y),f(a, b) \ge f(x, y)\mathclose{\text{,}} or f(a,b)≤f(x,y),f(a, b) \le f(x, y)\mathclose{\text{,}} for every point (x,y)(x, y) of the curve in some disc centred at (a,b)),(a, b)\mathclose{\text{),}} then there is a number λ\lambda with

∇f(a,b)=λ ∇g(a,b).\nabla f(a, b) = \lambda\,\nabla g(a, b).
Proof

Since ∇g(a,b)≠0,\nabla g(a, b) \ne \vb{0}\mathclose{\text{,}} gx(a,b)g_x(a, b) or gy(a,b)g_y(a, b) is not 0; say gy(a,b)≠0g_y(a, b) \ne 0 (otherwise exchange the roles of xx and y).y\mathclose{\text{).}} By the implicit function theorem of Module 17, applied to g(x,y)−c,g(x, y) - c\mathclose{\text{,}} near (a,b)(a, b) the points of the curve form the graph of a differentiable function y=φ(x)y = \varphi(x) with φ(a)=b.\varphi(a) = b\mathclose{\text{.}} So for small tt the curve is traced by r(t)=⟨a+t,φ(a+t)⟩,\vb{r}(t) = \langle a + t, \varphi(a + t)\rangle\mathclose{\text{,}} with r(0)=⟨a,b⟩\vb{r}(0) = \langle a, b\rangle and r′(0)=⟨1,φ′(a)⟩.\vb{r}'(0) = \langle 1, \varphi'(a)\rangle\mathclose{\text{.}}

The function k(t)=f(r(t))k(t) = f\big(\vb{r}(t)\big) has a local extremum at t=0,t = 0\mathclose{\text{,}} so k′(0)=0k'(0) = 0 by Fermat’s theorem (Theorem 8.1), and by the chain rule (Module 17) k′(0)=∇f(a,b)⋅r′(0).k'(0) = \nabla f(a, b) \cdot \vb{r}'(0)\mathclose{\text{.}} Also g(r(t))=cg\big(\vb{r}(t)\big) = c for every t,t\mathclose{\text{,}} and differentiating gives ∇g(a,b)⋅r′(0)=0.\nabla g(a, b) \cdot \vb{r}'(0) = 0\mathclose{\text{.}} So both gradients are perpendicular to r′(0)=⟨1,φ′(a)⟩.\vb{r}'(0) = \langle 1, \varphi'(a)\rangle\mathclose{\text{.}} A vector ⟨u,v⟩\langle u, v\rangle perpendicular to it has u+φ′(a)v=0,u + \varphi'(a)v = 0\mathclose{\text{,}} so it is v⟨−φ′(a),1⟩:v\langle -\varphi'(a), 1\rangle\mathclose{\text{:}} both gradients are multiples of ⟨−φ′(a),1⟩,\langle -\varphi'(a), 1\rangle\mathclose{\text{,}} namely ∇f(a,b)=fy(a,b)⟨−φ′(a),1⟩\nabla f(a, b) = f_y(a, b)\langle -\varphi'(a), 1\rangle and ∇g(a,b)=gy(a,b)⟨−φ′(a),1⟩.\nabla g(a, b) = g_y(a, b)\langle -\varphi'(a), 1\rangle\mathclose{\text{.}} Since gy(a,b)≠0,g_y(a, b) \ne 0\mathclose{\text{,}} ∇f(a,b)=λ∇g(a,b)\nabla f(a, b) = \lambda\nabla g(a, b) with λ=fy(a,b)/gy(a,b).\lambda = f_y(a, b)/g_y(a, b)\mathclose{\text{.}}

∎

The number λ\lambda is a Lagrange multiplier. The same holds in three variables, with ff and gg again having continuous first partial derivatives near the point: if f(x,y,z),f(x, y, z)\mathclose{\text{,}} restricted to the surface g(x,y,z)=c,g(x, y, z) = c\mathclose{\text{,}} has a local extremum at a point where ∇g≠0,\nabla g \ne \vb{0}\mathclose{\text{,}} then ∇f=λ∇g\nabla f = \lambda\nabla g there. The proof is the same, with the three-variable implicit function theorem, applied to every curve in the surface through the point: ∇f\nabla f is perpendicular to all their tangent vectors, which fill the plane perpendicular to ∇g,\nabla g\mathclose{\text{,}} so ∇f\nabla f is a multiple of ∇g.\nabla g\mathclose{\text{.}} The multiple can be 0: the theorem allows ∇f=0.\nabla f = \vb{0}\mathclose{\text{.}}

Key idea

The method of Lagrange multipliers. Suppose ff and gg have continuous first partial derivatives near the constraint curve. To find the extreme values of f(x,y)f(x, y) subject to g(x,y)=c:g(x, y) = c\mathclose{\text{:}}

  1. Solve the equations fx=λgx,f_x = \lambda g_x\mathclose{\text{,}} fy=λgyf_y = \lambda g_y and g(x,y)=cg(x, y) = c for x,x\mathclose{\text{,}} yy and λ.\lambda\mathclose{\text{.}}
  2. Add the points of the curve where ∇g=0,\nabla g = \vb{0}\mathclose{\text{,}} and the ends of the curve if it has ends.
  3. Evaluate ff at all these points. If the curve is nonempty, closed and bounded, like a circle or an ellipse, the largest value is the maximum and the smallest the minimum (Theorem 18.3). Otherwise an extra argument must show that an extremum exists.

In three variables, solve ∇f=λ∇g\nabla f = \lambda\nabla g and g(x,y,z)=c:g(x, y, z) = c\mathclose{\text{:}} four equations for x,x\mathclose{\text{,}} y,y\mathclose{\text{,}} zz and λ.\lambda\mathclose{\text{.}}

Try it

Choose ff and the constraint, and move the point along the curve with the slider or the arrow keys. Orange: ∇f\nabla f and the level curve of ff through the point; green: ∇g.\nabla g\mathclose{\text{.}} Start with xyxy on the ellipse: it is one-quarter of the rectangle area A=4xyA = 4xy in Example 18.5, so at its maximum the widget gives λ=1,\lambda = 1\mathclose{\text{,}} while the area’s multiplier is 4. Find where the arrows line up and read off λ.\lambda\mathclose{\text{.}} The “nearly parallel” readout means the arrows are within 0.5∘0.5^\circ of parallel; it gives an approximate multiplier and suggests a nearby candidate to locate exactly with algebra. Elsewhere the readout says which way ff changes as you move on: follow it to the maximum, then to the minimum. Then try x2+2y2x^2 + 2y^2 on the circle (Exercise 18.3), and 2x+y2x + y on the circle, whose maximum is not at a whole number of degrees.

Example 18.5The largest rectangle in an ellipse✓ Checked

Find the largest rectangle, with sides parallel to the axes, inside the ellipse x24+y2=1.\tfrac{x^2}{4} + y^2 = 1\mathclose{\text{.}}

Maximise A=4xyA = 4xy subject to g(x,y)=x24+y2=1,g(x, y) = \tfrac{x^2}{4} + y^2 = 1\mathclose{\text{,}} where (x,y)(x, y) is the corner with x≥0x \ge 0 and y≥0.y \ge 0\mathclose{\text{.}} A maximum exists. The quarter of the ellipse in the first quadrant is closed and bounded, and AA is continuous, so AA has a largest value on it (Theorem 18.3). At the ends of the arc, (2,0)(2, 0) and (0,1),(0, 1)\mathclose{\text{,}} the rectangle is flat and A=0,A = 0\mathclose{\text{,}} so the largest value is at a point with x>0x > 0 and y>0,y > 0\mathclose{\text{,}} where Theorem 18.5 applies: ∇g=⟨x2,2y⟩\nabla g = \big\langle \tfrac{x}{2}, 2y\big\rangle is never 0\vb{0} on the ellipse.

The equations. ∇A=⟨4y,4x⟩,\nabla A = \langle 4y, 4x\rangle\mathclose{\text{,}} so

4y=λx2,4x=2λy,x24+y2=1.4y = \lambda\frac{x}{2}, \quad 4x = 2\lambda y, \quad \frac{x^2}{4} + y^2 = 1.

With x,y>0,x, y > 0\mathclose{\text{,}} the first two give λ=8yx\lambda = \dfrac{8y}{x} and λ=2xy.\lambda = \dfrac{2x}{y}\mathclose{\text{.}} So 8yx=2xy,\dfrac{8y}{x} = \dfrac{2x}{y}\mathclose{\text{,}} that is, 8y2=2x2,8y^2 = 2x^2\mathclose{\text{,}} and x=2y,x = 2y\mathclose{\text{,}} since both are positive. The constraint becomes 4y24+y2=2y2=1,\tfrac{4y^2}{4} + y^2 = 2y^2 = 1\mathclose{\text{,}} so y=12=22,y = \tfrac{1}{\sqrt2} = \tfrac{\sqrt2}{2}\mathclose{\text{,}} x=2x = \sqrt2 and λ=2xy=4.\lambda = \tfrac{2x}{y} = 4\mathclose{\text{.}}

The answer. The largest rectangle is 222\sqrt2 by 2,\sqrt2\mathclose{\text{,}} with area 4⋅2⋅22=4,4 \cdot \sqrt2 \cdot \tfrac{\sqrt2}{2} = 4\mathclose{\text{,}} as the row t=π4t = \tfrac{\pi}{4} of the table above suggested. The corner lies on the line y=x2,y = \tfrac{x}{2}\mathclose{\text{,}} the diagonal of the 4 by 2 rectangle around the ellipse, and the best rectangle has exactly half its area.

Example 18.6The closest point on a parabola, again✓ Checked

Find the point of the parabola y=x2y = x^2 closest to (3,0),(3, 0)\mathclose{\text{,}} the problem of Example 8.15, with Lagrange multipliers.

Minimise the square of the distance, D(x,y)=(x−3)2+y2,D(x, y) = (x - 3)^2 + y^2\mathclose{\text{,}} subject to g(x,y)=y−x2=0.g(x, y) = y - x^2 = 0\mathclose{\text{.}} Here ∇D=⟨2(x−3),2y⟩\nabla D = \langle 2(x - 3), 2y\rangle and ∇g=⟨−2x,1⟩,\nabla g = \langle -2x, 1\rangle\mathclose{\text{,}} which is never 0.\vb{0}\mathclose{\text{.}} The equations are

2(x−3)=−2λx,2y=λ,y=x2.2(x - 3) = -2\lambda x, \quad 2y = \lambda, \quad y = x^2.

The last two give λ=2y=2x2,\lambda = 2y = 2x^2\mathclose{\text{,}} and then the first becomes 2(x−3)=−4x3,2(x - 3) = -4x^3\mathclose{\text{,}} that is, 4x3+2x−6=2(2x3+x−3)=0.4x^3 + 2x - 6 = 2(2x^3 + x - 3) = 0\mathclose{\text{.}} As in Example 8.15, 2x3+x−3=(x−1)(2x2+2x+3),2x^3 + x - 3 = (x - 1)(2x^2 + 2x + 3)\mathclose{\text{,}} and the quadratic factor has no real zero, so x=1,x = 1\mathclose{\text{,}} y=1y = 1 and λ=2:\lambda = 2\mathclose{\text{:}} one candidate, (1,1),(1, 1)\mathclose{\text{,}} at distance D(1,1)=4+1=5≈2.236.\sqrt{D(1, 1)} = \sqrt{4 + 1} = \sqrt5 \approx 2.236\mathclose{\text{.}}

It is the minimum. The parabola is not bounded, so Theorem 18.3 needs help. Where ∣x∣≥3,\abs{x} \ge 3\mathclose{\text{,}} D≥y2=x4≥81,D \ge y^2 = x^4 \ge 81\mathclose{\text{,}} far more than D(1,1)=5.D(1, 1) = 5\mathclose{\text{.}} On the arc with ∣x∣≤3,\abs{x} \le 3\mathclose{\text{,}} which is closed and bounded, DD has a smallest value, at most 5, so not at the ends of the arc, where D≥81.D \ge 81\mathclose{\text{.}} That point is a local minimum of DD on the parabola, so it solves the equations: it is (1,1),(1, 1)\mathclose{\text{,}} and D(1,1)=5D(1, 1) = 5 is the smallest value on the whole parabola.

The picture. ∇D=2⟨x−3,y⟩\nabla D = 2\langle x - 3, y\rangle is twice the vector from (3,0)(3, 0) to the point (x,y),(x, y)\mathclose{\text{,}} and ∇g\nabla g is perpendicular to the parabola. So ∇D=λ∇g\nabla D = \lambda\nabla g says that the segment from (3,0)(3, 0) to the closest point is perpendicular to the curve: the check at the end of Example 8.15, which the method now explains.

Example 18.7A box from a sheet of cardboard✓ Checked

An open box (with no lid) is to be made from 300 cm² of cardboard. Ignoring joins and waste, which dimensions give the largest volume?

Let the base be xx by yy and the height z,z\mathclose{\text{,}} in centimetres. Maximise V=xyzV = xyz subject to g(x,y,z)=xy+2xz+2yz=300g(x, y, z) = xy + 2xz + 2yz = 300 (the base and the four sides), with x,y,z>0.x, y, z > 0\mathclose{\text{.}} Here ∇V=⟨yz,xz,xy⟩\nabla V = \langle yz, xz, xy\rangle and ∇g=⟨y+2z, x+2z, 2x+2y⟩,\nabla g = \langle y + 2z,\ x + 2z,\ 2x + 2y\rangle\mathclose{\text{,}} which is nonzero for positive dimensions, so

yz=λ(y+2z),xz=λ(x+2z),xy=λ(2x+2y).\begin{aligned} yz &= \lambda(y + 2z), \\ xz &= \lambda(x + 2z), \\ xy &= \lambda(2x + 2y). \end{aligned}

Multiply the first equation by x,x\mathclose{\text{,}} the second by yy and the third by z:z\mathclose{\text{:}} all three left sides become xyz,xyz\mathclose{\text{,}} so

λ(xy+2xz)=λ(xy+2yz)=λ(2xz+2yz).\begin{aligned} \lambda(xy + 2xz) &= \lambda(xy + 2yz) \\ &= \lambda(2xz + 2yz). \end{aligned}

λ≠0,\lambda \ne 0\mathclose{\text{,}} since xyz>0;xyz > 0\mathclose{\text{;}} divide by it. The first equality gives xz=yz,xz = yz\mathclose{\text{,}} so x=y;x = y\mathclose{\text{;}} the second gives xy=2xz,xy = 2xz\mathclose{\text{,}} so y=2z.y = 2z\mathclose{\text{.}} So x=y=2z,x = y = 2z\mathclose{\text{,}} and the constraint becomes 4z2+4z2+4z2=12z2=300:4z^2 + 4z^2 + 4z^2 = 12z^2 = 300\mathclose{\text{:}} z=5,z = 5\mathclose{\text{,}} x=y=10,x = y = 10\mathclose{\text{,}} and λ=yzy+2z=5020=52.\lambda = \dfrac{yz}{y + 2z} = \dfrac{50}{20} = \dfrac52\mathclose{\text{.}} The volume is 10⋅10⋅5=50010 \cdot 10 \cdot 5 = 500 cm³.

It is the maximum. The surface g=300g = 300 is not bounded, and Theorem 18.5 says only that a largest box, if there is one, has these dimensions. An inequality settles it: for positive numbers p,p\mathclose{\text{,}} qq and r,r\mathclose{\text{,}}

p+q+r3≥pqr3,with equality only when p=q=r,\frac{p + q + r}{3} \ge \sqrt[3]{pqr}, \quad\text{with equality only when } p = q = r,

the inequality of the arithmetic and geometric means, which Exercise 18.11 proves. With p=xy,p = xy\mathclose{\text{,}} q=2xzq = 2xz and r=2yz,r = 2yz\mathclose{\text{,}} the sum is p+q+r=300p + q + r = 300 and the product is pqr=4(xyz)2=4V2.pqr = 4(xyz)^2 = 4V^2\mathclose{\text{.}} So 100≥4V23,100 \ge \sqrt[3]{4V^2}\mathclose{\text{,}} that is, 4V2≤1003,4V^2 \le 100^3\mathclose{\text{,}} and V≤500,V \le 500\mathclose{\text{,}} with equality only when xy=2xz=2yz,xy = 2xz = 2yz\mathclose{\text{,}} which is the box found. The largest box is 10 cm by 10 cm, and 5 cm deep.

Watch out

Three slips with Lagrange multipliers. Dividing by a variable that can be 0: in 2x=2λx,2x = 2\lambda x\mathclose{\text{,}} cancelling xx to get λ=1\lambda = 1 loses every candidate with x=0x = 0 (Exercise 18.3); write x(1−λ)=0x(1 - \lambda) = 0 instead. Forgetting the points where ∇g=0\nabla g = \vb{0}: on the curve y2=x3,y^2 = x^3\mathclose{\text{,}} the function f=xf = x is smallest at the origin, since x=y23≥0x = \sqrt[3]{y^2} \ge 0 on the curve; but there ∇g=⟨−3x2,2y⟩\nabla g = \langle -3x^2, 2y\rangle is 0,\vb{0}\mathclose{\text{,}} and the equations 1=−3λx2,1 = -3\lambda x^2\mathclose{\text{,}} 0=2λy,0 = 2\lambda y\mathclose{\text{,}} y2=x3y^2 = x^3 have no solution at all. Taking a candidate for an extremum without an argument: on the branch of the hyperbola xy=1xy = 1 with x>0,x > 0\mathclose{\text{,}} f=x+yf = x + y has the single candidate (1,1),(1, 1)\mathclose{\text{,}} where f=2.f = 2\mathclose{\text{.}} It is the minimum, since x+1x−2=(x−1)2x≥0,x + \tfrac1x - 2 = \tfrac{(x - 1)^2}{x} \ge 0\mathclose{\text{,}} but there is no maximum: f→∞f \to \infty as x→∞.x \to \infty\mathclose{\text{.}}

Going deeperWhat the multiplier measures

In the examples, λ\lambda was found and then forgotten. It has a meaning. Let M(c)M(c) be the largest value of ff on the curve g=c,g = c\mathclose{\text{,}} taken at a point (x(c),y(c))\big(x(c), y(c)\big) with multiplier λ(c).\lambda(c)\mathclose{\text{.}} Suppose that this family is defined for cc in an open interval, that x(c)x(c) and y(c)y(c) are differentiable there, and that ff and gg have continuous first partial derivatives near these points. Then

M′(c)=λ(c).M'(c) = \lambda(c).

Indeed M(c)=f(x(c),y(c)),M(c) = f\big(x(c), y(c)\big)\mathclose{\text{,}} and by the chain rule and the Lagrange equations

M′(c)=fxx′+fyy′=λ (gxx′+gyy′)=λ ddc g(x(c),y(c))=λ ddc c=λ.\begin{aligned} M'(c) &= f_x x' + f_y y' = \lambda\,(g_x x' + g_y y') \\ &= \lambda\,\frac{d}{dc}\,g\big(x(c), y(c)\big) = \lambda\,\frac{d}{dc}\,c = \lambda. \end{aligned}

The same argument works for the smallest value. So λ\lambda is the rate at which the best value changes as the constraint level changes: if cc is a budget, λ\lambda is about what one more unit of budget is worth at the optimum, provided that unit is small enough for the linear approximation. Economists call it a shadow price.

For the rectangle in the ellipse x24+y2=c\tfrac{x^2}{4} + y^2 = c with c>0,c > 0\mathclose{\text{,}} the steps of Example 18.5 give the corner (2c,c/2)\big(\sqrt{2c}, \sqrt{c/2}\big) and the largest area M(c)=4c,M(c) = 4c\mathclose{\text{,}} so M′(c)=4:M'(c) = 4\mathclose{\text{:}} the λ=4\lambda = 4 found there. For the box with AA cm² of cardboard, where A>0,A > 0\mathclose{\text{,}} they give x=y=2zx = y = 2z with 12z2=A,12z^2 = A\mathclose{\text{,}} so the largest volume is V(A)=4z3=4(A12)3/2,V(A) = 4z^3 = 4\big(\tfrac{A}{12}\big)^{3/2}\mathclose{\text{,}} and V′(A)=12(A12)1/2=z2,V'(A) = \tfrac12\big(\tfrac{A}{12}\big)^{1/2} = \tfrac{z}{2}\mathclose{\text{,}} which at A=300A = 300 is 52:\tfrac52\mathclose{\text{:}} the multiplier of Example 18.7. Each extra cm² of cardboard adds about 2.5 cm³ of volume, near this size.

Two constraints

Suppose that f,f\mathclose{\text{,}} gg and hh have continuous first partial derivatives near a point where g(x,y,z)=cg(x, y, z) = c and h(x,y,z)=d.h(x, y, z) = d\mathclose{\text{.}} To find extreme values of ff on their intersection, add a second multiplier μ,\mu\mathclose{\text{,}} read “mu”: at a constrained local extremum,

∇f=λ ∇g+μ ∇h,\nabla f = \lambda\,\nabla g + \mu\,\nabla h,

provided ∇g\nabla g and ∇h\nabla h are not parallel there (and neither is 0).\vb{0}\mathclose{\text{).}} These hypotheses ensure that the intersection is locally a differentiable curve with a nonzero tangent vector, by a version of the implicit function theorem of Module 17. The reason for the multiplier equation is the same as before. The curve lies in both surfaces, so its tangent vector T\vb{T} is perpendicular to ∇g\nabla g and to ∇h;\nabla h\mathclose{\text{;}} and at an extremum ∇f\nabla f is perpendicular to T\vb{T} too. The vectors perpendicular to T\vb{T} form a plane, and two non-parallel vectors in it, ∇g\nabla g and ∇h,\nabla h\mathclose{\text{,}} combine to give every vector of the plane, ∇f\nabla f included. With the two constraints, that makes five equations for x,x\mathclose{\text{,}} y,y\mathclose{\text{,}} z,z\mathclose{\text{,}} λ\lambda and μ.\mu\mathclose{\text{.}}

Example 18.8The highest point of a tilted circle✓ Checked

The plane x+y+z=0x + y + z = 0 meets the sphere x2+y2+z2=6x^2 + y^2 + z^2 = 6 in a circle. Find its highest and lowest points.

Maximise and minimise f=zf = z subject to g=x+y+z=0g = x + y + z = 0 and h=x2+y2+z2=6.h = x^2 + y^2 + z^2 = 6\mathclose{\text{.}} The equations ⟨0,0,1⟩=λ⟨1,1,1⟩+μ⟨2x,2y,2z⟩\langle 0, 0, 1\rangle = \lambda\langle 1, 1, 1\rangle + \mu\langle 2x, 2y, 2z\rangle are

0=λ+2μx,0=λ+2μy,1=λ+2μz,\begin{aligned} 0 &= \lambda + 2\mu x, \\ 0 &= \lambda + 2\mu y, \\ 1 &= \lambda + 2\mu z, \end{aligned}

with the two constraints. Subtracting the first two gives 2μ(x−y)=0.2\mu(x - y) = 0\mathclose{\text{.}} If μ=0,\mu = 0\mathclose{\text{,}} the first gives λ=0,\lambda = 0\mathclose{\text{,}} and then the third says 1=0,1 = 0\mathclose{\text{,}} which is impossible; so x=y.x = y\mathclose{\text{.}} The plane then gives z=−2x,z = -2x\mathclose{\text{,}} and the sphere x2+x2+4x2=6,x^2 + x^2 + 4x^2 = 6\mathclose{\text{,}} so x=±1.x = \pm1\mathclose{\text{.}} The candidates are (1,1,−2)(1, 1, -2) and (−1,−1,2),(-1, -1, 2)\mathclose{\text{,}} with λ=13\lambda = \tfrac13 and μ=∓16.\mu = \mp\tfrac16\mathclose{\text{.}} The gradients ⟨1,1,1⟩\langle 1, 1, 1\rangle and ⟨2x,2y,2z⟩\langle 2x, 2y, 2z\rangle are parallel only where x=y=z,x = y = z\mathclose{\text{,}} which on the plane means the origin, not on the sphere; so the method applies everywhere on the circle. The circle is closed and bounded, so ff has a largest and a smallest value on it (the Extreme Value Theorem in three variables), and they are among the candidates: the highest point is (−1,−1,2)(-1, -1, 2) and the lowest is (1,1,−2).(1, 1, -2)\mathclose{\text{.}}

Compute itLagrange systems with SymPy

solve handles the systems of this section. Declaring the dimensions positive keeps only the boxes that can be built; the multiplier is left real:

Python
import sympy as sp
x, y, z = sp.symbols('x y z', positive=True)
lam = sp.symbols('lambda', real=True)
V = x*y*z                                   # the open box of Example 18.7
g = x*y + 2*x*z + 2*y*z
eqs = [sp.diff(V, s) - lam*sp.diff(g, s) for s in (x, y, z)] + [g - 300]
print(sp.solve(eqs, [x, y, z, lam], dict=True))
Output
[{lambda: 5/2, x: 10, y: 10, z: 5}]

With two constraints there are two multipliers; here the unknowns are real, of either sign:

Python
x, y, z, lam, mu = sp.symbols('x y z lambda mu', real=True)
f, g, h = z, x + y + z, x**2 + y**2 + z**2   # Example 18.8
eqs = [sp.diff(f, s) - lam*sp.diff(g, s) - mu*sp.diff(h, s) for s in (x, y, z)]
eqs += [g, h - 6]
for sol in sp.solve(eqs, [x, y, z, lam, mu], dict=True):
    print(sol, ' f =', f.subs(sol))
Output
{lambda: 1/3, mu: -1/6, x: 1, y: 1, z: -2}  f = -2
{lambda: 1/3, mu: 1/6, x: -1, y: -1, z: 2}  f = 2

solve finds the candidates; deciding which is the maximum, and whether a maximum exists, is still your job.

5

Least squares

Data from an experiment or a log file often lie roughly along a line, and you want the line that fits them best. Suppose, hypothetically, that five measurements give the points (0,1),(0, 1)\mathclose{\text{,}} (1,4),(1, 4)\mathclose{\text{,}} (2,3),(2, 3)\mathclose{\text{,}} (3,6)(3, 6) and (4,6)(4, 6) (made-up numbers). No line passes through all five. For a line y=mx+b,y = mx + b\mathclose{\text{,}} the residual, or error, of the point (xi,yi)(x_i, y_i) is

ei=yi−(mxi+b),e_i = y_i - (mx_i + b),

the vertical distance, with a sign, from the line up to the point. One number measures how badly the line misses all the points at once: the sum of squared errors

E(m,b)=∑i=1n(yi−mxi−b)2.E(m, b) = \sum_{i=1}^{n}\big(y_i - mx_i - b\big)^2.

Here the data are fixed numbers, and the unknowns are the slope mm and the intercept b:b\mathclose{\text{:}} EE is a function of two variables.

Definition 18.6Least-squares line

A least-squares line, or regression line, of a nonempty finite collection of data points (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) is a line y=mx+by = mx + b whose slope and intercept minimise the sum of squared errors E(m,b).E(m, b)\mathclose{\text{.}} The next theorem gives the condition for this line to be unique.

Squares, rather than absolute values of the errors, make EE a smooth function of mm and b,b\mathclose{\text{,}} so that calculus applies; a large error can outweigh several smaller ones, which matters when the data contain a wild value. Mathematical Foundations’ least squares module treats the same problem with vectors and projections; here partial derivatives find the answer.

Theorem 18.6The normal equations

Let (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) be data points with at least two different xi.x_i\mathclose{\text{.}} Then E(m,b)E(m, b) has exactly one critical point, the solution of the normal equations

(∑xi2)m+(∑xi)b=∑xiyi,(∑xi)m+nb=∑yi,\begin{aligned} \Big(\sum x_i^2\Big)m + \Big(\sum x_i\Big)b &= \sum x_iy_i, \\ \Big(\sum x_i\Big)m + nb &= \sum y_i, \end{aligned}

and EE takes its absolute minimum value there, and nowhere else. (Each sum runs over i=1,…,n.)i = 1, \dots, n\mathclose{\text{.)}}

Proof

The critical point. EE is a polynomial in mm and b.b\mathclose{\text{.}} By the chain rule,

∂E∂m=−2∑xi(yi−mxi−b),∂E∂b=−2∑(yi−mxi−b).\begin{aligned} \frac{\partial E}{\partial m} &= -2\sum x_i\big(y_i - mx_i - b\big), \\ \frac{\partial E}{\partial b} &= -2\sum\big(y_i - mx_i - b\big). \end{aligned}

Setting both to 0 and moving the terms with mm and bb to the left gives the normal equations. To solve them, let xˉ=1n∑xi\bar x = \tfrac1n\sum x_i and yˉ=1n∑yi\bar y = \tfrac1n\sum y_i be the means, read “x bar” and “y bar”. The second equation divided by nn says b=yˉ−mxˉ.b = \bar y - m\bar x\mathclose{\text{.}} Substituting this into the first gives

(∑xi2−nxˉ2)m=∑xiyi−nxˉyˉ.\Big(\sum x_i^2 - n\bar x^2\Big)m = \sum x_iy_i - n\bar x\bar y.

Expanding the square shows that ∑(xi−xˉ)2=∑xi2−2xˉ∑xi+nxˉ2=∑xi2−nxˉ2,\sum (x_i - \bar x)^2 = \sum x_i^2 - 2\bar x\sum x_i + n\bar x^2 = \sum x_i^2 - n\bar x^2\mathclose{\text{,}} and this is positive, because not every xix_i equals xˉ.\bar x\mathclose{\text{.}} So mm is determined, then b:b\mathclose{\text{:}} exactly one solution (m∗,b∗).(m^*, b^*)\mathclose{\text{.}}

The minimum. Let ei=yi−m∗xi−b∗e_i = y_i - m^*x_i - b^* be the residuals of this line. The normal equations say exactly that ∑ei=0\sum e_i = 0 and ∑xiei=0.\sum x_ie_i = 0\mathclose{\text{.}} For any other line, with m=m∗+pm = m^* + p and b=b∗+q,b = b^* + q\mathclose{\text{,}}

E(m∗+p,b∗+q)=∑(ei−pxi−q)2=∑ei2−2p∑xiei−2q∑ei+∑(pxi+q)2=E(m∗,b∗)+∑(pxi+q)2.\begin{aligned} &E(m^* + p, b^* + q) \\ &\quad= \sum\big(e_i - px_i - q\big)^2 \\ &\quad= \sum e_i^2 - 2p\sum x_ie_i - 2q\sum e_i \\ &\qquad + \sum\big(px_i + q\big)^2 \\ &\quad= E(m^*, b^*) + \sum\big(px_i + q\big)^2. \end{aligned}

The last sum is never negative, and it is 0 only when pxi+q=0px_i + q = 0 for every i;i\mathclose{\text{;}} for two different xix_i that forces p=q=0.p = q = 0\mathclose{\text{.}}

∎

So the least-squares line passes through the mean point (xˉ,yˉ),(\bar x, \bar y)\mathclose{\text{,}} and its slope is

m=∑xiyi−nxˉyˉ∑xi2−nxˉ2.m = \frac{\sum x_iy_i - n\bar x\bar y}{\sum x_i^2 - n\bar x^2}.
Example 18.9Fitting a line to five points✓ Checked

Find the least-squares line of the points (0,1),(0, 1)\mathclose{\text{,}} (1,4),(1, 4)\mathclose{\text{,}} (2,3),(2, 3)\mathclose{\text{,}} (3,6)(3, 6) and (4,6),(4, 6)\mathclose{\text{,}} its residuals and E.E\mathclose{\text{.}}

The sums: n=5,n = 5\mathclose{\text{,}} ∑xi=10,\sum x_i = 10\mathclose{\text{,}} ∑xi2=0+1+4+9+16=30,\sum x_i^2 = 0 + 1 + 4 + 9 + 16 = 30\mathclose{\text{,}} ∑yi=20\sum y_i = 20 and ∑xiyi=0+4+6+18+24=52.\sum x_iy_i = 0 + 4 + 6 + 18 + 24 = 52\mathclose{\text{.}} The normal equations are

30m+10b=52,10m+5b=20.30m + 10b = 52, \qquad 10m + 5b = 20.

Subtract twice the second from the first: 10m=12,10m = 12\mathclose{\text{,}} so m=1.2,m = 1.2\mathclose{\text{,}} and then 5b=20−125b = 20 - 12 gives b=1.6.b = 1.6\mathclose{\text{.}} The least-squares line is

y=1.2x+1.6.y = 1.2x + 1.6.

It passes through the mean point (xˉ,yˉ)=(2,4).(\bar x, \bar y) = (2, 4)\mathclose{\text{.}} At x=0,1,2,3,4x = 0, 1, 2, 3, 4 the line gives 1.6,1.6\mathclose{\text{,}} 2.8,2.8\mathclose{\text{,}} 4.0,4.0\mathclose{\text{,}} 5.25.2 and 6.4,6.4\mathclose{\text{,}} so the residuals are −0.6,-0.6\mathclose{\text{,}} 1.2,1.2\mathclose{\text{,}} −1.0,-1.0\mathclose{\text{,}} 0.80.8 and −0.4.-0.4\mathclose{\text{.}} They add up to 0, and so do their products with the xi,x_i\mathclose{\text{,}} 0+1.2−2.0+2.4−1.6=0,0 + 1.2 - 2.0 + 2.4 - 1.6 = 0\mathclose{\text{,}} as the normal equations promise. Finally

E(1.2,1.6)=0.36+1.44+1.00+0.64+0.16=3.6,E(1.2, 1.6) = 0.36 + 1.44 + 1.00 + 0.64 + 0.16 = 3.6,

and every other line has a larger sum of squared errors. Figure 18.4 shows the line and its residuals.

The least-squares line y = 1.2x + 1.6 of Example 18.9 and its residuals (orange): the vertical errors -0.6, 1.2, -1.0, 0.8 and -0.4, whose squares add up to 3.6, less than for any other line.
Figure 18.4

The least-squares line y=1.2x+1.6y = 1.2x + 1.6 of Example 18.9 and its residuals (orange): the vertical errors −0.6,-0.6\mathclose{\text{,}} 1.2,1.2\mathclose{\text{,}} −1.0,-1.0\mathclose{\text{,}} 0.80.8 and −0.4,-0.4\mathclose{\text{,}} whose squares add up to 3.6,3.6\mathclose{\text{,}} less than for any other line.

Compute itLeast squares with NumPy

The normal equations are two linear equations for mm and b,b\mathclose{\text{,}} and np.linalg.solve solves them. NumPy also has a ready-made fit, np.polyfit:

Python
import numpy as np
xs = np.array([0, 1, 2, 3, 4])
ys = np.array([1, 4, 3, 6, 6])
n = len(xs)
A = np.array([[np.sum(xs**2), np.sum(xs)],
              [np.sum(xs),    n]])
rhs = np.array([np.sum(xs*ys), np.sum(ys)])
m, b = np.linalg.solve(A, rhs)               # the normal equations
print(f"normal equations: m = {m:.4f}, b = {b:.4f}")
print("np.polyfit:", np.polyfit(xs, ys, 1))
e = ys - (m*xs + b)
print("residuals:", np.round(e, 4), " E =", round(float(np.sum(e**2)), 4))
Output
normal equations: m = 1.2000, b = 1.6000
np.polyfit: [1.2 1.6]
residuals: [-0.6  1.2 -1.   0.8 -0.4]  E = 3.6

np.polyfit(xs, ys, 1) fits a polynomial of degree 1 and returns its coefficients highest power first, [m,b],[m, b]\mathclose{\text{,}} here as floating-point numbers. With degree 2 it fits y=ax2+bx+cy = ax^2 + bx + c by least squares; the minimiser satisfies three normal equations and is unique when there are at least three distinct input values. The fitted coefficient aa can be 0, so the result need not be a parabola. The numerical routine solves a scaled least-squares system rather than forming these normal equations. NumPy recommends numpy.polynomial.Polynomial.fit for new code because it is more stable numerically.

Watch out

Three slips with least squares. Expecting the line to treat xx and yy alike: it minimises vertical errors. Fitting xx as a function of yy to the data of Example 18.9 gives x=23y−23,x = \tfrac23y - \tfrac23\mathclose{\text{,}} that is, y=1.5x+1,y = 1.5x + 1\mathclose{\text{,}} a different line (both pass through the mean point). Forgetting the condition: if every xix_i equals the same number a,a\mathclose{\text{,}} only the fitted value ma+bma + b is determined. It must equal yˉ,\bar y\mathclose{\text{,}} so infinitely many pairs with b=yˉ−mab = \bar y - ma minimise E.E\mathclose{\text{.}} Trusting a fit blindly: a least-squares fit exists for every nonempty finite data set, even data that follow a curve. A pattern in the residuals, such as positive at both ends and negative in the middle, suggests that a line is a poor model; it is evidence, not proof, because measurements can contain noise.

6

Gradient descent: step size and conditioning

Solving ∇f=0\nabla f = \vb{0} by algebra works only for simple functions. A machine-learning model may have millions of parameters, and training often seeks smaller loss by gradient descent, met in one variable in Module 8 and in two in Module 17. For a differentiable function f,f\mathclose{\text{,}} from a starting point x0,\vb{x}_0\mathclose{\text{,}} with a step size η>0\eta > 0 (“eta”),

xk+1=xk−η ∇f(xk),k=0,1,2,…,\vb{x}_{k+1} = \vb{x}_k - \eta\,\nabla f(\vb{x}_k), \qquad k = 0, 1, 2, \dots,

where xk=⟨xk,yk⟩,\vb{x}_k = \langle x_k, y_k\rangle\mathclose{\text{,}} and each new point must stay in the domain. A nonzero gradient gives the direction of fastest decrease through −∇f,-\nabla f\mathclose{\text{,}} but a finite step can still increase ff if it is too large. Example 8.18 showed that on the parabola q2x2,\tfrac q2x^2\mathclose{\text{,}} with q>0,q > 0\mathclose{\text{,}} convergence to the minimum from every starting point holds exactly when 0<η<2/q.0 < \eta < 2/q\mathclose{\text{.}} In two variables a bowl can curve more steeply in one direction than in another, and that affects the choice of step size.

Example 18.10Gradient descent in a long valley✓ Checked

Run gradient descent on f(x,y)=12(x2+10y2)f(x, y) = \tfrac12(x^2 + 10y^2) from (−6,2.5)(-6, 2.5) with step size η=0.18.\eta = 0.18\mathclose{\text{.}}

∇f=⟨x,10y⟩,\nabla f = \langle x, 10y\rangle\mathclose{\text{,}} so one step is

xk+1=xk−0.18xk=0.82 xk,yk+1=yk−1.8 yk=−0.8 yk.\begin{aligned} x_{k+1} &= x_k - 0.18x_k = 0.82\,x_k, \\ y_{k+1} &= y_k - 1.8\,y_k = -0.8\,y_k. \end{aligned}

The two coordinates evolve separately: xk=−6(0.82)kx_k = -6(0.82)^k and yk=2.5(−0.8)k.y_k = 2.5(-0.8)^k\mathclose{\text{.}} The table shows the points and the values of ff there:

kkxkx_kyky_kff
00−6-62.52.549.2549.25
11−4.92-4.92−2-232.1032.10
22−4.034-4.0341.61.620.9420.94
33−3.308-3.308−1.28-1.2813.6613.66
44−2.713-2.7131.0241.0248.9228.922
55−2.224-2.224−0.8192-0.81925.8305.830
1010−0.8247-0.82470.26840.26840.70030.7003
2020−0.1134-0.11340.028820.028820.010580.01058

The xx-coordinate creeps towards 0, losing 18% of its magnitude each step. The yy-coordinate overshoots every time: it changes sign at each step, and its magnitude shrinks by only 20%. The path zigzags across the valley while it drifts slowly along it (Figure 18.5, left); after 20 steps ff has fallen from 49.25 to about 0.0106.

Left: gradient descent on f(x, y) = tfrac12(x^2 + 10y^2) from (-6, 2.5) with eta = 0.18, over the level curves of f. Each step is perpendicular to the level curve it leaves, so on these long ellipses it points mostly across the valley: the path zigzags. Right: the same function in the coordinates x and v = sqrt{10},y, where it is tfrac12(x^2 + v^2) and its level curves are circles. Now every step points straight at the minimum; with eta = 0.5 each step halves the distance, and eta = 1 would reach the minimum in one step.
Figure 18.5

Left: gradient descent on f(x,y)=12(x2+10y2)f(x, y) = \tfrac12(x^2 + 10y^2) from (−6,2.5)(-6, 2.5) with η=0.18,\eta = 0.18\mathclose{\text{,}} over the level curves of f.f\mathclose{\text{.}} Each step is perpendicular to the level curve it leaves, so on these long ellipses it points mostly across the valley: the path zigzags. Right: the same function in the coordinates xx and v=10 y,v = \sqrt{10}\,y\mathclose{\text{,}} where it is 12(x2+v2)\tfrac12(x^2 + v^2) and its level curves are circles. Now every step points straight at the minimum; with η=0.5\eta = 0.5 each step halves the distance, and η=1\eta = 1 would reach the minimum in one step.

Theorem 18.7Gradient descent on a quadratic bowl

Let f(x,y)=12(ax2+cy2)f(x, y) = \tfrac12(ax^2 + cy^2) with 0<a≤c.0 < a \le c\mathclose{\text{.}} Gradient descent with step size η>0\eta > 0 converges to the minimum (0,0)(0, 0) from every starting point exactly when η<2c.\eta < \dfrac{2}{c}\mathclose{\text{.}}

Proof

∇f=⟨ax,cy⟩,\nabla f = \langle ax, cy\rangle\mathclose{\text{,}} so xk+1=(1−ηa)xkx_{k+1} = (1 - \eta a)x_k and yk+1=(1−ηc)yk,y_{k+1} = (1 - \eta c)y_k\mathclose{\text{,}} and therefore xk=(1−ηa)kx0x_k = (1 - \eta a)^kx_0 and yk=(1−ηc)ky0.y_k = (1 - \eta c)^ky_0\mathclose{\text{.}} As in Example 8.18, the powers rkr^k of a number rr tend to 0 exactly when ∣r∣<1.\abs{r} < 1\mathclose{\text{.}} To converge from every start, including starts with x0≠0x_0 \ne 0 and y0≠0,y_0 \ne 0\mathclose{\text{,}} gradient descent needs ∣1−ηa∣<1\abs{1 - \eta a} < 1 and ∣1−ηc∣<1,\abs{1 - \eta c} < 1\mathclose{\text{,}} that is, 0<η<2/a0 < \eta < 2/a and 0<η<2/c.0 < \eta < 2/c\mathclose{\text{.}} Since a≤c,a \le c\mathclose{\text{,}} 2/c≤2/a,2/c \le 2/a\mathclose{\text{,}} and the two conditions together say 0<η<2/c.0 < \eta < 2/c\mathclose{\text{.}}

∎

Ill-conditioning

Theorem 18.7 says that convergence from every starting point requires η<2/c,\eta < 2/c\mathclose{\text{,}} where cc is the larger curvature. In the flatter direction, with curvature a,a\mathclose{\text{,}} the factor 1−ηa1 - \eta a is then more than 1−2a/c,1 - 2a/c\mathclose{\text{,}} which is close to 1 when cc is much larger than a:a\mathclose{\text{:}} a nonzero coordinate in that direction creeps towards 0. To minimise the larger of the two coordinate factors in magnitude, the best fixed step is η∗=2a+c,\eta^* = \dfrac{2}{a + c}\mathclose{\text{,}} which makes the two factors equal in size:

1−η∗a=c−ac+a,1−η∗c=−c−ac+a.1 - \eta^*a = \frac{c - a}{c + a}, \qquad 1 - \eta^*c = -\frac{c - a}{c + a}.

Exercise 18.10 shows that no other fixed step size makes that larger factor smaller. With κ=c/a,\kappa = c/a\mathclose{\text{,}} read “kappa”, the condition number of the bowl, the factor is

c−ac+a=κ−1κ+1.\frac{c - a}{c + a} = \frac{\kappa - 1}{\kappa + 1}.

For the valley of Example 18.10, κ=10:\kappa = 10\mathclose{\text{:}} this best fixed step is η∗=211≈0.18,\eta^* = \tfrac{2}{11} \approx 0.18\mathclose{\text{,}} and with it each step shrinks both coordinates, in size, by the factor 911≈0.82.\tfrac{9}{11} \approx 0.82\mathclose{\text{.}} When κ\kappa is large the factor is close to 1, and gradient descent is slow: to reduce the distance from the minimum by a fixed factor, the number of steps with η∗\eta^* grows asymptotically in proportion to κ\kappa (the Compute it box below counts them, and Exercise 18.10 explains why).

The picture explains it. For C>0,C > 0\mathclose{\text{,}} the level curves 12(ax2+cy2)=C\tfrac12(ax^2 + cy^2) = C are ellipses with semi-axes 2C/a\sqrt{2C/a} and 2C/c,\sqrt{2C/c}\mathclose{\text{,}} so they are κ\sqrt\kappa times as long as they are wide. The gradient is perpendicular to the level curve through the point (Module 17), and often points across the valley rather than directly towards the minimum. A quadratic bowl with a large condition number, and hence long, thin elliptical level curves, is ill-conditioned. Gradient descent can zigzag on it, as in Figure 18.5, though smaller steps or a start on an axis need not zigzag.

Rescaling

The shape of the level curves depends on the variables you choose. Put v=10 yv = \sqrt{10}\,y in Example 18.10: then f=12(x2+v2),f = \tfrac12(x^2 + v^2)\mathclose{\text{,}} whose level curves in the (x,v)(x, v) plane are circles, with κ=1.\kappa = 1\mathclose{\text{.}} Its gradient is ⟨x,v⟩,\langle x, v\rangle\mathclose{\text{,}} which points straight away from the minimum, and gradient descent in xx and vv with η=1\eta = 1 gives (x,v)−⟨x,v⟩=(0,0):(x, v) - \langle x, v\rangle = (0, 0)\mathclose{\text{:}} the minimum in one step (Figure 18.5, right). In terms of the original variables, the new method divides each partial derivative by the curvature in its direction before stepping.

A common cause of ill-conditioning is variables measured on very different scales. One common preprocessing step is to standardise nonconstant input features: subtract each feature’s training-data mean and divide by its standard deviation, the square root of the mean squared distance from that mean. The transformed training values then have mean 0 and standard deviation 1. This can help conditioning, though it does not remove every cause, such as strongly related features. Even centring alone can help. For the data of Example 18.9, the second partial derivatives of E(m,b)E(m, b) are Emm=2∑xi2=60,E_{mm} = 2\sum x_i^2 = 60\mathclose{\text{,}} Emb=2∑xi=20E_{mb} = 2\sum x_i = 20 and Ebb=2n=10;E_{bb} = 2n = 10\mathclose{\text{;}} the mixed term tilts the elliptical level curves, and they are long and thin. Centre the inputs, using xi−2x_i - 2 in place of xi,x_i\mathclose{\text{,}} where 2 is their mean: then ∑(xi−2)=0,\sum (x_i - 2) = 0\mathclose{\text{,}} the mixed term disappears, and Emm=20,E_{mm} = 20\mathclose{\text{,}} Emb=0,E_{mb} = 0\mathclose{\text{,}} Ebb=10,E_{bb} = 10\mathclose{\text{,}} a condition number of 2. The fitted line is the same, y=1.2(x−2)+4=1.2x+1.6y = 1.2(x - 2) + 4 = 1.2x + 1.6 (Exercise 18.12), but gradient descent finds it far faster in the computation below.

For a quadratic bowl whose elliptical level curves are tilted, the same analysis holds along the axes of the ellipses; their curvatures are the positive eigenvalues of the Hessian matrix, which Mathematical Foundations’ Module 13 studies. For a function with continuous second partial derivatives near a minimum, this gives a local guide when both Hessian eigenvalues there are positive: the quadratic approximation is a bowl. It is not a global convergence guarantee. A minimum with a zero eigenvalue, such as the origin for x4+y4,x^4 + y^4\mathclose{\text{,}} needs a different analysis. Mathematical Foundations’ Module 19 treats gradient methods and conditioning from the computing side, and the AI series shows them training real models.

Compute itCondition number and steps

How many steps does gradient descent with the best fixed step need on f=12(x2+κy2),f = \tfrac12(x^2 + \kappa y^2)\mathclose{\text{,}} from (1,1),(1, 1)\mathclose{\text{,}} to bring the point a million times closer to the minimum?

Python
import math

def steps_needed(kappa, max_steps=100000):
    if not math.isfinite(kappa) or kappa < 1:
        raise ValueError("kappa must be finite and at least 1")
    eta = 2/(1 + kappa)                         # the best fixed step
    x, y = 1.0, 1.0
    for k in range(max_steps + 1):
        if math.hypot(x, y) <= 1e-6*math.sqrt(2):
            return k                           # completed updates
        if k == max_steps:
            break
        x, y = x - eta*x, y - eta*kappa*y
    raise RuntimeError("distance tolerance not reached within the step limit")

for kappa in (1, 10, 100, 1000):
    print(f"kappa = {kappa:4}   steps needed: {steps_needed(kappa)}")
Output
kappa =    1   steps needed: 1
kappa =   10   steps needed: 69
kappa =  100   steps needed: 691
kappa = 1000   steps needed: 6908

From κ=10\kappa = 10 onwards, each tenfold increase in this table multiplies the work by about ten. The step limit prevents an endless loop if the requested reduction takes too long or floating-point arithmetic stops making progress.

Now least squares: fit the line of Example 18.9 by gradient descent on E(m,b),E(m, b)\mathclose{\text{,}} with the raw inputs and with the inputs centred, using the same step size η=0.025\eta = 0.025 for both, and stop when both partial derivatives have magnitude below 10−6:10^{-6}\mathclose{\text{:}}

Python
import numpy as np
xs = np.array([0, 1, 2, 3, 4.0])
ys = np.array([1, 4, 3, 6, 6.0])

def fit_by_descent(xs, ys, eta=0.025, max_steps=100000):
    m = b = 0.0
    for k in range(max_steps + 1):
        e = ys - (m*xs + b)                       # the residuals
        Em, Eb = -2*np.sum(xs*e), -2*np.sum(e)    # the partial derivatives of E
        if not np.isfinite([Em, Eb]).all():
            raise RuntimeError("nonfinite gradient; try a smaller step")
        if max(abs(Em), abs(Eb)) < 1e-6:
            return k, m, b                       # completed updates
        if k == max_steps:
            break
        m, b = m - eta*Em, b - eta*Eb
    raise RuntimeError("gradient tolerance not reached within the step limit")

for name, x in (("raw", xs), ("centred", xs - 2)):
    k, m, b = fit_by_descent(x, ys)
    print(f"{name:8} inputs: {k} steps, m = {m:.6f}, b = {b:.6f}")
Output
raw      inputs: 193 steps, m = 1.200000, b = 1.600000
centred  inputs: 61 steps, m = 1.200000, b = 4.000000

With the same step size, the centred inputs need 61 steps instead of 193. They also allow larger steps: the centred Hessian is diagonal with curvatures 20 and 10, so Theorem 18.7, applied after shifting the minimum to the origin, gives convergence from every start when 0<η<0.1.0 < \eta < 0.1\mathclose{\text{.}} From the same zero start, the raw inputs diverge already for η=0.03.\eta = 0.03\mathclose{\text{.}} The centred fit reports b≈4,b \approx 4\mathclose{\text{,}} the intercept at x=2:x = 2\mathclose{\text{:}} the line y=1.2(x−2)+4,y = 1.2(x - 2) + 4\mathclose{\text{,}} the same line as before. Both functions report an error if their tolerance is not reached within the permitted number of updates.

Watch out

Three slips with gradient descent. “A larger step is always faster”: a larger step can be worse even below 2/c.2/c\mathclose{\text{.}} For the quadratic bowl, a step above 2/c2/c makes any nonzero component in the steepest direction grow. “Gradient descent heads for the minimum”: a nonzero negative gradient gives the direction of steepest local decrease. On an elliptical bowl it points directly at the minimum on the axes, but usually across the valley elsewhere; circular level curves make every such direction radial. “It always finds a minimum”: it need not converge, and even a convergent sequence can reach a saddle. On f=x2−y2,f = x^2 - y^2\mathclose{\text{,}} with 0<η<1,0 < \eta < 1\mathclose{\text{,}} a start on the xx-axis stays there and converges to the saddle at the origin; a start off the axis escapes because its yy-coordinate is multiplied by 1+2η>11 + 2\eta > 1 at every step.

7

What you learned

  • A local extremum of f(x,y)f(x, y) is at a critical point, where ∇f=0\nabla f = \vb{0} or a partial derivative does not exist. An interior critical point that is neither a local maximum nor a local minimum is a saddle point, like the origin for x2−y2.x^2 - y^2\mathclose{\text{.}}
  • The second derivative test applies at an interior point where ∇f=0,\nabla f = \vb{0}\mathclose{\text{,}} when the second partial derivatives are continuous nearby. With D=fxxfyy−fxy2,D = f_{xx}f_{yy} - f_{xy}^2\mathclose{\text{,}} the determinant of the Hessian matrix: D>0D > 0 and fxx>0,f_{xx} > 0\mathclose{\text{,}} local minimum; D>0D > 0 and fxx<0,f_{xx} < 0\mathclose{\text{,}} local maximum; D<0,D < 0\mathclose{\text{,}} saddle point; D=0,D = 0\mathclose{\text{,}} inconclusive. It reads the sign of the quadratic approximation.
  • A continuous function on a nonempty, closed, bounded region has absolute extreme values (the Extreme Value Theorem): compare its values at the critical points inside with its extreme values on the boundary, found piece by piece with one-variable methods.
  • Lagrange multipliers: for ff and gg with continuous first partial derivatives nearby, an extremum of ff on g=cg = c where ∇g≠0\nabla g \ne \vb{0} satisfies ∇f=λ∇g.\nabla f = \lambda\nabla g\mathclose{\text{.}} Add singular points and any ends of the constraint, compare, and make sure an extremum exists. With two such constraints and independent gradients, ∇f=λ∇g+μ∇h.\nabla f = \lambda\nabla g + \mu\nabla h\mathclose{\text{.}} Along a differentiable family of optimal points, the multiplier is the rate at which the best value changes with c.c\mathclose{\text{.}}
  • Least squares: the line y=mx+by = mx + b that minimises E(m,b)=∑(yi−mxi−b)2E(m, b) = \sum(y_i - mx_i - b)^2 solves the normal equations, ∂E/∂m=∂E/∂b=0.\partial E/\partial m = \partial E/\partial b = 0\mathclose{\text{.}} It passes through the mean point and is unique when at least two inputs differ.
  • Gradient descent on a bowl 12(ax2+cy2),\tfrac12(ax^2 + cy^2)\mathclose{\text{,}} with 0<a≤c,0 < a \le c\mathclose{\text{,}} converges from every starting point exactly when 0<η<2/c.0 < \eta < 2/c\mathclose{\text{.}} The best fixed step shrinks the distance to the minimum by κ−1κ+1\tfrac{\kappa - 1}{\kappa + 1} per step, where κ=c/a\kappa = c/a is the condition number. Long, thin level curves can lead to slow, zigzagging paths; rescaling or centring can help.
8

Exercises

Exercise 18.1★Critical points and the test

Find and classify the critical points of

  • (a) f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x
  • (b) g(x,y)=x3−12x+y2+4yg(x, y) = x^3 - 12x + y^2 + 4y
Show solution✓ Checked

(a) fx=2x+y−3f_x = 2x + y - 3 and fy=x+2y.f_y = x + 2y\mathclose{\text{.}} The second equation gives x=−2y;x = -2y\mathclose{\text{;}} then the first becomes −4y+y−3=0,-4y + y - 3 = 0\mathclose{\text{,}} so y=−1y = -1 and x=2.x = 2\mathclose{\text{.}} The only critical point is (2,−1).(2, -1)\mathclose{\text{.}} The second partial derivatives are constants, fxx=2,f_{xx} = 2\mathclose{\text{,}} fxy=1f_{xy} = 1 and fyy=2,f_{yy} = 2\mathclose{\text{,}} so D=4−1=3>0D = 4 - 1 = 3 > 0 with fxx>0:f_{xx} > 0\mathclose{\text{:}} f(2,−1)=4−2+1−6=−3f(2, -1) = 4 - 2 + 1 - 6 = -3 is a local minimum value. In fact it is the absolute minimum: for a polynomial of degree 2 the quadratic approximation is exact, f(2+h,−1+k)=−3+h2+hk+k2,f(2 + h, -1 + k) = -3 + h^2 + hk + k^2\mathclose{\text{,}} and h2+hk+k2=(h+k2)2+34k2>0h^2 + hk + k^2 = \big(h + \tfrac{k}{2}\big)^2 + \tfrac34k^2 > 0 for (h,k)≠(0,0).(h, k) \ne (0, 0)\mathclose{\text{.}}

(b) gx=3x2−12=0g_x = 3x^2 - 12 = 0 gives x=±2,x = \pm2\mathclose{\text{,}} and gy=2y+4=0g_y = 2y + 4 = 0 gives y=−2:y = -2\mathclose{\text{:}} the critical points are (2,−2)(2, -2) and (−2,−2).(-2, -2)\mathclose{\text{.}} With gxx=6x,g_{xx} = 6x\mathclose{\text{,}} gxy=0g_{xy} = 0 and gyy=2,g_{yy} = 2\mathclose{\text{,}} D=12x.D = 12x\mathclose{\text{.}}

  • At (2,−2):(2, -2)\mathclose{\text{:}} D=24>0D = 24 > 0 and gxx=12>0,g_{xx} = 12 > 0\mathclose{\text{,}} so g(2,−2)=8−24+4−8=−20g(2, -2) = 8 - 24 + 4 - 8 = -20 is a local minimum value.
  • At (−2,−2):(-2, -2)\mathclose{\text{:}} D=−24<0,D = -24 < 0\mathclose{\text{,}} a saddle point, with g(−2,−2)=−8+24+4−8=12.g(-2, -2) = -8 + 24 + 4 - 8 = 12\mathclose{\text{.}}
Exercise 18.2★Reading the test

At five interior critical points of functions with continuous second partial derivatives nearby, the second partial derivatives are:

Point fxxf_{xx} fxyf_{xy} fyyf_{yy}
P1P_1 22 33 55
P2P_2 −1-1 22 −3-3
P3P_3 −4-4 11 −1-1
P4P_4 44 −2-2 11
P5P_5 00 11 00
  • (a) Classify each point, as far as the second derivative test allows.
  • (b) At P2,P_2\mathclose{\text{,}} both fxxf_{xx} and fyyf_{yy} are negative. Use the quadratic Q(h,k)Q(h, k) of Section 18.2 to find a direction in which ff increases away from P2.P_2\mathclose{\text{.}}
Show solution✓ Checked

(a) Compute D=fxxfyy−fxy2D = f_{xx}f_{yy} - f_{xy}^2 at each point.

  • P1:P_1\mathclose{\text{:}} D=10−9=1>0D = 10 - 9 = 1 > 0 and fxx>0:f_{xx} > 0\mathclose{\text{:}} a local minimum.
  • P2:P_2\mathclose{\text{:}} D=3−4=−1<0:D = 3 - 4 = -1 < 0\mathclose{\text{:}} a saddle point.
  • P3:P_3\mathclose{\text{:}} D=4−1=3>0D = 4 - 1 = 3 > 0 and fxx<0:f_{xx} < 0\mathclose{\text{:}} a local maximum.
  • P4:P_4\mathclose{\text{:}} D=4−4=0:D = 4 - 4 = 0\mathclose{\text{:}} the test is inconclusive.
  • P5:P_5\mathclose{\text{:}} D=0−1=−1<0:D = 0 - 1 = -1 < 0\mathclose{\text{:}} a saddle point.

(b) At P2,P_2\mathclose{\text{,}} Q(h,k)=−h2+4hk−3k2=−(h−k)(h−3k).Q(h, k) = -h^2 + 4hk - 3k^2 = -(h - k)(h - 3k)\mathclose{\text{.}} It is positive when h−kh - k and h−3kh - 3k have opposite signs, for example at (h,k)=(2,1),(h, k) = (2, 1)\mathclose{\text{,}} where Q=−4+8−3=1.Q = -4 + 8 - 3 = 1\mathclose{\text{.}} For displacements (h,k)=(2t,t),(h, k) = (2t, t)\mathclose{\text{,}} Q=t2,Q = t^2\mathclose{\text{,}} so the quadratic approximation and its smaller remainder give f(P2+(2t,t))>f(P2)f(P_2 + (2t, t)) > f(P_2) for sufficiently small nonzero t.t\mathclose{\text{.}} Along (t,0),(t, 0)\mathclose{\text{,}} Q=−t2,Q = -t^2\mathclose{\text{,}} and the values are smaller. A saddle point can hide behind two negative pure second derivatives; the mixed one decides.

Exercise 18.3★A constrained extremum on a circle

Use Lagrange multipliers to find the largest and smallest values of f(x,y)=x2+2y2f(x, y) = x^2 + 2y^2 on the circle x2+y2=1,x^2 + y^2 = 1\mathclose{\text{,}} and the multiplier at each candidate. Check your answer with the widget of Section 18.4, and explain with level curves why the extremes are where they are.

Show solution✓ Checked

With g=x2+y2,g = x^2 + y^2\mathclose{\text{,}} ∇f=⟨2x,4y⟩\nabla f = \langle 2x, 4y\rangle and ∇g=⟨2x,2y⟩,\nabla g = \langle 2x, 2y\rangle\mathclose{\text{,}} never 0\vb{0} on the circle. The equations are

2x=2λx,4y=2λy,x2+y2=1,2x = 2\lambda x, \quad 4y = 2\lambda y, \quad x^2 + y^2 = 1,

that is, x(1−λ)=0x(1 - \lambda) = 0 and y(2−λ)=0.y(2 - \lambda) = 0\mathclose{\text{.}} If x≠0,x \ne 0\mathclose{\text{,}} then λ=1,\lambda = 1\mathclose{\text{,}} so y(2−1)=0y(2 - 1) = 0 gives y=0y = 0 and x=±1.x = \pm1\mathclose{\text{.}} If x=0,x = 0\mathclose{\text{,}} the circle gives y=±1,y = \pm1\mathclose{\text{,}} and then λ=2.\lambda = 2\mathclose{\text{.}} The candidates are (±1,0),(\pm1, 0)\mathclose{\text{,}} with f=1f = 1 and λ=1,\lambda = 1\mathclose{\text{,}} and (0,±1),(0, \pm1)\mathclose{\text{,}} with f=2f = 2 and λ=2.\lambda = 2\mathclose{\text{.}} The circle is closed and bounded, so the largest value of ff on it is 2, at (0,±1),(0, \pm1)\mathclose{\text{,}} and the smallest is 1, at (±1,0).(\pm1, 0)\mathclose{\text{.}}

Cancelling xx in the first equation to get λ=1\lambda = 1 would have lost the two maximum points. In the widget, choose x2+2y2x^2 + 2y^2 and the circle: at t=0∘t = 0^\circ and 180∘180^\circ it reports λ=1,\lambda = 1\mathclose{\text{,}} at 90∘90^\circ and 270∘270^\circ λ=2.\lambda = 2\mathclose{\text{.}} For k>0,k > 0\mathclose{\text{,}} the level curves x2+2y2=kx^2 + 2y^2 = k are ellipses, 2\sqrt2 times as wide as they are high. The ellipse k=1,k = 1\mathclose{\text{,}} with semi-axes 1 and 12,\tfrac{1}{\sqrt2}\mathclose{\text{,}} lies inside the circle and touches it at (±1,0);(\pm1, 0)\mathclose{\text{;}} the ellipse k=2,k = 2\mathclose{\text{,}} with semi-axes 2\sqrt2 and 1, lies outside it and touches it at (0,±1).(0, \pm1)\mathclose{\text{.}}

Exercise 18.4★A line through three points

Find the least-squares line of the points (1,2),(1, 2)\mathclose{\text{,}} (2,3)(2, 3) and (3,5),(3, 5)\mathclose{\text{,}} its residuals and the sum of squared errors. Check that it passes through the mean point.

Show solution✓ Checked

The sums: n=3,n = 3\mathclose{\text{,}} ∑xi=6,\sum x_i = 6\mathclose{\text{,}} ∑xi2=1+4+9=14,\sum x_i^2 = 1 + 4 + 9 = 14\mathclose{\text{,}} ∑yi=10\sum y_i = 10 and ∑xiyi=2+6+15=23.\sum x_iy_i = 2 + 6 + 15 = 23\mathclose{\text{.}} The normal equations are

14m+6b=23,6m+3b=10.14m + 6b = 23, \qquad 6m + 3b = 10.

Subtract twice the second from the first: 2m=3,2m = 3\mathclose{\text{,}} so m=32,m = \tfrac32\mathclose{\text{,}} and then 3b=10−9=1,3b = 10 - 9 = 1\mathclose{\text{,}} so b=13.b = \tfrac13\mathclose{\text{.}} The line is y=32x+13.y = \tfrac32x + \tfrac13\mathclose{\text{.}} At x=1,2,3x = 1, 2, 3 it gives 116,\tfrac{11}{6}\mathclose{\text{,}} 103\tfrac{10}{3} and 296,\tfrac{29}{6}\mathclose{\text{,}} so the residuals are 16,\tfrac16\mathclose{\text{,}} −13-\tfrac13 and 16\tfrac16 (they add up to 0), and

E=136+19+136=16.E = \frac{1}{36} + \frac{1}{9} + \frac{1}{36} = \frac{1}{6}.

The mean point is (2,103),\big(2, \tfrac{10}{3}\big)\mathclose{\text{,}} and indeed 32⋅2+13=103.\tfrac32 \cdot 2 + \tfrac13 = \tfrac{10}{3}\mathclose{\text{.}}

Exercise 18.5★★Four critical points

Find the critical points of f(x,y)=2x3+xy2+5x2+y2f(x, y) = 2x^3 + xy^2 + 5x^2 + y^2 and classify them. Does ff have an absolute maximum?

Show solution✓ Checked

fx=6x2+y2+10xf_x = 6x^2 + y^2 + 10x and fy=2xy+2y=2y(x+1).f_y = 2xy + 2y = 2y(x + 1)\mathclose{\text{.}} The second is 0 when y=0y = 0 or x=−1.x = -1\mathclose{\text{.}}

  • If y=0:y = 0\mathclose{\text{:}} 6x2+10x=2x(3x+5)=0,6x^2 + 10x = 2x(3x + 5) = 0\mathclose{\text{,}} so x=0x = 0 or x=−53.x = -\tfrac53\mathclose{\text{.}}
  • If x=−1:x = -1\mathclose{\text{:}} 6+y2−10=0,6 + y^2 - 10 = 0\mathclose{\text{,}} so y2=4y^2 = 4 and y=±2.y = \pm2\mathclose{\text{.}}

The critical points are (0,0),(0, 0)\mathclose{\text{,}} (−53,0),\big(-\tfrac53, 0\big)\mathclose{\text{,}} (−1,2)(-1, 2) and (−1,−2).(-1, -2)\mathclose{\text{.}} The second partial derivatives are fxx=12x+10,f_{xx} = 12x + 10\mathclose{\text{,}} fxy=2yf_{xy} = 2y and fyy=2x+2,f_{yy} = 2x + 2\mathclose{\text{,}} so:

  • At (0,0):(0, 0)\mathclose{\text{:}} fxx=10,f_{xx} = 10\mathclose{\text{,}} fxy=0,f_{xy} = 0\mathclose{\text{,}} fyy=2,f_{yy} = 2\mathclose{\text{,}} so D=20>0D = 20 > 0 with fxx>0:f_{xx} > 0\mathclose{\text{:}} a local minimum, f(0,0)=0.f(0, 0) = 0\mathclose{\text{.}}
  • At (−53,0):\big(-\tfrac53, 0\big)\mathclose{\text{:}} fxx=−10,f_{xx} = -10\mathclose{\text{,}} fxy=0,f_{xy} = 0\mathclose{\text{,}} fyy=−43,f_{yy} = -\tfrac43\mathclose{\text{,}} so D=403>0D = \tfrac{40}{3} > 0 with fxx<0:f_{xx} < 0\mathclose{\text{:}} a local maximum, f=12527.f = \tfrac{125}{27}\mathclose{\text{.}}
  • At (−1,±2):(-1, \pm2)\mathclose{\text{:}} fxx=−2,f_{xx} = -2\mathclose{\text{,}} fxy=±4,f_{xy} = \pm4\mathclose{\text{,}} fyy=0,f_{yy} = 0\mathclose{\text{,}} so D=−16<0:D = -16 < 0\mathclose{\text{:}} two saddle points, where f=3.f = 3\mathclose{\text{.}}

For instance

f(−53,0)=2(−12527)+5⋅259=−250+37527=12527≈4.630.f\big(-\tfrac53, 0\big) = 2\Big(-\frac{125}{27}\Big) + 5 \cdot \frac{25}{9} = \frac{-250 + 375}{27} = \frac{125}{27} \approx 4.630.

The local maximum is not an absolute maximum, and there is none: f(x,0)=2x3+5x2→∞f(x, 0) = 2x^3 + 5x^2 \to \infty as x→∞.x \to \infty\mathclose{\text{.}}

Exercise 18.6★★Extreme values on a disc

Find the absolute maximum and minimum values of f(x,y)=x2+2y2−2xf(x, y) = x^2 + 2y^2 - 2x on the disc x2+y2≤4.x^2 + y^2 \le 4\mathclose{\text{.}}

Show solution✓ Checked

ff is continuous and the disc is closed and bounded, so both values exist.

Inside. fx=2x−2f_x = 2x - 2 and fy=4yf_y = 4y are 0 only at (1,0),(1, 0)\mathclose{\text{,}} which is inside the disc since 1<4.1 < 4\mathclose{\text{.}} There f(1,0)=1−2=−1.f(1, 0) = 1 - 2 = -1\mathclose{\text{.}}

On the boundary. Parametrise the circle by x=2cos⁡t,x = 2\cos t\mathclose{\text{,}} y=2sin⁡t,y = 2\sin t\mathclose{\text{,}} 0≤t≤2π.0 \le t \le 2\pi\mathclose{\text{.}} With sin⁡2t=1−cos⁡2t,\sin^2 t = 1 - \cos^2 t\mathclose{\text{,}}

f=4cos⁡2t+8sin⁡2t−4cos⁡t=8−4cos⁡2t−4cos⁡t.f = 4\cos^2 t + 8\sin^2 t - 4\cos t = 8 - 4\cos^2 t - 4\cos t.

As tt runs round the circle, u=cos⁡tu = \cos t runs through [−1,1],[-1, 1]\mathclose{\text{,}} so find the extreme values of φ(u)=8−4u2−4u\varphi(u) = 8 - 4u^2 - 4u on [−1,1].[-1, 1]\mathclose{\text{.}} φ′(u)=−8u−4=0\varphi'(u) = -8u - 4 = 0 at u=−12,u = -\tfrac12\mathclose{\text{,}} with φ(−12)=8−1+2=9;\varphi\big(-\tfrac12\big) = 8 - 1 + 2 = 9\mathclose{\text{;}} at the ends, φ(1)=0\varphi(1) = 0 and φ(−1)=8.\varphi(-1) = 8\mathclose{\text{.}} The value 9 is taken where cos⁡t=−12,\cos t = -\tfrac12\mathclose{\text{,}} at the points (−1,±3);(-1, \pm\sqrt3)\mathclose{\text{;}} the value 0 at (2,0),(2, 0)\mathclose{\text{,}} and 8 at (−2,0).(-2, 0)\mathclose{\text{.}}

Compare −1,-1\mathclose{\text{,}} 9,9\mathclose{\text{,}} 00 and 8:8\mathclose{\text{:}} the absolute maximum value is 9, at (−1,3)(-1, \sqrt3) and (−1,−3),(-1, -\sqrt3)\mathclose{\text{,}} and the absolute minimum value is −1,-1\mathclose{\text{,}} at (1,0),(1, 0)\mathclose{\text{,}} inside the disc.

Exercise 18.7★★The closest point of a plane

Use Lagrange multipliers to find the point of the plane x+2y+2z=14x + 2y + 2z = 14 closest to the point (1,1,1),(1, 1, 1)\mathclose{\text{,}} and the distance. Explain why a closest point exists, and check the answer geometrically.

Show solution✓ Checked

Minimise the square of the distance, f=(x−1)2+(y−1)2+(z−1)2,f = (x - 1)^2 + (y - 1)^2 + (z - 1)^2\mathclose{\text{,}} subject to g=x+2y+2z=14,g = x + 2y + 2z = 14\mathclose{\text{,}} with ∇g=⟨1,2,2⟩≠0.\nabla g = \langle 1, 2, 2\rangle \ne \vb{0}\mathclose{\text{.}} The equations ∇f=λ∇g\nabla f = \lambda\nabla g are

2(x−1)=λ,2(y−1)=2λ,2(z−1)=2λ,2(x - 1) = \lambda, \quad 2(y - 1) = 2\lambda, \quad 2(z - 1) = 2\lambda,

so x=1+λ2x = 1 + \tfrac{\lambda}{2} and y=z=1+λ.y = z = 1 + \lambda\mathclose{\text{.}} The plane gives (1+λ2)+2(1+λ)+2(1+λ)=5+92λ=14,\big(1 + \tfrac{\lambda}{2}\big) + 2(1 + \lambda) + 2(1 + \lambda) = 5 + \tfrac92\lambda = 14\mathclose{\text{,}} so λ=2\lambda = 2 and the point is (2,3,3).(2, 3, 3)\mathclose{\text{.}} Its distance from (1,1,1)(1, 1, 1) is 1+4+4=3.\sqrt{1 + 4 + 4} = 3\mathclose{\text{.}}

A closest point exists. The points of the plane with f≤9f \le 9 form a closed, bounded set, nonempty because it contains (2,3,3).(2, 3, 3)\mathclose{\text{.}} On it ff has a smallest value (the Extreme Value Theorem in three variables), at most 9, and outside it f>9.f > 9\mathclose{\text{.}} So that smallest value is the minimum over the whole plane, at a point where the method applies: it is the only candidate, (2,3,3).(2, 3, 3)\mathclose{\text{.}} The set with f≤9f \le 9 therefore consists of that point.

Geometrically, (2,3,3)−(1,1,1)=⟨1,2,2⟩,(2, 3, 3) - (1, 1, 1) = \langle 1, 2, 2\rangle\mathclose{\text{,}} the normal vector of the plane (Module 16): the shortest segment from a point to a plane is perpendicular to the plane. Module 16’s formula for the distance from a point to a plane gives the same answer:

∣1+2⋅1+2⋅1−14∣12+22+22=93=3.\frac{\abs{1 + 2 \cdot 1 + 2 \cdot 1 - 14}}{\sqrt{1^2 + 2^2 + 2^2}} = \frac{9}{3} = 3.
Exercise 18.8★★Gradient descent by hand

Let f(x,y)=x2+4y2.f(x, y) = x^2 + 4y^2\mathclose{\text{.}}

  • (a) Starting from (4,2),(4, 2)\mathclose{\text{,}} take three steps of gradient descent with η=0.2.\eta = 0.2\mathclose{\text{.}}
  • (b) For which step sizes does gradient descent converge from every starting point? What happens with η=0.3?\eta = 0.3\mathclose{\text{?}}
  • (c) Which step size is best in the sense of Section 18.6, and what is the condition number?
  • (d) Rescale yy so that the level curves become circles, and find a step size that reaches the minimum in one step.
Show solution✓ Checked

(a) ∇f=⟨2x,8y⟩,\nabla f = \langle 2x, 8y\rangle\mathclose{\text{,}} so with η=0.2,\eta = 0.2\mathclose{\text{,}} xk+1=xk−0.4xk=0.6xkx_{k+1} = x_k - 0.4x_k = 0.6x_k and yk+1=yk−1.6yk=−0.6yk:y_{k+1} = y_k - 1.6y_k = -0.6y_k\mathclose{\text{:}}

(4,2)→(2.4,−1.2)→(1.44,0.72)→(0.864,−0.432).(4, 2) \to (2.4, -1.2) \to (1.44, 0.72) \to (0.864, -0.432).

(b) f=12(2x2+8y2),f = \tfrac12(2x^2 + 8y^2)\mathclose{\text{,}} so a=2a = 2 and c=8c = 8 in Theorem 18.7: gradient descent converges from every start exactly when 0<η<28=0.25.0 < \eta < \tfrac28 = 0.25\mathclose{\text{.}} With η=0.3,\eta = 0.3\mathclose{\text{,}} yk+1=(1−2.4)yk=−1.4yk:y_{k+1} = (1 - 2.4)y_k = -1.4y_k\mathclose{\text{:}} from any start with y0≠0y_0 \ne 0 the yy-coordinate zigzags with growing size, and the method diverges.

(c) The best fixed step is η∗=2a+c=210=0.2,\eta^* = \tfrac{2}{a + c} = \tfrac{2}{10} = 0.2\mathclose{\text{,}} the step of (a), where both coordinates shrink by the factor 0.6=8−28+2.0.6 = \tfrac{8 - 2}{8 + 2}\mathclose{\text{.}} The condition number is κ=82=4,\kappa = \tfrac82 = 4\mathclose{\text{,}} and indeed κ−1κ+1=35.\tfrac{\kappa - 1}{\kappa + 1} = \tfrac35\mathclose{\text{.}}

(d) Put v=2y:v = 2y\mathclose{\text{:}} then f=x2+v2,f = x^2 + v^2\mathclose{\text{,}} whose level curves are circles. Its gradient in xx and vv is ⟨2x,2v⟩,\langle 2x, 2v\rangle\mathclose{\text{,}} and the step η=0.5\eta = 0.5 gives (x,v)−0.5⟨2x,2v⟩=(0,0):(x, v) - 0.5\langle 2x, 2v\rangle = (0, 0)\mathclose{\text{:}} the minimum in one step, from any start.

Exercise 18.9★★The cheapest can, with a multiplier

Example 8.14 found the closed cylindrical can of volume 330 cm³ with the least surface area by eliminating h.h\mathclose{\text{.}} Solve the same problem with Lagrange multipliers: minimise A(r,h)=2πr2+2πrhA(r, h) = 2\pi r^2 + 2\pi rh subject to πr2h=330,\pi r^2h = 330\mathclose{\text{,}} with r,h>0.r, h > 0\mathclose{\text{.}} Then use the meaning of λ\lambda (Section 18.4) to estimate how much more metal a can of 331 cm³ needs.

Show solution✓ Checked

∇A=⟨4πr+2πh, 2πr⟩\nabla A = \langle 4\pi r + 2\pi h,\ 2\pi r\rangle and, with V=πr2h,V = \pi r^2h\mathclose{\text{,}} ∇V=⟨2πrh, πr2⟩.\nabla V = \langle 2\pi rh,\ \pi r^2\rangle\mathclose{\text{.}} The equations are

4πr+2πh=λ 2πrh,2πr=λ πr2,πr2h=330.\begin{aligned} 4\pi r + 2\pi h &= \lambda\,2\pi rh, \\ 2\pi r &= \lambda\,\pi r^2, \\ \pi r^2h &= 330. \end{aligned}

The second, divided by πr>0,\pi r > 0\mathclose{\text{,}} gives λ=2r.\lambda = \tfrac{2}{r}\mathclose{\text{.}} Then the first becomes 4πr+2πh=2r⋅2πrh=4πh,4\pi r + 2\pi h = \tfrac2r \cdot 2\pi rh = 4\pi h\mathclose{\text{,}} so 4πr=2πh4\pi r = 2\pi h and h=2r:h = 2r\mathclose{\text{:}} the best can is as tall as it is wide, as in Example 8.14. The volume gives 2πr3=330,2\pi r^3 = 330\mathclose{\text{,}} so r=165/π3≈3.745r = \sqrt[3]{165/\pi} \approx 3.745 cm, h≈7.49h \approx 7.49 cm and λ=2r≈0.534.\lambda = \tfrac2r \approx 0.534\mathclose{\text{.}} That this candidate gives the minimum was shown in Example 8.14.

By the Going deeper box of Section 18.4, λ\lambda is the rate at which the least area grows with the volume: about 0.534 cm² of metal per extra cm³. So a 331 cm³ can needs about 0.53 cm² more metal. (The exact least area is 6πr26\pi r^2 with r3=V2π,r^3 = \tfrac{V}{2\pi}\mathclose{\text{,}} and 6π(3312π)2/3−6π(3302π)2/3≈0.534.)6\pi\big(\tfrac{331}{2\pi}\big)^{2/3} - 6\pi\big(\tfrac{330}{2\pi}\big)^{2/3} \approx 0.534\mathclose{\text{.)}}

Exercise 18.10★★★The best fixed step

Let f(x,y)=12(ax2+cy2)f(x, y) = \tfrac12(ax^2 + cy^2) with 0<a<c,0 < a < c\mathclose{\text{,}} and κ=c/a.\kappa = c/a\mathclose{\text{.}}

  • (a) Show that one step of gradient descent multiplies the distance from the minimum by at most ρ(η)=max⁡(∣1−ηa∣,∣1−ηc∣),\rho(\eta) = \max\big(\abs{1 - \eta a}, \abs{1 - \eta c}\big)\mathclose{\text{,}} and exactly by ρ(η)\rho(\eta) for a suitable starting point.
  • (b) Show that ρ(η)\rho(\eta) is smallest at η∗=2a+c,\eta^* = \dfrac{2}{a + c}\mathclose{\text{,}} where it equals κ−1κ+1.\dfrac{\kappa - 1}{\kappa + 1}\mathclose{\text{.}}
  • (c) With η∗,\eta^*\mathclose{\text{,}} about how many steps does it take to bring the point 10610^6 times closer to the minimum when κ\kappa is large? Use ln⁡(1+u)≈u\ln(1 + u) \approx u for small u,u\mathclose{\text{,}} and compare with the Compute it box of Section 18.6.
Show solution✓ Checked

(a) A step takes (x,y)(x, y) to ((1−ηa)x, (1−ηc)y),\big((1 - \eta a)x,\ (1 - \eta c)y\big)\mathclose{\text{,}} whose distance from the origin is

(1−ηa)2x2+(1−ηc)2y2≤ρ2x2+ρ2y2=ρx2+y2.\sqrt{(1 - \eta a)^2x^2 + (1 - \eta c)^2y^2} \le \sqrt{\rho^2x^2 + \rho^2y^2} = \rho\sqrt{x^2 + y^2}.

If ρ=∣1−ηa∣,\rho = \abs{1 - \eta a}\mathclose{\text{,}} a start on the xx-axis is multiplied by exactly ρ;\rho\mathclose{\text{;}} if ρ=∣1−ηc∣,\rho = \abs{1 - \eta c}\mathclose{\text{,}} a start on the yy-axis is.

(b) For every η>0,\eta > 0\mathclose{\text{,}} ρ(η)≥1−ηa\rho(\eta) \ge 1 - \eta a and ρ(η)≥ηc−1,\rho(\eta) \ge \eta c - 1\mathclose{\text{,}} since ∣s∣≥s\abs{s} \ge s and ∣s∣≥−s.\abs{s} \ge -s\mathclose{\text{.}} At η∗,\eta^*\mathclose{\text{,}} 1−η∗a=c−ac+a1 - \eta^*a = \tfrac{c - a}{c + a} and η∗c−1=c−ac+a,\eta^*c - 1 = \tfrac{c - a}{c + a}\mathclose{\text{,}} so ρ(η∗)=c−ac+a.\rho(\eta^*) = \tfrac{c - a}{c + a}\mathclose{\text{.}} If η<η∗,\eta < \eta^*\mathclose{\text{,}} then ρ(η)≥1−ηa>1−η∗a=ρ(η∗);\rho(\eta) \ge 1 - \eta a > 1 - \eta^*a = \rho(\eta^*)\mathclose{\text{;}} if η>η∗,\eta > \eta^*\mathclose{\text{,}} then ρ(η)≥ηc−1>η∗c−1=ρ(η∗).\rho(\eta) \ge \eta c - 1 > \eta^*c - 1 = \rho(\eta^*)\mathclose{\text{.}} So η∗\eta^* is the best fixed step, and dividing the top and bottom by aa gives ρ(η∗)=κ−1κ+1.\rho(\eta^*) = \tfrac{\kappa - 1}{\kappa + 1}\mathclose{\text{.}}

(c) After kk steps the distance is at most ρk\rho^k times the first one, so it is enough that ρk≤10−6,\rho^k \le 10^{-6}\mathclose{\text{,}} that is,

k≥ln⁡106ln⁡κ+1κ−1.k \ge \frac{\ln 10^6}{\ln\frac{\kappa + 1}{\kappa - 1}}.

With u=1κ,u = \tfrac1\kappa\mathclose{\text{,}} ln⁡κ+1κ−1=ln⁡(1+u)−ln⁡(1−u)≈u−(−u)=2κ,\ln\tfrac{\kappa + 1}{\kappa - 1} = \ln(1 + u) - \ln(1 - u) \approx u - (-u) = \tfrac{2}{\kappa}\mathclose{\text{,}} so k≈ln⁡1062 κ≈6.91κ:k \approx \tfrac{\ln 10^6}{2}\,\kappa \approx 6.91\kappa\mathclose{\text{:}} about 69 steps for κ=10,\kappa = 10\mathclose{\text{,}} 691 for κ=100\kappa = 100 and 6908 for κ=1000.\kappa = 1000\mathclose{\text{.}} The Compute it box counted 69, 691 and 6908 steps (from (1,1),(1, 1)\mathclose{\text{,}} where both coordinates shrink by exactly ρ).\rho\mathclose{\text{).}} The work grows in proportion to the condition number.

Exercise 18.11★★★Means and boxes
  • (a) Show that a3+b3+c3−3abc=12(a+b+c)[(a−b)2+(b−c)2+(c−a)2],a^3 + b^3 + c^3 - 3abc = \tfrac12(a + b + c)\big[(a - b)^2 + (b - c)^2 + (c - a)^2\big]\mathclose{\text{,}} and deduce that a3+b3+c3≥3abca^3 + b^3 + c^3 \ge 3abc for positive a,a\mathclose{\text{,}} b,b\mathclose{\text{,}} c,c\mathclose{\text{,}} with equality only when a=b=c.a = b = c\mathclose{\text{.}}
  • (b) Deduce the inequality of the arithmetic and geometric means used in Example 18.7: for positive p,p\mathclose{\text{,}} q,q\mathclose{\text{,}} r,r\mathclose{\text{,}} p+q+r3≥pqr3,\tfrac{p + q + r}{3} \ge \sqrt[3]{pqr}\mathclose{\text{,}} with equality only when p=q=r.p = q = r\mathclose{\text{.}}
  • (c) Prove that a closed box (with a lid) of surface area SS has volume at most (S6)3/2,\big(\tfrac S6\big)^{3/2}\mathclose{\text{,}} with equality only for a cube. Check with Lagrange multipliers for S=24.S = 24\mathclose{\text{.}}
Show solution✓ Checked

(a) Expand the right side. The bracket is 2(a2+b2+c2)−2(ab+bc+ca),2(a^2 + b^2 + c^2) - 2(ab + bc + ca)\mathclose{\text{,}} so the right side is (a+b+c)(a2+b2+c2−ab−bc−ca);(a + b + c)(a^2 + b^2 + c^2 - ab - bc - ca)\mathclose{\text{;}} multiplying out, the terms a2b,a^2b\mathclose{\text{,}} ab2,ab^2\mathclose{\text{,}} … cancel in pairs and leave a3+b3+c3−3abc.a^3 + b^3 + c^3 - 3abc\mathclose{\text{.}} For positive a,a\mathclose{\text{,}} b,b\mathclose{\text{,}} cc the factor a+b+ca + b + c is positive and the bracket, a sum of squares, is at least 0, so a3+b3+c3−3abc≥0.a^3 + b^3 + c^3 - 3abc \ge 0\mathclose{\text{.}} Equality needs the bracket to be 0, that is, a=b=c.a = b = c\mathclose{\text{.}}

(b) Put a=p3,a = \sqrt[3]p\mathclose{\text{,}} b=q3b = \sqrt[3]q and c=r3,c = \sqrt[3]r\mathclose{\text{,}} which are positive. Then (a) says p+q+r≥3p3q3r3=3pqr3,p + q + r \ge 3\sqrt[3]{p}\sqrt[3]{q}\sqrt[3]{r} = 3\sqrt[3]{pqr}\mathclose{\text{,}} with equality only when p3=q3=r3,\sqrt[3]p = \sqrt[3]q = \sqrt[3]r\mathclose{\text{,}} that is, p=q=r.p = q = r\mathclose{\text{.}}

(c) For a box xx by yy by z,z\mathclose{\text{,}} the area is S=2(xy+yz+zx)S = 2(xy + yz + zx) and the volume V=xyz.V = xyz\mathclose{\text{.}} Apply (b) to p=xy,p = xy\mathclose{\text{,}} q=yzq = yz and r=zx,r = zx\mathclose{\text{,}} whose sum is S2\tfrac S2 and whose product is (xyz)2=V2:(xyz)^2 = V^2\mathclose{\text{:}} S6≥V2/3,\tfrac S6 \ge V^{2/3}\mathclose{\text{,}} so V≤(S6)3/2.V \le \big(\tfrac S6\big)^{3/2}\mathclose{\text{.}} Equality needs xy=yz=zx,xy = yz = zx\mathclose{\text{,}} which for positive numbers means x=y=z:x = y = z\mathclose{\text{:}} a cube. For S=24S = 24 the bound is 43/2=8,4^{3/2} = 8\mathclose{\text{,}} the cube of side 2. With Lagrange multipliers, ∇V=λ∇(2(xy+yz+zx))\nabla V = \lambda\nabla\big(2(xy + yz + zx)\big) reads yz=2λ(y+z),yz = 2\lambda(y + z)\mathclose{\text{,}} xz=2λ(x+z),xz = 2\lambda(x + z)\mathclose{\text{,}} xy=2λ(x+y);xy = 2\lambda(x + y)\mathclose{\text{;}} multiplying by x,x\mathclose{\text{,}} yy and zz as in Example 18.7 gives xy+xz=xy+yz=xz+yz,xy + xz = xy + yz = xz + yz\mathclose{\text{,}} so x=y=z,x = y = z\mathclose{\text{,}} and 6x2=246x^2 = 24 gives x=2x = 2 and λ=yz2(y+z)=12.\lambda = \tfrac{yz}{2(y + z)} = \tfrac12\mathclose{\text{.}}

Exercise 18.12★★★Centring the data

Let (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) be data with at least two different xi,x_i\mathclose{\text{,}} and means xˉ,\bar x\mathclose{\text{,}} yˉ.\bar y\mathclose{\text{.}}

  • (a) Show that the slope of the least-squares line is m=∑(xi−xˉ)(yi−yˉ)∑(xi−xˉ)2.m = \dfrac{\sum(x_i - \bar x)(y_i - \bar y)}{\sum(x_i - \bar x)^2}\mathclose{\text{.}}
  • (b) Suppose the data are centred: xˉ=0.\bar x = 0\mathclose{\text{.}} Show that the normal equations become (∑xi2)m=∑xiyi\big(\sum x_i^2\big)m = \sum x_iy_i and b=yˉ,b = \bar y\mathclose{\text{,}} and that Emb=0.E_{mb} = 0\mathclose{\text{.}}
  • (c) Fit the data of Example 18.9 with x−2x - 2 in place of x,x\mathclose{\text{,}} and check that you get the line found there.
Show solution✓ Checked

(a) The proof of Theorem 18.6 found m=∑xiyi−nxˉyˉ∑xi2−nxˉ2m = \dfrac{\sum x_iy_i - n\bar x\bar y}{\sum x_i^2 - n\bar x^2} and showed that the denominator is ∑(xi−xˉ)2.\sum(x_i - \bar x)^2\mathclose{\text{.}} For the numerator, expand:

∑(xi−xˉ)(yi−yˉ)=∑xiyi−yˉ∑xi−xˉ∑yi+nxˉyˉ=∑xiyi−nxˉyˉ,\sum(x_i - \bar x)(y_i - \bar y) = \sum x_iy_i - \bar y\sum x_i - \bar x\sum y_i + n\bar x\bar y = \sum x_iy_i - n\bar x\bar y,

since ∑xi=nxˉ\sum x_i = n\bar x and ∑yi=nyˉ.\sum y_i = n\bar y\mathclose{\text{.}}

(b) If xˉ=0,\bar x = 0\mathclose{\text{,}} then ∑xi=0,\sum x_i = 0\mathclose{\text{,}} and the normal equations lose their mixed terms: (∑xi2)m=∑xiyi\big(\sum x_i^2\big)m = \sum x_iy_i and nb=∑yi,nb = \sum y_i\mathclose{\text{,}} that is, b=yˉ.b = \bar y\mathclose{\text{.}} The mixed partial derivative is Emb=2∑xi=0:E_{mb} = 2\sum x_i = 0\mathclose{\text{:}} above the minimum value, the level curves of EE are ellipses with axes along the mm and bb directions (circles if ∑xi2=n),\sum x_i^2 = n\mathclose{\text{),}} and the two unknowns can be found separately. At the minimum value the level set is a single point.

(c) The centred inputs are −2,−1,0,1,2,-2, -1, 0, 1, 2\mathclose{\text{,}} with the same yy values 1,4,3,6,6.1, 4, 3, 6, 6\mathclose{\text{.}} Then ∑xi2=10\sum x_i^2 = 10 and ∑xiyi=−2−4+0+6+12=12,\sum x_iy_i = -2 - 4 + 0 + 6 + 12 = 12\mathclose{\text{,}} so m=1210=1.2,m = \tfrac{12}{10} = 1.2\mathclose{\text{,}} and b=yˉ=4.b = \bar y = 4\mathclose{\text{.}} The line is y=1.2(x−2)+4=1.2x+1.6,y = 1.2(x - 2) + 4 = 1.2x + 1.6\mathclose{\text{,}} as in Example 18.9.

9

Check yourself

1
At an interior critical point of a function with continuous second partial derivatives nearby, fxx=3,f_{xx} = 3\mathclose{\text{,}} fxy=4f_{xy} = 4 and fyy=2.f_{yy} = 2\mathclose{\text{.}} The point is
2
At an interior point of the domain, ∇f(a,b)=0.\nabla f(a, b) = \vb{0}\mathclose{\text{.}} Which statement must be true?
3
At an interior point (a,b)(a, b) where fx=fy=0,f_x = f_y = 0\mathclose{\text{,}} the second partial derivatives are continuous nearby, D=0D = 0 and fxx=2.f_{xx} = 2\mathclose{\text{.}} Then
4
On which of these sets does every continuous function f(x,y)f(x, y) have an absolute maximum and an absolute minimum?
5
ff and gg have continuous first partial derivatives near (a,b).(a, b)\mathclose{\text{.}} There ff has a maximum on the curve g(x,y)=c,g(x, y) = c\mathclose{\text{,}} and ∇g(a,b)≠0.\nabla g(a, b) \ne \vb{0}\mathclose{\text{.}} Then at (a,b)(a, b)
6
The largest value of f(x,y)=x+yf(x, y) = x + y on the circle x2+y2=2x^2 + y^2 = 2 is
7
The least-squares line y=mx+by = mx + b of data (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) always
8
Gradient descent on f(x,y)=12(x2+25y2)f(x, y) = \tfrac12(x^2 + 25y^2) converges to the minimum from every starting point exactly when
Finished this module?