Skip to the content

Module 1: Rates of change: the question calculus answers

Module 2: Limits

Module 3: Continuity

Module 4: The derivative

Module 5: Rules for derivatives

Module 6: The chain rule and implicit differentiation

Module 7: Graphs, extremes and optimisation

Module 8: Linear approximation, related rates and Newton's method

Module 9: Area and the definite integral

Module 10: The fundamental theorem of calculus

Module 11: Substitution and integration by parts

Module 12: Areas, volumes and averages

Module 13: Numerical integration

Module 14: Infinity: L'Hôpital and improper integrals

Module 15: Sequences and series

Module 16: Taylor series

Module 17: Differential equations

Module 18: Functions of several variables

Module 19: Multiple integrals and vector calculus

Module 20: Rigour and the frontier

Answer key

  1. 1. Rates of change: the question calculus answers · The average rate of change of a function · QuizWhat is the average rate of change of f(x)=x2 from x=2 to x=5?B. 7f(5)=25 and f(2)=4, so the change in output is 21. The change in input is 5−2=3, and 21/3=7. The tempting 21 is only the change in output: a rate always divides by the change in input.
  2. 1. Rates of change: the question calculus answers · The average rate of change of a function · ProblemFind the average rate of change of f(x)=x3−2x from x=1 to x=3.11
    1. The outputs at the two ends:
    2. f(1)=1−2=−1,f(3)=27−6=21
    3. Divide the change in output by the change in input:
    4. f(3)−f(1)3−1=21−(−1)2=222=11
    5. On average, f grows by 11 for each 1 that x grows. Watch the sign of f(1): subtracting −1 adds 1.
  3. 1. Rates of change: the question calculus answers · Speed from a distance graph · ProblemThe ball has fallen d(t)=4.9t2 metres after t seconds. What is its average speed between t=2 s and t=3 s?24.5 m/s
    1. Where the ball is at each time:
    2. d(2)=4.9⋅4=19.6 m,d(3)=4.9⋅9=44.1 m
    3. average speed=44.1−19.63−2=24.5 m/s
    4. That is faster than the 14.7 m/s of the second before: a falling ball keeps speeding up.
  4. 1. Rates of change: the question calculus answers · Speed from a distance graph · Code challengeWrite averageRate(f, a, b): the average rate of change of the function f from a to b, that is the change in f divided by the change in input.One solution:
    Solution
    #include <functional>
    double averageRate(std::function<double(double)> f,                   double a, double b) {  return (f(b) - f(a)) / (b - a);}
    It is the formula f(b)−f(a)b−a as code: call f at both ends, subtract, divide by the run. Swapping the ends flips the sign of both the top and the bottom, so the rate stays the same. A common slip is forgetting the brackets: f(b) - f(a) / (b - a) divides only f(a).
  5. 1. Rates of change: the question calculus answers · Speed right now is a puzzle · QuizWhat speed do these averages approach, as h gets closer and closer to 0?C. 9.8 m/sThe average is 4.9(2+h)=9.8+4.9h. As h shrinks, 4.9h shrinks to nothing, leaving 9.8 m/s. That number is the instantaneous speed at 1 s. Notice you never set h=0 (that would be 00): you asked what the averages approach. That idea is a limit, the next module.
  6. 1. Rates of change: the question calculus answers · The area under a speed graph is the distance · QuizA cyclist rides at 6 m/s for 10 s, then at 4 m/s for 5 s. How far does she go?B. 80 mThe speed graph is two rectangles: 6×10=60 m and 4×5=20 m, so 80 m in all. Averaging the speeds, 6+42=5 m/s over 15 s gives 75 m, which is wrong: she spent longer at 6 m/s, so the times must weigh the speeds. Adding areas does that for you.
  7. 1. Rates of change: the question calculus answers · Two questions, slopes and areas · QuizWhich of these is a question for integral calculus? Choose all that apply.A. How much water flowed into a bath, given the flow rate each second; C. The total rainfall in a day, given the rate per hourAdding up a rate over time to get a total is an area question: water from flow rate, rainfall from rate per hour. The balloon’s growth "at the moment" and the steepest point are both about slopes, so they belong to differential calculus.
  8. 1. Rates of change: the question calculus answers · Where calculus came from · Put in orderPut these in the order they happened.1. Archimedes finds the area of a parabolic segment 2. Fermat finds tangents and maxima of curves 3. Newton develops his method of fluxions 4. Leibniz publishes the first paper on differential calculus 5. Cauchy builds calculus on limits 6. Weierstrass gives the ε-δ definitionsArchimedes (around 250 BC), Fermat (1630s), Newton (1665–66, unpublished), Leibniz (1684), Cauchy (1820s), Weierstrass (around 1860). Newton had his ideas first, but Leibniz published first, which is why the dispute was so heated.
  9. 1. Rates of change: the question calculus answers · Practice problems · ProblemLet f(x)=x2+3x. Simplify the average rate of change of f from x=2 to x=2+h (for h≠0). Then say what it approaches as h gets closer and closer to 0.Average rate = 7 + h; It approaches 7
    1. The two outputs:
    2. f(2)=4+6=10f(2+h)=(2+h)2+3(2+h)=4+4h+h2+6+3h=10+7h+h2
    3. The average rate of change, the slope of the secant:
    4. f(2+h)−f(2)h=7h+h2h=7+h
    5. As h shrinks towards 0, 7+h heads for 7: the slope of the curve right at x=2. You never set h=0; you watch where 7+h goes.
  10. 1. Rates of change: the question calculus answers · Practice problems · ProblemA stone dropped from a cliff falls d(t)=4.9t2 metres in t seconds. Find its average speed from t=3 to t=3+h as a formula in h, and then its speed at exactly t=3 s.Average speed = 29.4 + 4.9*h m/s; Speed at 3 s 29.4 m/s
    1. d(3+h)−d(3)=4.9(9+6h+h2)−4.9⋅9=4.9(6h+h2)
    2. d(3+h)−d(3)h=4.9(6+h)=29.4+4.9h m/s
    3. As h shrinks, 4.9h shrinks to nothing, so at exactly 3 s the stone falls at 29.4 m/s. Notice 29.4=9.8×3: its speed grows by 9.8 m/s every second.
  11. 1. Rates of change: the question calculus answers · Practice problems · ProblemA car drives at a steady 15 m/s for 20 s, then speeds up steadily from 15 m/s to 25 m/s over the next 10 s. How far does it go in these 30 s, and what is its average speed?Distance 500 m; Average speed 50/3 ≈ 16.6667 m/s
    1. The distance is the area under the speed graph. The first 20 s are a rectangle:
    2. 15×20=300 m
    3. The next 10 s are a rectangle 10 wide and 15 tall, with a triangle on top, 10 wide and 25−15=10 tall:
    4. 15×10+12×10×10=150+50=200 m
    5. In all 300+200=500 m. The average speed is the total distance over the total time:
    6. 500 m30 s=503≈16.7 m/s
  12. 1. Rates of change: the question calculus answers · Practice problems · ProblemA tank drains so that t minutes after the tap opens it holds V(t)=200−3t2 litres. What is the average rate of change of V from t=2 to t=5 minutes?-21 L/min
    1. V(2)=200−12=188,V(5)=200−75=125
    2. V(5)−V(2)5−2=125−1883=−633=−21 L/min
    3. The tank loses 21 litres a minute on average over those 3 minutes. The minus sign says the water is going down, like a secant line that falls.
  13. 2. Limits · Guessing a limit with a table · Code challengeWrite valuesNear(f, a, n): a vector of f(a + 0.1), f(a + 0.01), … , f(a+10−n), the values of f as the input closes in on a from above.One solution:
    Solution
    #include <cmath>#include <functional>#include <vector>
    std::vector<double> valuesNear(std::function<double(double)> f,                               double a, int n) {  std::vector<double> values;  for (int k = 1; k <= n; k++) {    values.push_back(f(a + std::pow(10, -k)));  }  return values;}
    The loop runs k from 1 to n and evaluates f at a + std::pow(10, -k): a+0.1, a+0.01, and so on. Starting at k = 0 is the usual slip: it adds f(a + 1), a point nowhere near a, and gives one value too many. C++ has no power operator, so 10−k is std::pow(10, -k); 10 ^ -k compiles, but ^ is bitwise XOR, a different number entirely.
  14. 2. Limits · Limits from the left and from the right · QuizFor this f, what is limx→1⁡f(x)?D. It does not existThe left-hand limit is 1 and the right-hand limit is 2. They disagree, so there is no single value f(x) approaches: the two-sided limit does not exist. f(1)=2 is true, but the value at the point never decides the limit. Averaging to 1.5 is not a thing limits do.
  15. 2. Limits · Three ways a limit can fail to exist · QuizWhat happens to 1x as x→0?C. It goes to ∞ from the right and −∞ from the left, so there is no limitFor small positive x, 1x is huge and positive; for small negative x it is huge and negative. The one-sided "limits" are ∞ and −∞, which disagree. Unlike 1x2, you can’t even write =∞ for the two-sided limit.
  16. 2. Limits · Limit laws let you split the work · QuizWhat is limx→3⁡x2+1x−1?A. 5The bottom at 3 is 3−1=2, not 0, so you may substitute: 9+12=5. Substitution only fails when it gives a 0 on the bottom; then you need the tricks on the next slide.
  17. 2. Limits · When substitution gives zero over zero · Fill the blanksFind limx→3⁡x2−9x−3 by factoring. Fill in the blanks.1: 3, 2: 6x2−9 is a difference of squares, (x−3)(x+3). Cancelling x−3 is allowed because a limit never looks at x=3 itself. What is left, x+3, can be substituted: 6.
  18. 2. Limits · When substitution gives zero over zero · ProblemFind limx→3⁡x2−x−6x2−9.5/6 ≈ 0.833333
    1. At x=3 the top is 9−3−6=0 and the bottom is 9−9=0. Factor both:
    2. x2−x−6x2−9=(x−3)(x+2)(x−3)(x+3)=x+2x+3(x≠3)
    3. Now the bottom isn’t 0 at 3, so substitute:
    4. limx→3⁡x+2x+3=56
  19. 2. Limits · The squeeze theorem · ProblemA function f satisfies 4x−9≤f(x)≤x2−4x+7 for every x. Find limx→4⁡f(x).7
    1. Both bounds are polynomials, so substitute:
    2. limx→4⁡(4x−9)=16−9=7,limx→4⁡(x2−4x+7)=16−16+7=7
    3. Both head for 7 and f is trapped between them, so by the squeeze theorem limx→4⁡f(x)=7. You never needed a formula for f.
  20. 2. Limits · The most important trig limit · QuizWhat is limx→0⁡sin⁡3xx?B. 3Write it as 3⋅sin⁡3x3x. As x→0, u=3x→0 too, so sin⁡uu→1, leaving 3. The tempting 1 comes from using the rule without matching the angle on top to the bottom.
  21. 2. Limits · Limits at infinity and horizontal asymptotes · Code challengeWrite limitAtInfinity(top, bottom). Each argument is a polynomial’s coefficients, highest power first ({3, 0, 1} is 3x2+1), and its first coefficient isn’t 0. Return the limit as x→∞: 0, the ratio of leading coefficients, or INFINITY / -INFINITY (from <cmath>).One solution:
    Solution
    #include <cmath>#include <vector>
    double limitAtInfinity(const std::vector<double>& top,                       const std::vector<double>& bottom) {  double ratio = top[0] / bottom[0];  if (top.size() < bottom.size()) return 0;  if (top.size() == bottom.size()) return ratio;  return ratio > 0 ? INFINITY : -INFINITY;}
    The degree is one less than the size of the vector. For huge x only the leading terms matter, so the fraction behaves like axmbxn. If m<n that shrinks to 0; if m=n it is ab; if m>n it grows without bound, with the sign of ab. Forgetting the sign is the usual mistake: −2x24x+1 heads to −∞. INFINITY is a real double value, not an error, so the tests can compare it exactly with EXPECT_EQ.
  22. 2. Limits · Practice problems · ProblemFind limx→9⁡x−3x−9.1/6 ≈ 0.166667
    1. x−3x−9⋅x+3x+3=x−9(x−9)(x+3)=1x+3
    2. limx→9⁡1x+3=13+3=16
    3. Factoring works too, once you see x−9=(x−3)(x+3) for x≥0.
  23. 2. Limits · Practice problems · ProblemFind limh→0⁡1h(12+h−12).-1/4 ≈ −0.25
    1. 12+h−12=2−(2+h)2(2+h)=−h2(2+h)
    2. Divide by h. It cancels, since a limit never looks at h=0 itself:
    3. 1h⋅−h2(2+h)=−12(2+h)→−12⋅2=−14
    4. This is the slope of y=1x at x=2, found as the limit of secant slopes: what module 4 calls a derivative.
  24. 2. Limits · Practice problems · ProblemWith x in radians, find (a) limx→0⁡sin⁡5x2x and (b) limx→0⁡sin⁡4xsin⁡3x.(a) 5/2 ≈ 2.5; (b) 4/3 ≈ 1.33333
    1. (a) Multiply and divide by 5, so the angle on top matches the bottom:
    2. sin⁡5x2x=52⋅sin⁡5x5x→52⋅1=52
    3. (b) Divide the top and the bottom by x, then match each angle:
    4. sin⁡4xsin⁡3x=4⋅sin⁡4x4x3⋅sin⁡3x3x→4⋅13⋅1=43
    5. The limit of a quotient is the quotient of the limits here, because the bottom’s limit, 3, isn’t 0.
  25. 2. Limits · More practice problems · ProblemLet f(x)=2x+1 for x<1 and f(x)=x2+4 for x≥1. Find the left-hand and right-hand limits at x=1. Then suppose the right piece were x2+c instead: which c would make limx→1⁡f(x) exist?limx→1−⁡f(x)= 3; limx→1+⁡f(x)= 5; c= 2
    1. From the left, f(x)=2x+1, a line, so substitute: limx→1−⁡f(x)=2+1=3.
    2. From the right, f(x)=x2+4: limx→1+⁡f(x)=1+4=5. They differ, so limx→1⁡f(x) does not exist: the graph jumps.
    3. With x2+c on the right, the right-hand limit is 1+c. It matches the left when 1+c=3, so c=2.
  26. 2. Limits · More practice problems · ProblemFind (a) limx→∞⁡4x3−x+22x3+5x2 and (b) limx→∞⁡9x2+12x+3.(a) 2; (b) 3/2 ≈ 1.5
    1. (a) Divide the top and the bottom by x3:
    2. 4x3−x+22x3+5x2=4−1x2+2x32+5x→42=2
    3. (b) Divide the top and the bottom by x. For x>0, x=x2, so it goes inside the root as x2:
    4. 9x2+12x+3=9+1x22+3x→92=32
    5. The highest powers decide: the top behaves like 9x2=3x and the bottom like 2x.
  27. 3. Continuity · The kinds of discontinuity · QuizWhat kind of discontinuity does g(x)=x2−4x−2 have at x=2?A. RemovableFor x≠2, g(x)=x+2, so the limit at 2 is 4. But g(2) is 00, undefined: condition 1 fails. Setting g(2)=4 fills the hole, which makes it removable. A 00 from substitution often means a hole, not a vertical asymptote.
  28. 3. Continuity · The kinds of discontinuity · Problemg(x)=x2+x−6x−2 is undefined at x=2. What value should g(2) be given to make g continuous there?g(2)= 5
    1. x2+x−6=(x−2)(x+3),so g(x)=x+3 for x≠2
    2. So limx→2⁡g(x)=2+3=5. Defining g(2)=5 makes the value equal the limit: the discontinuity was removable, and now it is removed.
  29. 3. Continuity · Most formulas are continuous wherever they make sense · QuizWhere is h(x)=x+1x2−4 not continuous? Choose all that apply.B. x=2; C. x=−2It is a fraction of polynomials, so it breaks only where the bottom is zero: x2−4=0 at x=2 and x=−2. At x=−1 the top is zero, which just makes h(−1)=0, a perfectly good value.
  30. 3. Continuity · Choosing a value to join two pieces · Fill the blanksMake f(x)=3x−c (for x<1) and f(x)=x2+1 (for x≥1) continuous.1: 2, 2: 1The right piece is 12+1=2 at the join. The left piece arrives at 3⋅1−c=3−c. Setting them equal gives c=1. Then the left-hand limit, the right-hand limit and f(1) are all 2.
  31. 3. Continuity · Choosing a value to join two pieces · ProblemFind k so that f(x)=x2−1 for x<3 and f(x)=kx−4 for x≥3 is continuous at x=3.k= 4
    1. The left piece arrives at 32−1=8. The right piece is at 3k−4.
    2. 3k−4=8⟹k=4
    3. Check: with k=4 the left-hand limit, the right-hand limit and f(3)=12−4 are all 8.
  32. 3. Continuity · The theorem needs continuity · QuizWhich of these does the intermediate value theorem guarantee has a root in the given interval? Choose all that apply.A. x3+x−1 on [0,1]; D. ex−3 on [1,2]x3+x−1 is −1 at 0 and 1 at 1, and is a continuous polynomial: a root is guaranteed. ex−3 is about −0.28 at 1 and 4.39 at 2, and is continuous: guaranteed. 1x−2 breaks at 2 and tan⁡x breaks at π2≈1.57, inside their intervals, so the theorem says nothing (and neither has a root there).
  33. 3. Continuity · Bisection: halve the interval until the root is trapped · Put in orderPut one step of the bisection method in order.1. Check that f(a) and f(b) have opposite signs 2. Compute the midpoint m=a+b2 3. Evaluate f(m) 4. If f(a) and f(m) have opposite signs, set b=m; otherwise set a=m 5. Stop if b−a is smaller than the tolerance, else repeatThe sign check comes first: without a sign change the IVT promises nothing. Then the midpoint and its value decide which half keeps the sign change. Comparing f(a) with f(m) (not f(m) with 0 alone) is what works for both rising and falling functions.
  34. 3. Continuity · Bisection as code · Code challengeWrite bisect(f, a, b, tol). f(a) and f(b) have opposite signs. Halve the interval, keeping the half where the sign changes, until b - a < tol, then return the midpoint.One solution:
    Solution
    #include <functional>
    double bisect(std::function<double(double)> f,              double a, double b, double tol) {  while (b - a >= tol) {    double m = (a + b) / 2;    if (f(m) * f(a) > 0) a = m;  // same sign: the root is right of m    else b = m;  }  return (a + b) / 2;}
    If f(m) has the same sign as f(a), the sign change must be in [m,b], so a moves up to m; otherwise b moves down. Two numbers have the same sign exactly when their product is positive, which is the test f(m) * f(a) > 0. Testing f(m) > 0 to decide only works when f rises; cos⁡x−x falls, and that version would walk away from the root. Each loop halves the width, so it ends after about log2⁡b−atol steps.
  35. 3. Continuity · Bisection as code · QuizStarting from an interval of width 1, about how many bisection steps make it narrower than 10−6?B. 20After n steps the width is 2−n. Since 210=1024≈103, 220≈106, so 20 steps do it. Bisection gains about one decimal digit every 3.3 steps: slow but certain. Newton’s method (module 8) is much faster, when it works.
  36. 3. Continuity · The extreme value theorem · QuizWhich function is guaranteed a maximum by the extreme value theorem?C. sin⁡x+x3 on [−5,5]sin⁡x+x3 is continuous everywhere and [−5,5] is closed: the theorem applies. x2 on the open (0,2) creeps toward 4 but never gets there. 1x grows without bound near 0. tan⁡x breaks at π2, inside [0,2].
  37. 3. Continuity · Practice problems · ProblemFind a and b so that this function is continuous everywhere: f(x)=2x+6 for x<−1, f(x)=ax+b for −1≤x≤2, and f(x)=x3−1 for x>2.a= 1; b= 5
    1. At x=−1: the left piece arrives at 2(−1)+6=4, and the middle piece is at −a+b.
    2. At x=2: the middle piece is at 2a+b, and the right piece arrives at 23−1=7.
    3. −a+b=42a+b=7
    4. Subtracting the first equation from the second gives 3a=3, so a=1 and b=4+a=5. The middle piece is x+5.
  38. 3. Continuity · Practice problems · Problemh(x)=x−1x2−4x+3 is discontinuous at two points. Find the one that is removable, the value that fills its hole, and the one where h blows up.Removable at x= 1; Filled by the value -1/2 ≈ −0.5; Infinite at x= 3
    1. h(x)=x−1(x−1)(x−3)=1x−3(x≠1,3)
    2. At x=1 the factor cancels and the limit is 11−3=−12: a hole, removed by setting h(1)=−12.
    3. At x=3 nothing cancels: the bottom goes to 0 while the top stays near 2, so h blows up. That one is infinite, and no value of h(3) can fix it.
  39. 3. Continuity · Practice problems · Problemf(x)=x3−x−1 has a root in [1,2]. Take three steps of the bisection method: which interval are you left with? And how many steps, starting from [1,2], make the interval narrower than 0.001?Left end 1.25; Right end 1.375; Steps for a width under 0.001 10
    1. m=1.5:f(1.5)=3.375−1.5−1=0.875>0,keep [1,1.5]m=1.25:f(1.25)≈−0.297<0,keep [1.25,1.5]m=1.375:f(1.375)≈0.225>0,keep [1.25,1.375]
    2. After three steps the root is trapped in [1.25,1.375]. (It is 1.3247….)
    3. The width after n steps is 2−n. Since 29=512 and 210=1024, 2−10<0.001<2−9: it takes 10 steps.
  40. 3. Continuity · Practice problems · ProblemLet f(x)=sin⁡3xx for x≠0, and f(0)=c. Which c makes f continuous at 0?c= 3
    1. limx→0⁡sin⁡3xx=limx→0⁡3⋅sin⁡3x3x=3⋅1=3
    2. So c=3. Any other value leaves a removable discontinuity at 0: the graph heads for height 3 from both sides.
  41. 4. The derivative · Working out a derivative from the definition · Find the bugThis works out the derivative of f(x)=3x2 at a. One line is wrong. Which?Line 2: = 3(a^2 + 2ah + h^2) - 3a^2(a+h)2 is a2+2ah+h2, not a2+h2: the middle term 2ah was lost. With it, the quotient is 6a+3h and the derivative is 6a. A derivative of 0 everywhere would mean 3x2 is flat, which the graph plainly isn’t: a good sanity check.
  42. 4. The derivative · Working out a derivative from the definition · ProblemUse the definition of the derivative to find f′(x) for f(x)=2x2−3x. Then find f′(2).f′(x)= 4*x - 3; f′(2)= 5
    1. f(x+h)−f(x)=2(x+h)2−3(x+h)−(2x2−3x)=2x2+4xh+2h2−3x−3h−2x2+3x=4xh+2h2−3h
    2. f(x+h)−f(x)h=4x+2h−3→4x−3as h→0
    3. So f′(x)=4x−3, and f′(2)=8−3=5.
  43. 4. The derivative · The equation of the tangent line · Fill the blanksFind the tangent line to f(x)=1x at a=2, using f′(a)=−1a2. Write decimals.1: 0.5, 2: -0.25, 3: 1f(2)=12 and f′(2)=−14. Expanding, 0.5−0.25x+0.5=1−0.25x. The slope is negative because 1x falls as x grows.
  44. 4. The derivative · The equation of the tangent line · ProblemFind the tangent line to f(x)=1x at x=12, using f′(a)=−1a2. Give it as y=mx+c.y= -4*x + 4
    1. f(12)=2,f′(12)=−1(1/2)2=−4
    2. y=2−4(x−12)=2−4x+2=−4x+4
    3. Check: at x=12 the line gives −2+4=2, the curve’s height.
  45. 4. The derivative · The derivative is a function too · Quizx3 always rises. What is its slope at x=0?A. 0f′(0)=3⋅02=0: the tangent at the origin is flat, the x-axis itself. A function can rise everywhere and still be momentarily flat at a point. So "slope 0" does not mean "turns round", a fact module 7 relies on.
  46. 4. The derivative · Ways to write the derivative · QuizV(t) is the water in a tank, in litres, t minutes after midnight. V′(10)=−4. What does that say?B. At 00:10 the tank is losing water at 4 litres per minuteA derivative is a rate: litres per minute, at that moment. Negative means the amount is falling. It says nothing directly about how much is in the tank (that is V(10)) or about the total change over the first 10 minutes (that is V(10)−V(0)).
  47. 4. The derivative · Differentiable means smooth, not only unbroken · QuizWhich statement is true?B. If f is differentiable at a, it is continuous at aDifferentiable is the stronger property. Since f(a+h)−f(a)=h⋅f(a+h)−f(a)h→0⋅f′(a)=0, f(a+h)→f(a): that is continuity. The converse fails at every corner, |x| being the classic case.
  48. 4. The derivative · Reading the derivative from a graph · QuizFrom the graph alone, where is f′(x)<0?B. Between x=−1 and x=1The curve climbs to a hilltop at x=−1, falls to a valley at x=1, then climbs again. Falling means a negative slope, so f′<0 on (−1,1), and f′=0 exactly at the hilltop and the valley. Confusing "f is negative" with "f is falling" is the usual slip: f is negative for x<−3 and for 0<x<3, which is a different set.
  49. 4. The derivative · Finite differences as code · Code challengeWrite forwardDiff(f, x, h) and centralDiff(f, x, h), the two estimates of f′(x) from the last slide.One solution:
    Solution
    #include <functional>
    using Fn = std::function<double(double)>;
    double forwardDiff(Fn f, double x, double h) {  return (f(x + h) - f(x)) / h;}
    double centralDiff(Fn f, double x, double h) {  return (f(x + h) - f(x - h)) / (2 * h);}
    The central difference divides by 2 * h because its two points are 2h apart; dividing by h doubles the answer, the most common slip. It is exact for a parabola: (x+h)2−(x−h)22h=4xh2h=2x, whatever h is, because the error terms of the two sides cancel. using Fn = … gives the long function type a short name. The tests wrap std::sin in a lambda because std::sin comes in several versions (for float, double…), and the compiler can’t tell which one to pass.
  50. 4. The derivative · Practice problems · ProblemUse the definition of the derivative to find f′(9) for f(x)=x. Then give the tangent line at x=9 as y=mx+c.f′(9)= 1/6 ≈ 0.166667; Tangent: y= x/6 + 3/2
    1. 9+h−3h⋅9+h+39+h+3=(9+h)−9h(9+h+3)=19+h+3
    2. f′(9)=limh→0⁡19+h+3=16
    3. The tangent goes through (9,3) with slope 16:
    4. y=3+16(x−9)=x6+32
  51. 4. The derivative · Practice problems · ProblemThe tangent to f(x)=x3 at x=1 crosses the x-axis at one point and meets the curve again at another. Find the x of each. (f′(x)=3x2.)Crosses the x-axis at x= 2/3 ≈ 0.666667; Meets the curve again at x= -2
    1. At x=1: f(1)=1 and f′(1)=3, so the tangent is y=1+3(x−1)=3x−2.
    2. It crosses the axis where 3x−2=0: x=23.
    3. It meets the curve where x3=3x−2:
    4. x3−3x+2=(x−1)2(x+2)=0
    5. x=1 is the point of tangency, a double root because the line touches there. The other meeting is at x=−2, the point (−2,−8).
  52. 4. The derivative · Practice problems · ProblemEstimate f′(1) for f(x)=x3 with h=0.1, first with the forward difference, then with the central difference. (The true value is 3.)Forward 3.31; Central 3.01
    1. forward: f(1.1)−f(1)0.1=1.331−10.1=3.31
    2. central: f(1.1)−f(0.9)0.2=1.331−0.7290.2=0.6020.2=3.01
    3. The forward error, 0.31, is close to the estimate h2|f′′(1)|=0.05⋅6=0.3. The central error, 0.01, is h26|f′′′|=0.016⋅6 exactly: about 30 times smaller for the same h.
  53. 4. The derivative · Practice problems · ProblemA stone thrown upward is y(t)=20t−5t2 metres high after t seconds. From the definition of the derivative, find its velocity v(t)=y′(t). How fast is it rising at t=1 s, and when does it stop rising?v(t)= 20 - 10*t m/s; At t=1 10 m/s; Stops rising at t= 2 s
    1. y(t+h)−y(t)=20(t+h)−5(t+h)2−20t+5t2=20h−10th−5h2
    2. y(t+h)−y(t)h=20−10t−5h→20−10t
    3. So v(t)=20−10t m/s, and v(1)=10 m/s.
    4. It rises while v>0 and stops rising when v(t)=0: 20−10t=0 at t=2 s. It is then at its highest, y(2)=40−20=20 m.
  54. 5. Rules for derivatives · The power rule · QuizWhat is ddx1x3?B. −3x−4Write it as x−3. The power rule brings down −3 and lowers the power by one, from −3 to −4: −3x−4=−3x4. Going from −3 to −2 is the usual slip: lowering a negative power makes it more negative.
  55. 5. Rules for derivatives · Sums and constant multiples · Fill the blanksDifferentiate f(x)=2x5−3x2+6x, term by term.1: 10, 2: -6, 3: 32⋅5=10, −3⋅2=−6, and 6⋅12=3. So f′(x)=10x4−6x+3x. A constant multiple never changes the rule, it only scales the answer.
  56. 5. Rules for derivatives · Sums and constant multiples · ProblemDifferentiate f(x)=x4−3x2+8x+4x−7. Then find f′(1).f′(x)= 4*x^3 - 6*x - 8/x^2 + 2/sqrt(x); f′(1)= -8
    1. f(x)=x4−3x2+8x−1+4x1/2−7
    2. f′(x)=4x3−6x−8x−2+2x−1/2=4x3−6x−8x2+2x
    3. The constant −7 has derivative 0. At x=1: f′(1)=4−6−8+2=−8.
  57. 5. Rules for derivatives · Differentiating a polynomial in code · Code challengeWrite derivePoly(c), the coefficients of the derivative (lowest power first; {} for a constant), and evalPoly(c, x), the polynomial’s value at x.One solution:
    Solution
    #include <vector>
    std::vector<double> derivePoly(const std::vector<double>& c) {  std::vector<double> d;  for (int k = 1; k < int(c.size()); k++) d.push_back(k * c[k]);  return d;}
    double evalPoly(const std::vector<double>& c, double x) {  double sum = 0;  for (int k = int(c.size()) - 1; k >= 0; k--) sum = sum * x + c[k];  return sum;}
    The term ckxk at index k becomes kckxk−1, so the loop starts at k = 1 (the constant has nothing to give) and each k * c[k] lands one index lower, at k - 1. Starting at k = 0 is the common off-by-one: it pushes a 0 for the constant and every coefficient sits one place too high. int(c.size()) turns the size, which is an unsigned std::size_t, into an int, so that evalPoly’s k can go below 0 to end its loop. evalPoly uses Horner’s rule, (((4)x−5)x+7)x−2, which needs no powers at all. Whole-number coefficients times whole numbers are exact in a double, which is why the first two tests may compare with EXPECT_EQ.
  58. 5. Rules for derivatives · The derivative of ln x · QuizWhat is ddx(5ex−3ln⁡x+2x)?A. 5ex−3x+2xln⁡2ex stays ex, ln⁡x becomes 1x, and 2x becomes 2xln⁡2. The second option applies the power rule to 2x, but the power rule is for xn, with x in the base. In 2x, x is the exponent: a different kind of function.
  59. 5. Rules for derivatives · The product rule · Find the bugThis differentiates x3sin⁡x. One line is wrong. Which?Line 3: (fg)' = f' g + f g' = 3x^2 sin x + x^3 cos xMultiplying the derivatives is the classic mistake. Test it on x⋅x=x2: f′g′=1⋅1=1, but the derivative of x2 is 2x. The product rule gives 1⋅x+x⋅1=2x, as it should.
  60. 5. Rules for derivatives · The quotient rule · QuizWhat is ddxxx2+1?B. 1−x2(x2+1)2With f=x and g=x2+1: 1⋅(x2+1)−x⋅2x(x2+1)2=1−x2(x2+1)2. Swapping the order on top gives the third option, the right size with the wrong sign. And f′g′=12x is the "derivative of top over derivative of bottom" mistake.
  61. 5. Rules for derivatives · The quotient rule · ProblemDifferentiate y=x2−1x2+1 and simplify. What is the slope at x=1?y′= 4*x/(x^2 + 1)^2; Slope at x=1 1
    1. y′=2x(x2+1)−(x2−1)2x(x2+1)2=2x3+2x−2x3+2x(x2+1)2=4x(x2+1)2
    2. At x=1: 422=1.
  62. 5. Rules for derivatives · Higher derivatives · QuizWhat is the 4th derivative of sin⁡x?D. sin⁡xsin⁡→cos⁡→−sin⁡→−cos⁡→sin⁡: the derivatives cycle with period 4. So the 4th, 8th, 100th derivatives are all sin⁡x. This cycle is what makes the Taylor series of sine so tidy (module 16).
  63. 5. Rules for derivatives · Position, velocity and acceleration · QuizAt what time is the ball highest, and how high is it?A. At t≈2.04 s, about 25.4 m upAt the top the velocity is 0: 20−9.8t=0 gives t=209.8≈2.04 s. Then s≈5+40.82−20.41≈25.4 m. At the top the acceleration is still −9.8 m/s², not 0: the ball is stopped for an instant, but its velocity is still changing.
  64. 5. Rules for derivatives · Practice problems · ProblemDifferentiate y=x2ln⁡x, and find the slope at x=e.y′= 2*x*ln(x) + x; Slope at x=e 3*e ≈ 8.15485
    1. y′=2x⋅ln⁡x+x2⋅1x=2xln⁡x+x
    2. y′(e)=2e⋅1+e=3e≈8.15
  65. 5. Rules for derivatives · Practice problems · ProblemFind the tangent line to y=xex at x=1, as y=mx+c.y= 2*e*x - e
    1. y′=1⋅ex+x⋅ex=(1+x)ex,y′(1)=2e,y(1)=e
    2. y=e+2e(x−1)=2ex−e
    3. Its slope is 2e≈5.44, and it crosses the y-axis at −e.
  66. 5. Rules for derivatives · Practice problems · ProblemLet f(x)=x3+sin⁡x. Find f′′(x) and f(4)(x).f′′(x)= 6*x - sin(x); f(4)(x)= sin(x)
    1. f′(x)=3x2+cos⁡x,f′′(x)=6x−sin⁡x,f′′′(x)=6−cos⁡x,f(4)(x)=sin⁡x
    2. The polynomial part runs out after four steps (x3→3x2→6x→6→0), just as the sine comes back round to itself.
  67. 5. Rules for derivatives · Practice problems · ProblemA particle moves along a line, at position s(t)=t3−6t2+9t metres after t seconds (t≥0). At which two times is it at rest? What is its acceleration at the second of them?First at rest at t= 1 s; Then again at t= 3 s; Acceleration then 6 m/s²
    1. v(t)=s′(t)=3t2−12t+9=3(t−1)(t−3)
    2. v=0 at t=1 s and at t=3 s: it stops, turns round, and stops again.
    3. a(t)=v′(t)=6t−12,a(3)=18−12=6 m/s2
    4. At t=3 s it is stopped but accelerating forwards, so it sets off forwards again. At t=1 s, a=−6 m/s²: that is where it turns back.
  68. 5. Rules for derivatives · More practice problems · ProblemDifferentiate y=exx and simplify. At which x is the tangent flat?y′= e^x*(x - 1)/x^2; Flat at x= 1
    1. y′=ex⋅x−ex⋅1x2=ex(x−1)x2
    2. The tangent is flat where y′=0. Since ex>0 and x2>0 (for x≠0), only x−1=0 will do: x=1, at the point (1,e).
  69. 5. Rules for derivatives · More practice problems · ProblemA culture of bacteria has N(t)=500⋅2t cells after t hours. How fast is it growing at t=3 hours?4000*ln(2) ≈ 2772.59 cells per hour
    1. N′(t)=500⋅2tln⁡2
    2. N′(3)=500⋅8ln⁡2=4000ln⁡2≈2773 cells per hour
    3. At that moment it has N(3)=4000 cells, and it is growing at ln⁡2≈0.693 times its size per hour: growth in proportion to size.
  70. 6. The chain rule and implicit differentiation · Functions inside functions · QuizIn ln⁡(cos⁡x), which is the inner function?B. cos⁡xTo compute ln⁡(cos⁡x) for a number x, you take cos⁡x first, then the logarithm of that. What you do first is the inner function: u=cos⁡x, and the outer one is ln⁡u. It is not a product: nothing is multiplied.
  71. 6. The chain rule and implicit differentiation · The chain rule in practice · Fill the blanksDifferentiate y=cos⁡(5x3). Write the inner derivative as a number times a power.1: 15x^2, 2: -sin uThe derivative of cos⁡u is −sin⁡u, kept at the inner function u=5x3, and the inner derivative is 15x2. So dydx=−15x2sin⁡(5x3).
  72. 6. The chain rule and implicit differentiation · The chain rule in practice · ProblemDifferentiate y=(2x2−3)4, and find the slope at x=1.y′= 16*x*(2*x^2 - 3)^3; Slope at x=1 -16
    1. y′=4(2x2−3)3⋅4x=16x(2x2−3)3
    2. At x=1: 16⋅1⋅(2−3)3=16⋅(−1)=−16.
  73. 6. The chain rule and implicit differentiation · The mistake everyone makes once · Find the bugThis differentiates y=esin⁡x+(x2+1)3. One line is wrong. Which?Line 2: d/dx (x^2 + 1)^3 = 3(x^2 + 1)^2 * 2xThe outer power rule gave 3(x2+1)2, but the inside x2+1 has derivative 2x, so the term is 6x(x2+1)2. Line 3 is wrong too, but only because it copied line 2: the first wrong step is the bug.
  74. 6. The chain rule and implicit differentiation · The chain rule as code · Code challengeWrite chainDerivative(fPrime, g, gPrime). It returns a function of x giving the derivative of f(g(x)), by the chain rule.One solution:
    Solution
    #include <functional>
    using Fn = std::function<double(double)>;
    Fn chainDerivative(Fn fPrime, Fn g, Fn gPrime) {  return [=](double x) { return fPrime(g(x)) * gPrime(x); };}
    The outer derivative is evaluated at the inner function, fPrime(g(x)), not at x: cos⁡(x2), not cos⁡x. Then it is multiplied by gPrime(x). Both halves are needed: the starter’s fPrime(x) gets each wrong. The last test checks the rule against the finite difference from module 4, which is how you test a hand-written derivative in practice. Without the = in [=], the lambda could not use fPrime, g or gPrime at all: the compiler stops with an error.
  75. 6. The chain rule and implicit differentiation · Implicit differentiation · ProblemThe point (2,1) is on the ellipse x2+4y2=8. Find the slope of the tangent there, and the tangent line as y=mx+c.dydx= -1/2 ≈ −0.5; Tangent: y= -x/2 + 2
    1. 2x+8ydydx=0⟹dydx=−x4y
    2. At (2,1): dydx=−24=−12.
    3. y=1−12(x−2)=−x2+2
  76. 6. The chain rule and implicit differentiation · Implicit differentiation, step by step · Put in orderPut the steps of implicit differentiation in order.1. Differentiate both sides with respect to x 2. Wherever y is differentiated, multiply by y′ (the chain rule) 3. Collect the terms with y′ on one side 4. Factor out y′ and divide to get y′ alone 5. Substitute the point’s x and yYou can only substitute the point at the end, once y′ is alone: substituting before differentiating would turn the curve into a single number, whose derivative is 0. The chain-rule factor y′ appears during differentiation, and gathering it up is plain algebra.
  77. 6. The chain rule and implicit differentiation · Derivatives of inverse functions · Quizf(x)=x3+x has an inverse, and f(1)=2. What is (f−1)′(2)?B. 14f′(x)=3x2+1. The point y=2 on the inverse matches x=1 on f, so (f−1)′(2)=1f′(1)=14. Evaluating f′ at 2 instead gives 113: the slope must be taken at the matching point x=1, not at y=2.
  78. 6. The chain rule and implicit differentiation · Logarithmic differentiation · QuizAt what x is the slope of y=xx equal to 0?C. x=1e≈0.368xx is never 0 for x>0, so the slope is 0 only when ln⁡x+1=0, that is ln⁡x=−1, x=e−1. That is the lowest point of xx, about 0.692. At x=1 the slope is 1⋅(0+1)=1.
  79. 6. The chain rule and implicit differentiation · Derivatives of the inverse trig functions · QuizWhat is ddxarctan⁡(2x)?B. 21+4x2Outer derivative 11+u2 at u=2x gives 11+4x2; times the inner derivative 2. The first option forgets the inner derivative, the third squares 2x wrongly: (2x)2=4x2.
  80. 6. The chain rule and implicit differentiation · Practice problems · ProblemDifferentiate (a) y=ecos⁡2x and (b) y=ln⁡(x2+9).(a) y′= -2*sin(2*x)*e^(cos(2*x)); (b) y′= 2*x/(x^2 + 9)
    1. (a) Peel the layers from the outside:
    2. ddxecos⁡2x=ecos⁡2x⋅(−sin⁡2x)⋅2=−2sin⁡(2x)ecos⁡2x
    3. (b) Outer ln⁡u, inner u=x2+9:
    4. ddxln⁡(x2+9)=1x2+9⋅2x=2xx2+9
  81. 6. The chain rule and implicit differentiation · Practice problems · ProblemDifferentiate y=x2e−3x and factor the result. At which x>0 is the tangent flat?y′= x*e^(-3*x)*(2 - 3*x); Flat at x= 2/3 ≈ 0.666667
    1. y′=2x⋅e−3x+x2⋅(−3e−3x)=xe−3x(2−3x)
    2. e−3x is never 0, so y′=0 only at x=0 and x=23. The one with x>0 is x=23.
  82. 6. The chain rule and implicit differentiation · Practice problems · Problemf(x)=x5+2x+1 has an inverse. Find (f−1)′(4) and (f−1)′(1).(f−1)′(4)= 1/7 ≈ 0.142857; (f−1)′(1)= 1/2 ≈ 0.5
    1. f′(x)=5x4+2.
    2. f(1)=1+2+1=4, so y=4 matches x=1:
    3. (f−1)′(4)=1f′(1)=15+2=17
    4. f(0)=1, so y=1 matches x=0:
    5. (f−1)′(1)=1f′(0)=12
  83. 6. The chain rule and implicit differentiation · Practice problems · ProblemUse logarithmic differentiation to differentiate y=x2x (for x>0). What is the slope at x=1?y′= 2*x^(2*x)*(ln(x) + 1); Slope at x=1 2
    1. ln⁡y=2xln⁡x
    2. y′y=2ln⁡x+2x⋅1x=2ln⁡x+2
    3. y′=x2x(2ln⁡x+2)=2x2x(ln⁡x+1)
    4. At x=1: 2⋅1⋅(0+1)=2.
  84. 6. The chain rule and implicit differentiation · More practice problems · ProblemThe point (1,2) is on the curve x2+xy+y2=7. Find dydx there, and the tangent line as y=mx+c.dydx= -4/5 ≈ −0.8; Tangent: y= -4*x/5 + 14/5
    1. 2x+y+xy′+2yy′=0
    2. y′(x+2y)=−(2x+y)⟹y′=−2x+yx+2y
    3. At (1,2): y′=−2+21+4=−45.
    4. y=2−45(x−1)=−45x+145
  85. 6. The chain rule and implicit differentiation · More practice problems · ProblemLet y=xarctan⁡x. Find the slope of its graph at x=1 and at x=3.At x=1 pi/4 + 1/2 ≈ 1.2854; At x=3 pi/3 + sqrt(3)/4 ≈ 1.48021
    1. y′=1⋅arctan⁡x+x⋅11+x2=arctan⁡x+x1+x2
    2. y′(1)=π4+12≈1.285
    3. y′(3)=π3+31+3=π3+34≈1.480
  86. 7. Graphs, extremes and optimisation · Critical points are where the slope is zero or undefined · QuizWhat are the critical points of g(x)=x4−2x2?C. x=−1, 0 and 1g′(x)=4x3−4x=4x(x−1)(x+1), which is 0 at x=−1,0,1. Dividing 4x3=4x by x loses the solution x=0: always factor instead of dividing. ±2 are where g itself is 0, not its slope.
  87. 7. Graphs, extremes and optimisation · The first derivative test · Quizh′(x)=(x−2)2(x+1). What does h have at x=2?C. Neither(x−2)2 is never negative, so near x=2 the sign of h′ is the sign of x+1: positive on both sides. No change of sign, so h just flattens out for a moment and keeps rising. At x=−1, h′ goes − to +: a local minimum there.
  88. 7. Graphs, extremes and optimisation · The first derivative test · ProblemFind the local maximum and the local minimum of f(x)=2x3−3x2−12x+5: where each one is, and its value.Local maximum at x= -1; Its value 12; Local minimum at x= 2; Its value -15
    1. f′(x)=6x2−6x−12=6(x2−x−2)=6(x−2)(x+1)
    2. The critical points are x=−1 and x=2. Test points: f′(−2)=24>0, f′(0)=−12<0, f′(3)=24>0.
    3. So f′ goes + to − at x=−1, a local maximum, and − to + at x=2, a local minimum:
    4. f(−1)=−2−3+12+5=12,f(2)=16−12−24+5=−15
  89. 7. Graphs, extremes and optimisation · The second derivative test · QuizFor f(x)=x4, f′(0)=0 and f′′(0)=0. What is at x=0?B. A local minimumThe second derivative test is silent when f′′(c)=0, but the first derivative test isn’t: f′(x)=4x3 goes − to + at 0, a minimum. f′′(c)=0 does not make an inflection point either: f′′(x)=12x2 is positive on both sides, so the concavity never changes.
  90. 7. Graphs, extremes and optimisation · The mean value theorem · QuizThe mean value theorem implies which of these?A. If f′(x)=0 everywhere on an interval, f is constant thereIf f took two different values at a and b, the theorem would give a c with f′(c)=f(b)−f(a)b−a≠0. So zero slope everywhere means constant: the reason all antiderivatives differ by a constant (module 10). f(a)=f(b) only gives a flat tangent somewhere (Rolle’s theorem), not a flat function.
  91. 7. Graphs, extremes and optimisation · The mean value theorem · ProblemFind the number c that the mean value theorem promises for f(x)=x on [1,9].c= 4
    1. f(9)−f(1)9−1=3−18=14
    2. 12c=14⟹c=2⟹c=4
    3. c=4 is inside (1,9), as the theorem promises. There the tangent is parallel to the secant from (1,1) to (9,3).
  92. 7. Graphs, extremes and optimisation · Sketching a curve from its derivatives · Put in orderPut the steps of sketching y=f(x) in a sensible order.1. Find the domain, and where the graph crosses the axes 2. Compute f′ and find the critical points 3. Use the sign of f′ to find where f rises and falls, and classify the extremes 4. Compute f′′ and find the concavity and inflection points 5. Check what happens as x→±∞ and near any asymptotes 6. Plot the key points and join them with the right shapeEach step uses the one before: you need f′ before you can read rising and falling, and the critical points before classifying them. Plotting comes last, when you know every feature it must show. In practice the behaviour at infinity can be checked at any time; it just has to be done before drawing.
  93. 7. Graphs, extremes and optimisation · The biggest value on a closed interval · QuizWhat is the maximum of the running example f on [0,2]?A. 5, at x=1The critical points inside [0,2]: only x=1, with f(1)=5. The ends: f(0)=1 and f(2)=8−24+18+1=3. The largest is 5. On [0,5] the right end won instead: always check the ends.
  94. 7. Graphs, extremes and optimisation · Optimisation: finding the best design · QuizFrom a 30 cm square of card, cut a square of side x from each corner and fold up an open box: V(x)=x(30−2x)2. Which x gives the biggest volume?A. x=5 cm, V=2000 cm³By the product and chain rules, V′(x)=(30−2x)2−4x(30−2x)=(30−2x)(30−6x), which is 0 at x=5 and x=15. On [0,15], V(0)=V(15)=0 and V(5)=5⋅202=2000 cm³. x=15 is a critical point too, but it is the empty box.
  95. 7. Graphs, extremes and optimisation · Finding a maximum by computer · Code challengeWrite findMax(f, a, b) for a unimodal f on [a, b]: repeat the ternary search step until b - a < 1e-9, then return the midpoint, the x of the peak.One solution:
    Solution
    #include <functional>
    double findMax(std::function<double(double)> f, double a, double b) {  while (b - a >= 1e-9) {    double m1 = a + (b - a) / 3;    double m2 = b - (b - a) / 3;    if (f(m1) < f(m2)) a = m1;    else b = m2;  }  return (a + b) / 2;}
    When f(m1)<f(m2), the function is still rising somewhere after m1, and a unimodal function never rises again after its peak, so the peak is right of m1: move a up. Moving b in that case is the usual slip, and it walks straight away from the peak. Each step keeps two thirds, so it needs about log1.5⁡b−a10−9 steps: roughly 60 for [0,50].
  96. 7. Graphs, extremes and optimisation · Practice problems · ProblemFind the inflection point of f(x)=x3−3x2−9x+2, and the slope of the curve there.x= 1; y= -9; Slope there -12
    1. f′(x)=3x2−6x−9,f′′(x)=6x−6
    2. f′′ is negative for x<1 and positive for x>1, so the concavity changes at x=1.
    3. f(1)=1−3−9+2=−9,f′(1)=3−6−9=−12
    4. The inflection point is (1,−9), where the curve falls most steeply, with slope −12.
  97. 7. Graphs, extremes and optimisation · Practice problems · ProblemFind the largest and the smallest values of f(x)=x3−12x+1 on [−3,5].Largest 66; Smallest -15
    1. f′(x)=3x2−12=3(x−2)(x+2)=0 at x=±2
    2. Both are inside [−3,5]. Evaluate there and at the ends:
    3. f(−3)=10,f(−2)=17,f(2)=−15,f(5)=66
    4. The largest value is 66, at the right end, and the smallest is −15, at x=2. The local maximum at x=−2 is not the largest value on this interval: the ends matter.
  98. 7. Graphs, extremes and optimisation · Practice problems · ProblemA farmer has 240 m of fence to make a rectangular field beside a straight river. The side along the river needs no fence. Which dimensions give the largest area, and what is that area?Each side meeting the river 60 m; Side along the river 120 m; Largest area 7200 m²
    1. With the two sides that meet the river x long, the fence left for the far side is 240−2x:
    2. A(x)=x(240−2x)=240x−2x2,0≤x≤120
    3. A′(x)=240−4x=0⟹x=60
    4. Compare with the ends: A(0)=A(120)=0, while A(60)=60⋅120=7200 m². The best field is 60 m deep and 120 m along the river: twice as long as deep, because only one long side needs fence.
  99. 7. Graphs, extremes and optimisation · Practice problems · ProblemWhich point of the curve y=x is closest to the point (3,0)? Give its x, and its distance from (3,0).x= 5/2 ≈ 2.5; Distance sqrt(11)/2 ≈ 1.65831
    1. D2=(x−3)2+(x−0)2=x2−6x+9+x=x2−5x+9
    2. ddx(x2−5x+9)=2x−5=0⟹x=52
    3. This is the smallest: 2x−5 goes − to + there, and it is the only critical point for x≥0 (at the end x=0, D2=9 is bigger).
    4. D2=254−252+9=114,D=112≈1.66
  100. 7. Graphs, extremes and optimisation · More practice problems · ProblemAn open-topped box with a square base must hold 32000 cm³. Which base side and height use the least cardboard, and how much cardboard is that?Base side 40 cm; Height 20 cm; Cardboard 4800 cm²
    1. h=32000x2,S(x)=x2+4x⋅32000x2=x2+128000x,x>0
    2. S′(x)=2x−128000x2=0⟹x3=64000⟹x=40
    3. S′<0 for x<40 and S′>0 for x>40: S falls all the way to x=40 and rises after it, so x=40 gives the least cardboard. (There are no ends to check here.)
    4. h=320001600=20,S=1600+3200=4800 cm2
  101. 7. Graphs, extremes and optimisation · More practice problems · ProblemA closed cylindrical can must hold 1000 cm³. Which radius and height use the least metal? (Its volume is πr2h, and its surface is two discs and a side, 2πr2+2πrh.)Radius cbrt(500/pi) ≈ 5.41926 cm; Height 2*cbrt(500/pi) ≈ 10.8385 cm
    1. h=1000πr2,S(r)=2πr2+2πr⋅1000πr2=2πr2+2000r
    2. S′(r)=4πr−2000r2=0⟹r3=500π⟹r=500/π3≈5.42 cm
    3. S′ goes − to + there, so it is the least.
    4. h=1000πr2=1000πr3⋅r=1000500⋅r=2r≈10.8 cm
    5. The best can is exactly as tall as it is wide: its height equals its diameter.
  102. 8. Linear approximation, related rates and Newton's method · Near a point, a curve is almost its tangent · ProblemLinearise f(x)=x3 at a=8, and use it to estimate 8.123.L(x)= 2 + (x - 8)/12; 8.123≈ 2.01
    1. f(8)=2,f′(8)=13⋅8−2/3=13⋅14=112
    2. L(x)=2+112(x−8)
    3. 8.123≈2+0.1212=2.01(true value 2.00997…)
  103. 8. Linear approximation, related rates and Newton's method · Approximations worth knowing by heart · QuizLinearise x at a=9 to estimate 9.06.B. 3.01f(9)=3 and f′(9)=12⋅3=16, so 9.06≈3+0.066=3.01 (truly 3.00998…). Adding the 0.06 straight on, as if the slope were 1, gives 3.06, which is far off.
  104. 8. Linear approximation, related rates and Newton's method · A linearisation as code · Code challengeWrite linearise(f, fPrime, a). It returns the function L(x)=f(a)+f′(a)(x−a).One solution:
    Solution
    #include <functional>
    using Fn = std::function<double(double)>;
    Fn linearise(Fn f, Fn fPrime, double a) {  double height = f(a);  double slope = fPrime(a);  return [=](double x) { return height + slope * (x - a); };}
    Working out f(a) and fPrime(a) once, outside the returned lambda, means every call to L costs one multiplication and an addition: that cheapness is the point of a linearisation. [=] copies height, slope and a into the lambda, so L keeps them. Writing slope * x instead of slope * (x - a) is the slip to avoid: it is a line with the right slope through the wrong point.
  105. 8. Linear approximation, related rates and Newton's method · Differentials: small changes in, small changes out · Fill the blanksA sphere has V=43πr3. Estimate the change in volume when r goes from 5 cm to 5.02 cm. Leave π as pi.1: 25, 2: 0.02, 3: 2dVdr=4πr2, which at r=5 is 100π; times dr=0.02 that is 2π≈6.28 cm³. The derivative of 43πr3 being 4πr2, the surface area, is no accident: a thin shell of thickness dr has volume about area times thickness.
  106. 8. Linear approximation, related rates and Newton's method · How big is the error? · QuizYou measure a cube’s side with a possible error of 1%. About how large can the error in its volume be, relatively?B. 3%V=s3, so dVV=3dss=3×1%=3%. Cubing triples the relative error; squaring would double it. Checking: 1.013=1.0303, just over 3% bigger.
  107. 8. Linear approximation, related rates and Newton's method · Related rates · ProblemA stone dropped in a pond makes a circular ripple whose radius grows at 3 cm/s. How fast is the area inside the ripple growing when the radius is 10 cm?60*pi ≈ 188.496 cm²/s
    1. A=πr2⟹dAdt=2πrdrdt
    2. dAdt=2π⋅10⋅3=60π≈188.5 cm2/s
    3. The radius grows steadily, but the area grows faster and faster: at r=20 cm it would be 120π cm²/s.
  108. 8. Linear approximation, related rates and Newton's method · The sliding ladder · Put in orderPut the steps for solving a related rates problem in order.1. Draw a picture and name the changing quantities 2. Write the given rate and the wanted rate as derivatives 3. Write an equation linking the quantities 4. Differentiate it with respect to time 5. Substitute the values at the moment asked about, and solveThe equation must be true at every moment before you differentiate it; only then do you plug in the moment’s numbers. Substituting x=3 before differentiating is the classic error: it turns a changing quantity into a constant.
  109. 8. Linear approximation, related rates and Newton's method · Newton’s method doubles the digits each step · Code challengeWrite newton(f, fPrime, x0, steps): start at x0, take steps Newton steps, and return the last x.One solution:
    Solution
    #include <functional>
    using Fn = std::function<double(double)>;
    double newton(Fn f, Fn fPrime, double x0, int steps) {  double x = x0;  for (int i = 0; i < steps; i++) {    x = x - f(x) / fPrime(x);  }  return x;}
    Each pass replaces x with where its tangent crosses zero. Adding f(x) / fPrime(x) instead of subtracting sends each guess the wrong way and the method runs off. Note how few steps the tests allow: 5 steps reach 12 digits, where bisection would need about 40.
  110. 8. Linear approximation, related rates and Newton's method · When Newton’s method fails · QuizWhat is a robust way to combine the two root finders you know?A. Use bisection to get close, then switch to Newton’s methodBisection always closes in, but slowly; Newton’s method is fast only once it is close. Bisecting to a small bracket and then taking Newton steps (falling back to bisection if a step leaves the bracket) is what safeguarded library root finders do. Brent’s method, a common one, works the same way, with fast steps that need no derivative.
  111. 8. Linear approximation, related rates and Newton's method · Practice problems · ProblemA cube’s side is measured as 10 cm, with a possible error of 0.05 cm. Use a differential to estimate the possible error in its volume, then give that as a percentage of the volume.Error in the volume 15 cm³; Relative error 1.5 %
    1. dV=3s2ds=3⋅100⋅0.05=15 cm3
    2. dVV=151000=0.015=1.5%
    3. As a check: the side’s relative error is 0.0510=0.5%, and cubing triples it to 1.5%.
  112. 8. Linear approximation, related rates and Newton's method · Practice problems · ProblemA 13 m ladder leans against a wall, and its foot is pulled away from the wall at 2 m/s. How fast is the top moving when the foot is 5 m from the wall? Give dydt, where y is the height of the top (it is negative, since the top falls).dydt= -5/6 ≈ −0.833333 m/s
    1. x2+y2=169⟹2xdxdt+2ydydt=0
    2. When x=5, y=169−25=12. Substitute, with dxdt=2:
    3. 2⋅5⋅2+2⋅12dydt=0⟹dydt=−2024=−56≈−0.833 m/s
  113. 8. Linear approximation, related rates and Newton's method · Practice problems · ProblemUse Newton’s method on f(x)=x3−2x−5, starting from x0=2. Find x1 and x2.x1= 2.1; x2≈ 2.1 - 0.061/11.23 ≈ 2.09457
    1. x1=2−f(2)f′(2)=2−8−4−512−2=2−−110=2.1
    2. f(2.1)=9.261−4.2−5=0.061,f′(2.1)=3⋅4.41−2=11.23
    3. x2=2.1−0.06111.23≈2.1−0.00543=2.09457
    4. The root is 2.0945515…, so x2 already has five correct digits. This is the very equation Newton used to demonstrate his method.
  114. 8. Linear approximation, related rates and Newton's method · Practice problems · ProblemWater pours at 2 m³/min into a tank shaped like a cone, point down, 4 m deep with a top radius of 2 m. How fast is the water level rising when the water is 3 m deep? (A cone’s volume is 13πr2h.)8/(9*pi) ≈ 0.282942 m/min
    1. By similar triangles r=h2, so the water’s volume is a function of its depth alone:
    2. V=13πr2h=13π(h2)2h=πh312
    3. dVdt=πh24dhdt
    4. Substitute dVdt=2 and h=3:
    5. 2=9π4dhdt⟹dhdt=89π≈0.283 m/min
  115. 8. Linear approximation, related rates and Newton's method · More practice problems · ProblemUse the linearisation of sin⁡x at x=π6 (that is, 30∘) to estimate sin⁡31∘. Remember to work in radians.sin⁡31∘≈ 1/2 + sqrt(3)*pi/360 ≈ 0.515115
    1. L(x)=sin⁡π6+cos⁡π6(x−π6)=12+32(x−π6)
    2. 31∘ is π6+π180 radians, so x−π6=π180≈0.01745:
    3. sin⁡31∘≈12+32⋅π180≈0.5+0.01511=0.51511
    4. The true value is 0.51504…. Stepping by 1 instead of π180, as if degrees were radians, gives 1.37: more than any sine can be.
  116. 8. Linear approximation, related rates and Newton's method · More practice problems · ProblemA ball’s radius is measured with a possible error of 2%. (a) About how large can the relative error in its volume V=43πr3 be? (b) To know the volume to within 3%, how precisely must the radius be measured?(a) 6 %; (b) 1 %
    1. dV=4πr2dr⟹dVV=4πr2dr43πr3=3drr
    2. (a) 3×2%=6%.
    3. (b) 3drr≤3% needs drr≤1%: the radius must be measured to within 1%.
  117. 9. Area and the definite integral · Sigma notation for long sums · QuizWhat is ∑i=14(2i−1)?C. 16The terms are 1,3,5,7 (for i=1,2,3,4), which add to 16. That is 42: the sum of the first n odd numbers is always n2. The tempting 7 is only the last term.
  118. 9. Area and the definite integral · Sigma notation for long sums · ProblemUse the sum formulas to find (a) ∑i=120(3i+2) and (b) ∑i=110(2i2−i).(a) 670; (b) 715
    1. ∑i=120(3i+2)=3⋅20⋅212+20⋅2=630+40=670
    2. ∑i=110(2i2−i)=2⋅10⋅11⋅216−10⋅112=770−55=715
  119. 9. Area and the definite integral · Riemann sums in general · Code challengeWrite riemann(f, a, b, n, rule), the left, right or midpoint Riemann sum of f on [a, b] with n strips. Rule is an enum class: a type whose only values are the ones it lists, Rule::left, Rule::right and Rule::midpoint.One solution:
    Solution
    #include <functional>
    enum class Rule { left, right, midpoint };
    double riemann(std::function<double(double)> f,               double a, double b, int n, Rule rule) {  double dx = (b - a) / n;  double shift = 0.5;  // the midpoint  if (rule == Rule::left) shift = 0;  if (rule == Rule::right) shift = 1;  double sum = 0;  for (int i = 0; i < n; i++) {    sum += f(a + (i + shift) * dx) * dx;  }  return sum;}
    Every rule visits strip i once; only the point where the height is taken moves: i * dx, (i + 1) * dx or (i + 0.5) * dx past a. A frequent slip is looping i from 0 to n inclusive for the right sum, which adds an extra strip beyond b. Multiplying by dx inside the loop or once at the end gives the same answer. Notice 1.0 / 3 in the tests: 1 / 3 divides two ints and is 0.
  120. 9. Area and the definite integral · Riemann sums in general · ProblemWork out the left, right and midpoint sums for f(x)=x2+1 on [0,2] with n=4 strips.L4= 3.75; R4= 5.75; M4= 4.625
    1. Δx=0.5. At the edges f is 1,1.25,2,3.25,5; at the midpoints it is 1.0625,1.5625,2.5625,4.0625.
    2. L4=0.5(1+1.25+2+3.25)=0.5⋅7.5=3.75
    3. R4=0.5(1.25+2+3.25+5)=0.5⋅11.5=5.75
    4. M4=0.5(1.0625+1.5625+2.5625+4.0625)=0.5⋅9.25=4.625
    5. The exact area is 143≈4.667 (module 10 shows how). The midpoint sum is off by only 0.04; the left and right sums by about 0.9 and 1.1.
  121. 9. Area and the definite integral · Area below the axis counts as negative · QuizWhat is ∫−11x3dx?A. 0x3 is an odd function: its graph on the left of 0 is its graph on the right turned upside down. The area below the axis on [−1,0] exactly cancels the area above it on [0,1]. The total shaded area is 12, but the integral counts the left part as negative.
  122. 9. Area and the definite integral · Properties of the integral · QuizGiven ∫02f(x)dx=5 and ∫05f(x)dx=12, what is ∫523f(x)dx?B. −21Splitting [0,5] at 2: ∫25f=12−5=7. Reversing the limits flips the sign, so ∫52f=−7, and the constant 3 comes out: −21. Missing the reversed limits gives 21.
  123. 9. Area and the definite integral · Displacement and distance from velocity · QuizOver the 8 seconds, the displacement is 16 m. What is the total distance travelled?C. 34 mForward, from 0 to 5 s: a triangle of base 5 and height 10, area 25 m. Backward, from 5 to 8 s: a triangle of base 3 and depth 6, area 9 m. Displacement is 25−9=16 m; distance is 25+9=34 m.
  124. 9. Area and the definite integral · Displacement and distance from velocity · Code challengeWrite displacement(v, a, b, n) and distance(v, a, b, n): midpoint Riemann sums with n strips of v and of its absolute value.One solution:
    Solution
    #include <cmath>#include <functional>
    using Fn = std::function<double(double)>;
    double midpoint(Fn f, double a, double b, int n) {  double dt = (b - a) / n;  double sum = 0;  for (int i = 0; i < n; i++) sum += f(a + (i + 0.5) * dt) * dt;  return sum;}
    double displacement(Fn v, double a, double b, int n) {  return midpoint(v, a, b, n);}
    double distance(Fn v, double a, double b, int n) {  return midpoint([=](double t) { return std::abs(v(t)); }, a, b, n);}
    Distance is the integral of speed, |v|, so the only difference is wrapping v in std::abs before summing: the lambda [=](double t) { return std::abs(v(t)); } is the speed, built from v. Taking std::abs of the final displacement instead gives 16, not 34: the cancellation has already happened by then.
  125. 9. Area and the definite integral · Practice problems · ProblemWrite the right sum Rn for ∫02x2dx as a formula in n. Then find the integral as its limit.Rn= 4*(n + 1)*(2*n + 1)/(3*n^2); ∫02x2dx= 8/3 ≈ 2.66667
    1. Rn=∑i=1n(2in)2⋅2n=8n3∑i=1ni2=8n3⋅n(n+1)(2n+1)6=4(n+1)(2n+1)3n2
    2. ∫02x2dx=limn→∞⁡4(2n2+3n+1)3n2=4⋅23=83
    3. That is 8 times the 13 under x2 on [0,1]: the region is twice as wide, and at matching points four times as tall.
  126. 9. Area and the definite integral · Practice problems · ProblemFind these integrals from areas you know: (a) ∫−339−x2dx and (b) ∫04(2x−3)dx.(a) 9*pi/2 ≈ 14.1372; (b) 4
    1. (a) The region is half a disc of radius 3:
    2. ∫−339−x2dx=12π⋅32=9π2≈14.14
    3. (b) Below the axis, from 0 to 1.5, is a triangle with base 1.5 and depth 3. Above it, from 1.5 to 4, is one with base 2.5 and height 5:
    4. ∫04(2x−3)dx=12(2.5)(5)−12(1.5)(3)=6.25−2.25=4
  127. 9. Area and the definite integral · Practice problems · ProblemYou know ∫14f(x)dx=6, ∫46f(x)dx=3 and ∫14g(x)dx=−2. Find (a) ∫16f(x)dx, (b) ∫14(2f(x)−3g(x)+1)dx and (c) ∫64f(x)dx.(a) 9; (b) 21; (c) -3
    1. (a)∫16f=∫14f+∫46f=6+3=9
    2. (b)2∫14f−3∫14g+∫141dx=2(6)−3(−2)+3=12+6+3=21
    3. (c)∫64f=−∫46f=−3
  128. 9. Area and the definite integral · Practice problems · ProblemA lift moves with velocity v(t)=4−t m/s for 0≤t≤10 s, with up counted as positive. Find its displacement, and the total distance it travels.Displacement -10 m; Distance 26 m
    1. Up, from 0 to 4 s: a triangle with base 4 and height 4, area 8 m. Down, from 4 to 10 s: a triangle with base 6 and depth 6, area 18 m.
    2. displacement=8−18=−10 m,distance=8+18=26 m
    3. It ends 10 m below where it started, having travelled 26 m.
  129. 9. Area and the definite integral · More practice problems · ProblemThe area under sin⁡x from 0 to π is exactly 2. Work out the midpoint sums M2 and M4 for it.M2= pi*sqrt(2)/2 ≈ 2.22144; M4= pi/2*(sin(pi/8) + sin(3*pi/8)) ≈ 2.05234
    1. M2=π2(sin⁡π4+sin⁡3π4)=π2⋅2=π22≈2.2214
    2. M4=π4(sin⁡π8+sin⁡3π8+sin⁡5π8+sin⁡7π8)=π2(sin⁡π8+sin⁡3π8)≈2.0523
    3. The errors are about 0.221 and 0.052: doubling n divided the error by about 4, as it should for a midpoint sum.
  130. 9. Area and the definite integral · More practice problems · ProblemFind ∫−22(x3+5)dx without any sums.20
    1. ∫−22(x3+5)dx=∫−22x3dx+∫−225dx=0+5⋅4=20
    2. x3 is odd, so its area below the axis on [−2,0] cancels its area above on [0,2]. The constant 5 is a rectangle 4 wide and 5 tall.
  131. 10. The fundamental theorem of calculus · Part 1: the area’s rate of growth is the height · QuizLet g(x)=∫2x(t3+1)dt. What are g(2) and g′(2)?A. g(2)=0, g′(2)=9g(2)=∫22=0: no width, no area. By part 1, g′(x)=x3+1, so g′(2)=9. No integral needs computing. Differentiating t3+1 to get 12 is the slip: part 1 gives back the integrand itself, not its derivative.
  132. 10. The fundamental theorem of calculus · Part 1 with the chain rule · Fill the blanksDifferentiate H(x)=∫1x3ln⁡tdt (for x>0). Use ln and ^.1: 3x^2, 2: x^3, 3: 9x^2 ln xPut the upper limit into the integrand, ln⁡(x3), and multiply by its derivative 3x2. Since ln⁡(x3)=3ln⁡x, the result is 9x2ln⁡x. Forgetting the 3x2 is the same mistake as forgetting the inner derivative in any chain rule.
  133. 10. The fundamental theorem of calculus · Part 1 with the chain rule · ProblemLet G(x)=∫1x211+t2dt. Find G′(x), and G′(1).G′(x)= 2*x/(1 + x^4); G′(1)= 1
    1. The upper limit is g(x)=x2, with g′(x)=2x:
    2. G′(x)=11+(x2)2⋅2x=2x1+x4
    3. G′(1)=22=1. The lower limit, 1, makes no difference to the derivative.
  134. 10. The fundamental theorem of calculus · Antiderivatives, and the constant C · QuizWhich function is an antiderivative of 2xcos⁡(x2)?A. sin⁡(x2)+5Check by differentiating: ddx(sin⁡(x2)+5)=cos⁡(x2)⋅2x by the chain rule, and the 5 vanishes. Checking an antiderivative by differentiating it is always the quickest test. Undoing the chain rule in general is substitution (module 11).
  135. 10. The fundamental theorem of calculus · A table of antiderivatives · QuizWhat is ∫sin⁡xdx?B. −cos⁡x+CYou need a function whose derivative is sin⁡x. Since (cos⁡x)′=−sin⁡x, (−cos⁡x)′=sin⁡x. The minus sign is the one people drop: derivative and antiderivative of the trig functions go in opposite directions round the cycle.
  136. 10. The fundamental theorem of calculus · Working with indefinite integrals · Fill the blanksFind ∫(6x5+2x−ex)dx. Write powers with ^.1: x^6, 2: -e^x, 3: CRaise the power by one and divide by the new power: 6x66=x6. 1x is the one power the rule can’t do, and gives ln⁡|x|. ex is its own antiderivative. The +C says that the answer is a family, not a single function.
  137. 10. The fundamental theorem of calculus · Evaluating a definite integral without slips · Find the bugThis computes ∫02(3x2−2x)dx. One line is wrong. Which?Line 2: F(x) = x^3 - x^2Line 2 differentiates instead of integrating. The antiderivative raises each power: x3−x2. Then F(2)−F(0)=(8−4)−0=4. A quick check catches it: differentiating F must give back f, and (6x−2)′=6.
  138. 10. The fundamental theorem of calculus · Evaluating a definite integral without slips · QuizWhat is ∫14xdx?A. 143x=x1/2, with antiderivative x3/23/2=23x3/2. Then 23(43/2−13/2)=23(8−1)=143. 163 forgets to subtract the value at the lower limit.
  139. 10. The fundamental theorem of calculus · Evaluating a definite integral without slips · ProblemEvaluate ∫13(x2−2x+3)dx.20/3 ≈ 6.66667
    1. F(x)=x33−x2+3x
    2. F(3)−F(1)=(9−9+9)−(13−1+3)=9−73=203
    3. Bracket the whole of F(1) before subtracting it: every sign inside flips.
  140. 10. The fundamental theorem of calculus · The net change theorem · QuizA town grows at P′(t)=300+60t people per year. How many people does it gain from t=0 to t=10 years?C. 6000∫010(300+60t)dt=[300t+30t2]010=3000+3000=6000. 900 is the growth rate at t=10, people per year, not a number of people. 3000 forgets that the rate keeps rising.
  141. 10. The fundamental theorem of calculus · Both parts of the theorem, in code · Code challengeWrite accumulate(f, a, n). It returns the function A(x)=∫axf(t)dt, worked out with a midpoint sum of n strips each time it is called. The last test checks part 1: the slope of A is f.One solution:
    Solution
    #include <functional>
    using Fn = std::function<double(double)>;
    Fn accumulate(Fn f, double a, int n) {  return [=](double x) {    double dt = (x - a) / n;    double sum = 0;    for (int i = 0; i < n; i++) sum += f(a + (i + 0.5) * dt) * dt;    return sum;  };}
    Each call integrates from a to its own x, so the strip width (x - a) / n is worked out inside the returned lambda, not once outside. The last test is the fundamental theorem made numerical: a central difference of the accumulated area gives back the integrand.
  142. 10. The fundamental theorem of calculus · Both parts of the theorem, in code · Code challengePolynomials are vectors of coefficients, lowest power first, as in module 5. Write integratePoly(c), the antiderivative with constant 0, and definite(c, a, b), the integral from a to b by part 2.One solution:
    Solution
    #include <cmath>#include <vector>
    std::vector<double> integratePoly(const std::vector<double>& c) {  std::vector<double> F = {0};  for (int k = 0; k < int(c.size()); k++) F.push_back(c[k] / (k + 1));  return F;}
    double value(const std::vector<double>& c, double x) {  double sum = 0;  for (int k = 0; k < int(c.size()); k++) sum += c[k] * std::pow(x, k);  return sum;}
    double definite(const std::vector<double>& c, double a, double b) {  std::vector<double> F = integratePoly(c);  return value(F, b) - value(F, a);}
    The term cxk becomes ck+1xk+1, so every coefficient moves up one place and is divided by its new power; the constant 0 goes in front. In definite the constant would cancel anyway, which is why part 2 lets you pick any antiderivative. Dividing by k instead of k + 1 divides the constant term by zero, which for a double gives inf rather than an error. Here c[k] / (k + 1) is a double over an int, so it keeps its fraction; two ints, as in 1 / 3, would not.
  143. 10. The fundamental theorem of calculus · Practice problems · ProblemEvaluate (a) ∫0π/2(cos⁡x+2sin⁡x)dx and (b) ∫01(ex+11+x2)dx.(a) 3; (b) e - 1 + pi/4 ≈ 2.50368
    1. (a)[sin⁡x−2cos⁡x]0π/2=(1−0)−(0−2)=3
    2. (b)[ex+arctan⁡x]01=(e+π4)−(1+0)=e−1+π4≈2.504
  144. 10. The fundamental theorem of calculus · Practice problems · ProblemFind the function f with f′(x)=6x2−4x+1 and f(1)=5. What is f(2)?f(x)= 2*x^3 - 2*x^2 + x + 4; f(2)= 14
    1. f(x)=2x3−2x2+x+C
    2. f(1)=2−2+1+C=1+C=5⟹C=4
    3. f(x)=2x3−2x2+x+4,f(2)=16−8+2+4=14
  145. 10. The fundamental theorem of calculus · Practice problems · ProblemWater flows out of a tank at r(t)=12+6t litres per minute, t minutes after a valve opens. The tank holds 500 litres when it opens. How much flows out in the first 9 minutes, and how much is left then?Flowed out 216 L; Left 284 L
    1. ∫09(12+6t1/2)dt=[12t+4t3/2]09=108+4⋅27=216 L
    2. Here 6⋅t3/23/2=4t3/2, and 93/2=(9)3=27. The tank had 500 L, so 500−216=284 L are left.
  146. 10. The fundamental theorem of calculus · Practice problems · ProblemA particle moves with velocity v(t)=t2−4t+3 m/s. Over 0≤t≤4 s, find its displacement and the total distance it travels.Displacement 4/3 ≈ 1.33333 m; Distance 4 m
    1. An antiderivative of v is F(t)=t33−2t2+3t, with F(0)=0, F(1)=43, F(3)=0 and F(4)=43.
    2. displacement=F(4)−F(0)=43 m
    3. v>0 on (0,1), v<0 on (1,3) and v>0 on (3,4), so the middle piece counts with its sign flipped:
    4. distance=(F(1)−F(0))−(F(3)−F(1))+(F(4)−F(3))=43+43+43=4 m
  147. 10. The fundamental theorem of calculus · More practice problems · ProblemFind b in each: (a) ∫0bx2dx=9, and (b) ∫1b1xdx=2, with b>1.(a) b= 3; (b) b= e^2 ≈ 7.38906
    1. (a)[x33]0b=b33=9⟹b3=27⟹b=3
    2. (b)[ln⁡x]1b=ln⁡b=2⟹b=e2≈7.389
  148. 10. The fundamental theorem of calculus · More practice problems · ProblemLet F(x)=∫0x(t2−4)dt. Where does F have a local minimum, and what is its value there?x= 2; F there -16/3 ≈ −5.33333
    1. F′(x)=x2−4=(x−2)(x+2)
    2. F′ goes − to + at x=2 (and + to − at x=−2, a local maximum). So the local minimum is at x=2.
    3. F(2)=∫02(t2−4)dt=[t33−4t]02=83−8=−163
    4. The accumulated area keeps falling while the integrand is negative, and starts to grow once t2−4 turns positive at t=2.
  149. 11. Substitution and integration by parts · Every derivative rule runs backwards · QuizWhich function is an antiderivative of 2xcos⁡(x2)?A. sin⁡(x2)Differentiate each one. By the chain rule, (sin⁡(x2))′=cos⁡(x2)⋅2x, which is exactly the integrand. The others give a sign or a factor of 2 wrong, or extra terms from the product rule. Recognising the inner function x2 and its derivative 2x sitting next to it is the whole idea of the next slide.
  150. 11. Substitution and integration by parts · Substitution undoes the chain rule · QuizWhat is ∫sin⁡(x)cos3⁡(x)dx?A. −14cos4⁡(x)+CTake u=cos⁡x, so du=−sin⁡xdx and sin⁡xdx=−du. The integral becomes −∫u3du=−u44+C. The usual slip is the minus sign: the derivative of cos⁡ is −sin⁡. Differentiate −14cos4⁡x to check: −14⋅4cos3⁡x⋅(−sin⁡x)=sin⁡xcos3⁡x.
  151. 11. Substitution and integration by parts · Substitution undoes the chain rule · ProblemFind the antiderivative F of f(x)=x2(x3+1)4 with F(0)=0. Then use it to work out ∫01x2(x3+1)4dx.F(x)= ((x^3 + 1)^5 - 1)/15; ∫01f(x)dx= 31/15 ≈ 2.06667
    1. Take u=x3+1, so du=3x2dx and x2dx=13du:
    2. ∫x2(x3+1)4dx=13∫u4du=u515+C=(x3+1)515+C
    3. At x=0 this is 115+C, so F(0)=0 needs C=−115: F(x)=(x3+1)5−115.
    4. ∫01x2(x3+1)4dx=F(1)−F(0)=25−115=3115≈2.07
  152. 11. Substitution and integration by parts · With a definite integral, change the limits too · QuizSomeone substitutes u=x2+1 in ∫012x(x2+1)3dx but keeps the limits 0 and 1. What do they get?A. 14∫01u3du=14, the area under u3 over the wrong interval. The answer is 154: as x runs from 0 to 1, u=x2+1 runs from 1 to 2. Either change the limits, or put x back before using the original ones; mixing the two is the mistake.
  153. 11. Substitution and integration by parts · With a definite integral, change the limits too · ProblemEvaluate ∫0π/2sin⁡xecos⁡xdx exactly, changing the limits as you substitute.e - 1 ≈ 1.71828
    1. Take u=cos⁡x, so du=−sin⁡xdx. When x=0, u=1; when x=π2, u=0.
    2. ∫0π/2sin⁡xecos⁡xdx=∫10−eudu=∫01eudu=e−1≈1.718
    3. Swapping the limits changes the sign, which cancels the minus from du.
  154. 11. Substitution and integration by parts · Integration by parts undoes the product rule · QuizWhat is ∫xcos⁡xdx?A. xsin⁡x+cos⁡x+CWith u=x and dv=cos⁡xdx: du=dx, v=sin⁡x, so the integral is xsin⁡x−∫sin⁡xdx=xsin⁡x+cos⁡x+C. The tempting −cos⁡x forgets that ∫sin⁡xdx=−cos⁡x, and the minus in front makes it plus. Differentiate to check: sin⁡x+xcos⁡x−sin⁡x=xcos⁡x.
  155. 11. Substitution and integration by parts · Integration by parts undoes the product rule · ProblemEvaluate ∫0πxsin⁡xdx by parts.pi ≈ 3.14159
    1. Take u=x and dv=sin⁡xdx, so du=dx and v=−cos⁡x:
    2. ∫0πxsin⁡xdx=[−xcos⁡x]0π+∫0πcos⁡xdx=(−πcos⁡π−0)+[sin⁡x]0π=π+0=π
  156. 11. Substitution and integration by parts · Trig identities turn powers into things you can integrate · QuizWhich are antiderivatives of sin⁡xcos⁡x? Choose every one.A. 12sin2⁡x+C; B. −12cos2⁡x+C; C. −14cos⁡2x+CThe first three are all right: substitute u=sin⁡x, or u=cos⁡x, or use sin⁡xcos⁡x=12sin⁡2x. They look different but differ only by constants (12sin2⁡x and −12cos2⁡x differ by 12), which the +C absorbs. Two correct answers in different forms are normal in integration; differentiate both to be sure. The last one fails: its derivative is sin2⁡x−cos2⁡x.
  157. 11. Substitution and integration by parts · Partial fractions split a fraction into easy pieces · QuizSplit 1x2−1=Ax−1+Bx+1. What are A and B?A. A=12, B=−12x2−1=(x−1)(x+1). Cover (x−1) and put x=1: A=11+1=12. Cover (x+1) and put x=−1: B=1−1−1=−12. So ∫dxx2−1=12ln⁡|x−1x+1|+C. Forgetting that B comes out negative is the usual slip.
  158. 11. Substitution and integration by parts · Try it: cover-up and checking by differentiating · Code challengeWrite coverUp(p, q, a, b): it returns the pair {A, B} with px+q(x−a)(x−b)=Ax−a+Bx−b, for a≠b. A std::pair<double, double> holds two numbers; auto [A, B] = coverUp(…) unpacks them.One solution:
    Solution
    #include <utility>
    std::pair<double, double> coverUp(double p, double q,                                  double a, double b) {  // Cover (x - a), put x = a in what is left; then the same for b.  double A = (p * a + q) / (a - b);  double B = (p * b + q) / (b - a);  return {A, B};}
    Multiply both sides by (x−a): you get px+qx−b=A+Bx−ax−b. At x=a the B term vanishes, leaving A=pa+qa−b; B is the same with the roles swapped. The common mistake is the sign of the denominator: for B it is b−a, not a−b. The last test writes 5.0 / (3 * -2): with 5 / it would divide two ints and expect 0.
  159. 11. Substitution and integration by parts · Try it: cover-up and checking by differentiating · Code challengeWrite isAntiderivative(F, f, xs): it returns true when, at every point in xs, the central difference (F(x + h) - F(x - h)) / (2 * h) with h = 1e-5 is within 1e-6 of f(x).One solution:
    Solution
    #include <cmath>#include <functional>#include <vector>
    using Fn = std::function<double(double)>;
    bool isAntiderivative(Fn F, Fn f, const std::vector<double>& xs) {  double h = 1e-5;  for (double x : xs) {    double slope = (F(x + h) - F(x - h)) / (2 * h);    if (std::abs(slope - f(x)) >= 1e-6) return false;  }  return true;}
    The central difference F(x+h)−F(x−h)2h has an error proportional to h2, about 10−10 here, far below the tolerance, while a wrong antiderivative is off by a visible amount. Returning false at the first failing point stops the loop there; only after every point has passed is the answer true. A one-sided difference F(x+h)−F(x)h would be off by about h, 10−5, and reject correct answers.
  160. 11. Substitution and integration by parts · A strategy for choosing a method · QuizWhich method suits ∫x2ln⁡xdx, and with which u?A. By parts, with u=ln⁡x and dv=x2dxLogarithms come first in LIATE: differentiating ln⁡x gives 1x, which cancels against v=x33, leaving ∫x23dx. The result is x33ln⁡x−x39+C. The other way round you would need to integrate ln⁡x and get a messier integral. Substitution fails because 1x, the derivative of ln⁡x, is not a factor.
  161. 11. Substitution and integration by parts · A strategy for choosing a method · Put in orderPut the steps of integrating ∫xe2xdx by parts in order.1. Choose u=x and dv=e2xdx 2. Work out du=dx and v=12e2x 3. Write uv−∫vdu=x2e2x−∫12e2xdx 4. Integrate what is left: x2e2x−14e2x+C 5. Differentiate the answer to check it gives xe2xChoose, then compute du and v (integrating e2x needs the factor 12), then apply the formula, then finish the simpler integral, then check. Checking last is not optional: differentiating x2e2x−14e2x gives 12e2x+xe2x−12e2x=xe2x.
  162. 11. Substitution and integration by parts · Some integrals have no formula at all · QuizWhich of these has an elementary antiderivative?A. xe−x2With u=−x2, du=−2xdx, so ∫xe−x2dx=−12e−x2+C. The factor x is exactly what substitution needs. Without it, e−x2 has no elementary antiderivative; you integrate it with a series (a later module) or numerically (the module after next).
  163. 11. Substitution and integration by parts · Practice problems · ProblemEvaluate ∫354x+1x2−x−2dx: factor the bottom, split the fraction into Ax−2+Bx+1, and integrate.A= 3; B= 1; The integral ln(81/2) ≈ 3.7013
    1. x2−x−2=(x−2)(x+1). Cover-up: A=4⋅2+12+1=3 and B=4⋅(−1)+1−1−2=1.
    2. ∫35(3x−2+1x+1)dx=[3ln⁡|x−2|+ln⁡|x+1|]35=(3ln⁡3+ln⁡6)−(3ln⁡1+ln⁡4)=ln⁡27+ln⁡32=ln⁡812≈3.70
  164. 11. Substitution and integration by parts · Practice problems · ProblemEvaluate ∫0π/2sin3⁡xdx.2/3 ≈ 0.666667
    1. ∫0π/2sin3⁡xdx=∫0π/2(1−cos2⁡x)sin⁡xdx
    2. With u=cos⁡x, du=−sin⁡xdx, and u runs from 1 down to 0:
    3. ∫10−(1−u2)du=∫01(1−u2)du=1−13=23
  165. 11. Substitution and integration by parts · Practice problems · ProblemFind the function F with F′(x)=xln⁡x for x>0 and F(1)=0.F(x)= x^2*ln(x)/2 - x^2/4 + 1/4
    1. Take u=ln⁡x and dv=xdx, so du=dxx and v=x22:
    2. ∫xln⁡xdx=x22ln⁡x−∫x22⋅1xdx=x22ln⁡x−x24+C
    3. At x=1: 0−14+C=0, so C=14 and F(x)=x22ln⁡x−x24+14.
    4. Check by differentiating: xln⁡x+x2−x2=xln⁡x.
  166. 11. Substitution and integration by parts · Practice problems · ProblemWater flows into a tank at r(t)=te−t/2 litres per minute, t minutes after a tap is opened. How much water flows in during the first 4 minutes? Give it exactly or to 3 significant figures.Water 4 - 12*e^(-2) ≈ 2.37598 L
    1. The water is the integral of the rate. By parts, u=t and dv=e−t/2dt, so du=dt and v=−2e−t/2:
    2. ∫te−t/2dt=−2te−t/2+∫2e−t/2dt=−2te−t/2−4e−t/2+C∫04te−t/2dt=(−8e−2−4e−2)−(0−4)=4−12e−2≈2.38L
  167. 12. Areas, volumes and averages · The area between two curves is top minus bottom · QuizWhat is the area between y=4−x2 and the x-axis?A. 323The parabola meets the axis where 4−x2=0, at x=±2. Then ∫−22(4−x2)dx=[4x−x33]−22=(8−83)−(−8+83)=323. 163 is only the right half; finding the limits first, from where the curves meet, is the step people skip.
  168. 12. Areas, volumes and averages · The area between two curves is top minus bottom · ProblemFind the area of the region between the parabola y=4−x2 and the line y=x+2. First find where they meet.Left crossing, x= -2; Right crossing, x= 1; Area 9/2 ≈ 4.5
    1. 4−x2=x+2 gives x2+x−2=0, so (x+2)(x−1)=0: x=−2 and x=1. At x=0 the parabola is at 4 and the line at 2, so the parabola is on top.
    2. A=∫−21((4−x2)−(x+2))dx=∫−21(2−x−x2)dx=[2x−x22−x33]−21=76−(−103)=92
  169. 12. Areas, volumes and averages · When the curves cross, split the integral · QuizWhat is ∫0π/2(cos⁡x−sin⁡x)dx, without splitting?A. 0[sin⁡x+cos⁡x]0π/2=(1+0)−(0+1)=0. The piece where cos⁡ is on top and the piece where sin⁡ is on top cancel exactly. That is a correct net integral but the wrong area: the region plainly has area about 0.828.
  170. 12. Areas, volumes and averages · Volumes by slicing: disks · QuizWhat is the volume of that bowl, y=x for 0≤x≤4, spun around the x-axis?A. 8πV=π∫04(x)2dx=π∫04xdx=π⋅8=8π≈25.1. The tempting 16π3 comes from integrating x itself instead of its square: the area of a disk is πr2, so the radius must be squared.
  171. 12. Areas, volumes and averages · Volumes by slicing: disks · ProblemThe arch of y=sin⁡x, for 0≤x≤π, spins around the x-axis. Find the volume of the solid it sweeps out.Volume pi^2/2 ≈ 4.9348
    1. V=π∫0πsin2⁡xdx=π∫0π1−cos⁡2x2dx=π[x2−sin⁡2x4]0π=π⋅π2=π22≈4.93
  172. 12. Areas, volumes and averages · Washers have a hole in the middle · QuizWhich integral gives the volume when the region between y=x and y=x2, for 0≤x≤1, spins around the x-axis?A. π∫01(x2−x4)dxOuter radius x, inner radius x2: the washer is πx2−πx4. The tempting π(x−x2)2 squares the difference of the radii, which is the area of a disk as thick as the region, not of a ring. R2−r2 and (R−r)2 are different numbers.
  173. 12. Areas, volumes and averages · Shells: slicing parallel to the axis · QuizThe region under y=x2 for 0≤x≤2 spins around the y-axis. What is its volume, by shells?A. 8πV=2π∫02x⋅x2dx=2π[x44]02=2π⋅4=8π. 32π5 is π∫02x4dx: disks around the x-axis, a different solid. Pick the method by the axis: shells have radius x when spinning around the y-axis.
  174. 12. Areas, volumes and averages · The average value of a function · QuizWhat is the average value of x2 on [0,3]?A. 313∫03x2dx=13⋅9=3. Not 4.5, the average of the end values 0 and 9: that only works for straight lines. The curve spends most of the interval low down, so its average is lower.
  175. 12. Areas, volumes and averages · The average value of a function · Code challengeWrite average(f, a, b, n): the average value of f on [a, b], from a Riemann sum with n equal strips, each measured at its midpoint.One solution:
    Solution
    #include <functional>
    double average(std::function<double(double)> f,               double a, double b, int n) {  double width = (b - a) / n;  double area = 0;  for (int i = 0; i < n; i++) {    area += f(a + (i + 0.5) * width) * width;  }  return area / (b - a);}
    The sum of f(midpoint) * width approximates the integral, and dividing by b - a turns the area into an average. Since every strip has the same width, this is the same as the plain average of the n sampled values: the continuous average really is the limit of ordinary averages. Forgetting the division gives the area, 9, instead of the average, 3.
  176. 12. Areas, volumes and averages · The length of a curve · Code challengeWrite arcLength(f, a, b, n): split [a, b] into n equal steps and add the straight-line distances between consecutive points on the curve (use std::hypot(dx, dy), from <cmath>, which is dx2+dy2).One solution:
    Solution
    #include <cmath>#include <functional>
    double arcLength(std::function<double(double)> f,                 double a, double b, int n) {  double dx = (b - a) / n;  double length = 0;  for (int i = 0; i < n; i++) {    double x = a + i * dx;    length += std::hypot(dx, f(x + dx) - f(x));  }  return length;}
    Each chord is dx2+dy2, exactly the ds of the formula before the limit; adding chords is how arc length is defined in the first place. The chords cut corners, so the sum is always a little short, and the shortfall shrinks as n grows. The starter returns b - a, the length of a flat line: right only when the curve is flat. In the tests, 2.0 / 3 and 14.0 / 3 are written with a .0 on purpose: 2 / 3 divides two ints, and is 0.
  177. 12. Areas, volumes and averages · Work done by a varying force · QuizA spring with k=400 N/m is stretched from 0.1 m to 0.3 m. How much work does that take?A. 16 J∫0.10.3400xdx=200(0.32−0.12)=200(0.09−0.01)=16 J. 8 J would be 12k(0.2)2, as if the spring started relaxed: the first 0.1 m was already stretched, and the later centimetres are the expensive ones because the force is bigger there.
  178. 12. Areas, volumes and averages · A preview: probability as area · QuizFor the density p(x)=2x on [0,1], what is P(X≤12)?A. 14First, it is a density: 2x≥0 and ∫012xdx=1. Then P(X≤12)=∫01/22xdx=(12)2=14. Not 12: the density grows with x, so most of the probability sits in the right half. The statistics course builds everything on this idea.
  179. 12. Areas, volumes and averages · Practice problems · ProblemThe region between y=2x and y=x2 spins around the x-axis. Find the volume of the solid.Volume 64*pi/15 ≈ 13.4041
    1. The curves meet at x=0 and x=2, and between them 2x≥x2: the outer radius is R=2x and the inner one r=x2.
    2. V=π∫02(4x2−x4)dx=π[4x33−x55]02=π(323−325)=64π15≈13.4
  180. 12. Areas, volumes and averages · Practice problems · ProblemThe region under y=x−x2 and above the x-axis spins around the y-axis. Find its volume by shells.Volume pi/6 ≈ 0.523599
    1. V=2π∫01x(x−x2)dx=2π∫01(x2−x3)dx=2π(13−14)=π6≈0.524
  181. 12. Areas, volumes and averages · Practice problems · ProblemOver a 12-hour day the temperature is T(t)=10+6sin⁡(πt12) degrees Celsius, t hours after 6 am. What is the average temperature from t=0 to t=12?Average 10 + 12/pi ≈ 13.8197 °C
    1. ∫012sin⁡πt12dt=[−12πcos⁡πt12]012=12π(1−cos⁡π)=24π
    2. T‾=112(10⋅12+6⋅24π)=10+12π≈13.8∘C
    3. The temperature runs from 10 to 16 °C, but the average is not 13: the sine arch spends more of the day near its top than near its ends.
  182. 12. Areas, volumes and averages · More practice problems · ProblemFind the length of the curve y=x3/2 from x=0 to x=4.Length 8*(10*sqrt(10) - 1)/27 ≈ 9.07342
    1. L=∫041+94xdx=49⋅23[(1+94x)3/2]04=827(103/2−1)=8(1010−1)27≈9.07
    2. The straight line from (0,0) to (4,8) is 80≈8.94 long: the curve is a little longer, as it must be.
  183. 12. Areas, volumes and averages · More practice problems · ProblemA force of 30N holds a spring stretched 0.1m beyond its natural length. Find its spring constant, and the work needed to stretch it from 0.1m to 0.3m beyond its natural length.k= 300 N/m; Work 12 J
    1. 30=k⋅0.1, so k=300N/m.
    2. W=∫0.10.3300xdx=150(0.32−0.12)=150⋅0.08=12J
  184. 12. Areas, volumes and averages · More practice problems · ProblemA random quantity X has density p(x)=cx2 on [0,3], and 0 elsewhere. Find c, the probability P(X≤2), and the mean 𝔼[X].c= 1/9 ≈ 0.111111; P(X≤2)= 8/27 ≈ 0.296296; 𝔼[X]= 9/4 ≈ 2.25
    1. ∫03cx2dx=9c=1, so c=19.
    2. P(X≤2)=∫02x29dx=827≈0.296,𝔼[X]=∫03x39dx=8136=94
    3. The mean, 2.25, is past the middle of [0,3] because the density grows with x.
  185. 13. Numerical integration · Computers integrate by sampling · QuizWhen do you need to integrate numerically? Choose every one.A. The antiderivative is not elementary, like for e−x2; B. You only have measured values, not a formula; D. The antiderivative exists but would take pages of algebraWith no formula, or only data, sampling is the only option; and even when a formula exists, a good numerical method with an error estimate is often faster and safer than pages of algebra. For x3 the antiderivative x44 is immediate and exact, so there is no reason to approximate.
  186. 13. Numerical integration · The trapezoid rule joins the samples with straight lines · Code challengeWrite trapezoid(f, a, b, n): the trapezoid rule with n equal pieces.One solution:
    Solution
    #include <functional>
    double trapezoid(std::function<double(double)> f,                 double a, double b, int n) {  double h = (b - a) / n;  double sum = (f(a) + f(b)) / 2;  for (int i = 1; i < n; i++) {    sum += f(a + i * h);  }  return sum * h;}
    Start with the two half-weighted ends, add the inside points once each, and multiply by h at the end (one multiplication instead of n). A common slip is a loop from 0 to n, which counts the ends fully as well: the answer then comes out too big by hf(a)+f(b)2. For a convex curve like x2 the chords lie above it, so the rule overestimates.
  187. 13. Numerical integration · The trapezoid rule joins the samples with straight lines · ProblemUse the trapezoid rule with n=4 to estimate ∫02x3dx, and find its error: the estimate minus the exact value, 4.T4= 4.25; Error 0.25
    1. The samples are f(0)=0, f(0.5)=0.125, f(1)=1, f(1.5)=3.375 and f(2)=8.
    2. T4=0.5[12⋅0+0.125+1+3.375+12⋅8]=0.5⋅8.5=4.25
    3. The exact value is [x44]02=4, so the error is +0.25: x3 is convex on [0,2], and the chords lie above it.
  188. 13. Numerical integration · The midpoint rule samples the middle of each piece · Fill the blanksComplete the midpoint rule.1: n, 2: 0.5, 3: hThe step is b−an. Piece i runs from a+ih to a+(i+1)h, so its centre is at a+(i+0.5)h. Every rectangle has width h, so multiplying once at the end is enough. No end point is ever sampled, which also makes the rule usable when f blows up at an end.
  189. 13. Numerical integration · The midpoint rule samples the middle of each piece · QuizFor some integral, T10 is 0.006 too big. About what would you expect M10 to be off by?A. 0.003 too smallFor smooth f and small h, the trapezoid error is about +(b−a)h212f′′ and the midpoint error about −(b−a)h224f′′: half the size, opposite sign. That suggests combining them so the errors cancel, which is exactly the next slide.
  190. 13. Numerical integration · The midpoint rule samples the middle of each piece · ProblemNow use the midpoint rule with n=4 on the same integral, ∫02x3dx=4. What is M4, and what is its error?M4= 3.875; Error −0.125
    1. The centres are 0.25, 0.75, 1.25 and 1.75, whose cubes are 0.015625, 0.421875, 1.953125 and 5.359375.
    2. M4=0.5(0.015625+0.421875+1.953125+5.359375)=0.5⋅7.75=3.875
    3. The error is 3.875−4=−0.125: half the trapezoid error, +0.25, with the opposite sign.
  191. 13. Numerical integration · Simpson's rule fits parabolas · Find the bugThis Simpson's rule gives wrong answers even for f(x)=1. Which line is wrong?Line 6: int w = i % 2 == 1 ? 4 : 2;The odd-numbered points, the middles of each pair, get weight 4; the even inside points, shared by two pairs, get 2. Swapped, the weights add up to the wrong total: with n=2 and f=1 it gives h3(1+2+1)=4h3 instead of 2h. Testing with f=1, where every rule must give b−a, catches weight mistakes at once.
  192. 13. Numerical integration · Simpson's rule fits parabolas · Code challengeWrite simpson(f, a, b, n): Simpson's rule with n pieces. It returns a std::optional<double>: the estimate, or std::nullopt (no answer) when n is odd.One solution:
    Solution
    #include <functional>#include <optional>
    std::optional<double> simpson(std::function<double(double)> f,                              double a, double b, int n) {  if (n % 2 != 0) return std::nullopt;  // n must be even  double h = (b - a) / n;  double sum = f(a) + f(b);  for (int i = 1; i < n; i++) {    sum += (i % 2 == 1 ? 4 : 2) * f(a + i * h);  }  return sum * h / 3;}
    The ends get weight 1, odd inside points 4 and even inside points 2, and the sum is multiplied by h3. With n=10, Simpson is already within about 10−4 of 2; the trapezoid rule needs more than 100 pieces for that. An odd n leaves one piece without a partner, so the parabolas do not fit, and the honest thing is to refuse: std::nullopt says “no answer” without stopping the program, and *s reads the number out of an answer that has one.
  193. 13. Numerical integration · Simpson's rule fits parabolas · ProblemEstimate ∫0πsin⁡xdx, which is exactly 2, with n=4 (so h=π4): by the trapezoid rule, and by Simpson's rule. Give S4 exactly or to 4 decimal places.T4= pi*(1 + sqrt(2))/4 ≈ 1.89612; S4= pi*(2*sqrt(2) + 1)/6 ≈ 2.00456
    1. The samples are 0,22,1,22,0.
    2. T4=π4[0+22+1+22+0]=π(1+2)4≈1.8961
    3. S4=π12[0+4⋅22+2⋅1+4⋅22+0]=π(42+2)12=π(22+1)6≈2.0046
    4. From the same five samples, the trapezoid rule is off by about 0.10 and Simpson by about 0.005, over twenty times less.
  194. 13. Numerical integration · Error and order: what halving the step buys · QuizSimpson's rule with n=10 has an error of 0.0016. About what is the error with n=20?A. 0.0001Order 4: halving h divides the error by 24=16, and 0.001616=0.0001. 0.0004 is what an order-2 rule would give. Measuring the ratio of errors as n doubles is how you check, in practice, that code implements the rule you think it does.
  195. 13. Numerical integration · Adaptive methods spend samples where they are needed · QuizWhy does adaptive Simpson split the pieces near 0 more often for x on [0,1]?A. Its error estimate is large there, because x curves sharply near 0The derivatives of x blow up at 0, so a parabola fits it badly there, and the halves disagree with the whole. Far from 0 it is gentle, the estimates agree, and the method stops early. This is how library integrators (such as QUADPACK, behind many scientific tools) handle awkward functions without being told where the trouble is.
  196. 13. Numerical integration · Monte Carlo: integrate by random sampling · QuizA Monte Carlo estimate with 10000 samples has an error of about 0.01. How many samples for 0.001?A. 1000000The error goes like 1N: ten times smaller error needs N ten times bigger, so N a hundred times bigger. That is why Monte Carlo is the last resort in one dimension and the first choice in fifty.
  197. 13. Numerical integration · Practice problems · ProblemFor one smooth integral, T10 has an error of 0.0032. For another, S10 (Simpson's rule) has an error of 0.0032. Estimate the error of T40 for the first, and of S40 for the second.Error of T40 0.0002; Error of S40 0.0000125
    1. From n=10 to n=40, h becomes 4 times smaller, so an error of order p shrinks by 4p.
    2. Trapezoid, p=2: 0.003242=0.0002,Simpson, p=4: 0.003244=0.0000125
  198. 13. Numerical integration · Practice problems · ProblemHow many trapezoids guarantee that Tn is within 10−4 of ∫01exdx? Use the bound |ET|≤(b−a)h212max⁡|f′′|, with max⁡|f′′|=e on [0,1], and give the smallest whole n.n= 48
    1. e12n2<10−4⟺n2>e⋅10412≈2265.2⟺n>47.6
    2. So n=48 is the smallest n the bound guarantees. The real error is a little smaller, because the bound uses the largest value of f′′, e, all along the interval.
  199. 13. Numerical integration · Practice problems · ProblemA car starts from rest, and its speed is logged every 10 seconds: 0,12,20,24,26 m/s at t=0,10,20,30,40 s. Estimate how far it travels in those 40 seconds, by the trapezoid rule and by Simpson's rule.Trapezoid 690 m; Simpson 700 m
    1. T4=10[12⋅0+12+20+24+12⋅26]=10⋅69=690m
    2. S4=103[0+4⋅12+2⋅20+4⋅24+26]=103⋅210=700m
    3. The speeds rise by less each time (12, 8, 4, 2), so the curve bends down: the chords lie below it and the trapezoid estimate is a little low, while Simpson follows the bend.
  200. 13. Numerical integration · Practice problems · ProblemA Monte Carlo estimate with N=2500 random samples has an error of about 0.02. About how many samples bring the error down to 0.005?N≈ 40000
    1. The error is about σN. To make it 4 times smaller, N must be 4 times bigger, so N must be 42=16 times bigger:
    2. N≈16×2500=40000
  201. 14. Infinity: L'Hôpital and improper integrals · Some limits cannot be read off · QuizWhich of these forms is not indeterminate?A. 0∞A top shrinking to 0 over a bottom growing without bound can only go to 0: both effects push the same way. In the others the two parts pull in opposite directions, and which wins depends on the functions. 1∞ is the sneaky one: 1n=1, but (1+1n)n→e, because the base is only near 1.
  202. 14. Infinity: L'Hôpital and improper integrals · L'Hôpital's rule compares the speeds · QuizWhat is limx→0⁡1−cos⁡xx2?A. 12It is 00, so differentiate: sin⁡x2x, still 00. Again: cos⁡x2→12. Stopping after one step and saying "sin⁡x2x at 0 is 0" is the usual slip: that was still indeterminate.
  203. 14. Infinity: L'Hôpital and improper integrals · L'Hôpital's rule compares the speeds · QuizSomeone computes limx→1⁡x2+1x+1 with L'Hôpital: limx→1⁡2x1=2. What is the real limit?A. 1At x=1 the fraction is 22=1, nothing indeterminate about it, so plugging in is the answer. The rule only holds for 00 and ∞∞; used on anything else it gives nonsense. Always check the form first.
  204. 14. Infinity: L'Hôpital and improper integrals · L'Hôpital's rule compares the speeds · ProblemFind limx→0⁡e2x−1−2xx2.2
    1. At x=0 the top is 1−1−0=0 and the bottom is 0: the form is 00.
    2. limx→0⁡e2x−1−2xx2=limx→0⁡2e2x−22x(00 again)=limx→0⁡4e2x2=2
  205. 14. Infinity: L'Hôpital and improper integrals · Rewrite the other forms as a fraction · QuizWhat is limx→0+⁡xx?A. 1Take logs: ln⁡(xx)=xln⁡x→0, as above. So xx→e0=1. It looks as if 0 to any power should be 0, but the exponent is shrinking too, and the two effects balance at 1. Forgetting to undo the log at the end, and answering 0, is the classic slip.
  206. 14. Infinity: L'Hôpital and improper integrals · Rewrite the other forms as a fraction · ProblemFind limx→∞⁡(1+3x)x.e^3 ≈ 20.0855
    1. ln⁡y=xln⁡(1+3x)=ln⁡(1+3t)t,t=1x→0+
    2. limt→0+⁡ln⁡(1+3t)t=limt→0+⁡3/(1+3t)1=3⇒y→e3≈20.1
  207. 14. Infinity: L'Hôpital and improper integrals · Logs are slow, powers are steady, exponentials win · QuizWhat is limx→∞⁡(ln⁡x)3x?A. 0Any power of a log loses to any positive power of x. To see it, put x=eu: the fraction is u3eu/2, a power over an exponential, which goes to 0. The table hides this for small x: at x=100 the top is about 98 and the bottom 10. Growth rates are about the long run.
  208. 14. Infinity: L'Hôpital and improper integrals · An integral over an infinite interval is a limit · QuizWhat is ∫0∞e−2xdx?A. 12∫0Re−2xdx=[−12e−2x]0R=12−12e−2R→12. Exponential decay is fast enough for a finite area. Answering 2 comes from dividing by −2 the wrong way: the antiderivative of ekx is 1kekx.
  209. 14. Infinity: L'Hôpital and improper integrals · An integral over an infinite interval is a limit · ProblemEvaluate ∫0∞dx1+x2.pi/2 ≈ 1.5708
    1. ∫0∞dx1+x2=limR→∞⁡[arctan⁡x]0R=limR→∞⁡arctan⁡R−0=π2
    2. Far out the curve falls like 1x2, a p=2 integrand, so a finite area is what you should expect.
  210. 14. Infinity: L'Hôpital and improper integrals · The p-integrals: the yardstick for convergence · Code challengeWrite pIntegral(p): the value of the integral of 1xp from 1 to infinity, or INFINITY (from <cmath>) when it diverges.One solution:
    Solution
    #include <cmath>
    double pIntegral(double p) {  // The antiderivative is x^(1 - p) / (1 - p); it only  // shrinks to 0 at infinity when 1 - p is negative.  if (p <= 1) return INFINITY;  return 1 / (p - 1);}
    For p≠1, ∫1Rx−pdx=R1−p−11−p. When p>1, R1−p→0 and the value is 1p−1. When p<1, R1−p→∞. p=1 is the logarithm, which also grows without bound. Writing 1 / (p - 1) for every p would return −2 for p=0.5: a negative area for a positive function, which should make you suspicious.
  211. 14. Infinity: L'Hôpital and improper integrals · Integrands that blow up are improper too · QuizSomeone writes ∫−11dxx2=[−1x]−11=−1−1=−2. What is wrong?A. 1x2 blows up at x=0, inside the interval, and the integral divergesA positive function cannot have a negative integral: that is the alarm. The fundamental theorem needs f continuous on the whole interval, and 1x2 is not defined at 0. Split at 0: ∫01dxx2 is a p-integral with p=2≥1, so it diverges, and so does the whole thing. Always look for points where the integrand blows up inside the interval.
  212. 14. Infinity: L'Hôpital and improper integrals · Integrands that blow up are improper too · ProblemEvaluate ∫01ln⁡xdx. The integrand goes to −∞ at x=0.-1
    1. ∫ε1ln⁡xdx=[xln⁡x−x]ε1=(0−1)−(εln⁡ε−ε)
    2. As ε→0+, ε→0 and εln⁡ε→0 (by L'Hôpital, earlier in this module). So the integral is −1: the region lies below the axis, with area 1.
  213. 14. Infinity: L'Hôpital and improper integrals · Compare with something you know · QuizWhich of these converge? Choose every one.A. ∫1∞dxx2+x; C. ∫1∞sin2⁡xx2dx1x2+x≤1x2 and sin2⁡xx2≤1x2, both under a convergent p=2 integral. xx2+1 behaves like 1x for large x (its antiderivative is 12ln⁡(x2+1)), and 1x is p=12: both diverge. What matters is how the integrand behaves far out, not its first few values.
  214. 14. Infinity: L'Hôpital and improper integrals · Compare with something you know · Code challengeWrite improper(f, a, n): approximate the integral of f from a to infinity by substituting x=a+t1−t, which maps t in [0, 1) onto [a, ∞), then using the midpoint rule with n pieces on g(t)=f(a+t1−t)(1−t)2.One solution:
    Solution
    #include <functional>
    double improper(std::function<double(double)> f, double a, int n) {  auto g = [=](double t) {    return f(a + t / (1 - t)) / ((1 - t) * (1 - t));  };  double h = 1.0 / n;  double sum = 0;  for (int i = 0; i < n; i++) sum += g((i + 0.5) * h);  return sum * h;}
    The substitution squeezes the infinite interval into [0,1); dx=dt(1−t)2 is the stretch factor. The midpoint rule never samples t=1, where x would be infinite. This only works when the integral converges: try it on 1x and the answer keeps growing with n, which is the numerical sign of divergence. Forgetting the 1(1−t)2 factor gives the integral of the wrong function. And the step is 1.0 / n: with an int n, 1 / n is 0.
  215. 14. Infinity: L'Hôpital and improper integrals · Gabriel's horn: finite volume, infinite surface · QuizWhy is the horn's volume finite while its surface is not?A. Volume uses 1x2 (the radius squared), which falls fast enough; area uses about 1x, which does notEach disk has area πr2=πx2, a p=2 integrand; each ring of surface has area about 2πrdx=2πxdx, a p=1 integrand. One power of x is the whole difference. Volume and area are not comparable at all: they are measured in different units.
  216. 14. Infinity: L'Hôpital and improper integrals · Practice problems · ProblemEvaluate ∫2∞dxx(ln⁡x)2.1/ln(2) ≈ 1.4427
    1. With u=ln⁡x, du=dxx; x=2 gives u=ln⁡2, and u→∞ as x→∞.
    2. ∫2∞dxx(ln⁡x)2=∫ln⁡2∞duu2=limR→∞⁡[−1u]ln⁡2R=1ln⁡2≈1.443
    3. Compare ∫2∞dxxln⁡x=∫ln⁡2∞duu, which diverges: one more power of the log makes the difference.
  217. 14. Infinity: L'Hôpital and improper integrals · Practice problems · ProblemA drug is absorbed into the blood at a rate of 50e−0.2t milligrams per hour, t hours after a dose. How much is absorbed in the first 5 hours, and how much in total, if absorption goes on forever?First 5 hours 250*(1 - e^(-1)) ≈ 158.03 mg; In total 250 mg
    1. ∫0550e−0.2tdt=[−250e−0.2t]05=250(1−e−1)≈158mg
    2. ∫0∞50e−0.2tdt=limR→∞⁡250(1−e−0.2R)=250mg
    3. About 63% of the total arrives in the first 5 hours.
  218. 14. Infinity: L'Hôpital and improper integrals · Practice problems · ProblemThe region under y=e−x, for x≥0, spins around the x-axis: a solid that goes on forever. Find its volume.Volume pi/2 ≈ 1.5708
    1. V=π∫0∞e−2xdx=πlimR→∞⁡[−12e−2x]0R=π⋅12=π2≈1.57
    2. Finite: the radius shrinks exponentially, far faster than the 1x of Gabriel's horn.
  219. 15. Sequences and series · A sequence is a list that goes on forever · QuizWhat is the limit of an=3n2+1n2+5n?A. 3Divide top and bottom by n2: 3+1/n21+5/n→31=3. For large n only the highest powers matter. The 5n and the 1 affect the first terms, never the limit.
  220. 15. Sequences and series · A series is the limit of its partial sums · QuizA series has partial sums SN=2−3N. What is its sum, and what is its first term?A. Sum 2, first term −1The sum is lim⁡SN=2. The first term is S1=2−3=−1. Keep the two sequences apart: the terms an, and the partial sums SN. Here the terms are an=Sn−Sn−1, which go to 0, while the partial sums go to 2.
  221. 15. Sequences and series · A series is the limit of its partial sums · ProblemUsing 2n(n+2)=1n−1n+2, find the partial sum S4 of ∑n=1∞2n(n+2), and the sum of the series.S4= 17/15 ≈ 1.13333; Sum 3/2 ≈ 1.5
    1. SN=(1−13)+(12−14)+(13−15)+⋯+(1N−1N+2)=1+12−1N+1−1N+2
    2. So S4=32−15−16=45−6−530=1715. As N→∞ the last two terms vanish, and the sum is 32.
  222. 15. Sequences and series · Geometric series: the one you can always sum · QuizWhat is ∑n=1∞(23)n?A. 2The first term is a=23 (the sum starts at n=1) and r=23, so the sum is 2/31−2/3=2. The tempting 3 is 11−r, which assumes the series starts with 1, at n=0. Always check where the index starts.
  223. 15. Sequences and series · Geometric series: the one you can always sum · ProblemFind ∑n=1∞54n. Then write the repeating decimal 0.272727… as a fraction, by treating it as a geometric series.∑n=1∞54n= 5/3 ≈ 1.66667; 0.2727…= 3/11 ≈ 0.272727
    1. ∑n=1∞54n=5/41−1/4=5/43/4=53
    2. 0.2727…=27100+271002+⋯=27/1001−1/100=2799=311
  224. 15. Sequences and series · If the terms do not shrink to zero, the series diverges · QuizWhat does the divergence test say about ∑1n and about ∑nn+1?A. Nothing about the first; the second divergesnn+1→1≠0, so that series diverges: you keep adding numbers close to 1. 1n→0, so the test is silent; the series happens to diverge, but you need another argument (grouping, or the integral test next) to show it. "The terms go to 0, so it converges" is the classic false step.
  225. 15. Sequences and series · The integral test, and the p-series · QuizWhich of these converge? Choose every one.A. ∑1n1.01; C. ∑1nnp=1.01 and p=1.5 (since nn=n3/2) are above 1, so they converge; p=12 and p=1 do not. p=1.01 converges absurdly slowly, but it converges: the line is sharp at p=1.
  226. 15. Sequences and series · The integral test, and the p-series · Code challengeWrite partialSum(term, N): the sum of term(n) for n from 1 to N. term takes n as a double, so a term like 1 / n divides with decimals.One solution:
    Solution
    #include <functional>
    double partialSum(std::function<double(double)> term, int N) {  double sum = 0;  for (int n = 1; n <= N; n++) sum += term(n);  return sum;}
    A loop from 1 to N inclusive; starting at 0 would divide by zero for 1n, and with a double that gives inf rather than an error, so the sum would be inf. The tests show the two behaviours: ∑1n2 settles (after 105 terms it is within 10−5 of π26, since the tail is about 1N), while the harmonic sum keeps growing like ln⁡N+γ, with γ≈0.5772 the Euler–Mascheroni constant. A million terms of a divergent series still only reach about 14.4: you cannot see divergence by adding terms. Had term taken an int, 1 / n would be integer division, 0 for every n above 1.
  227. 15. Sequences and series · Comparison tests: measure against a known series · QuizUsing limit comparison, what does ∑n+3n3−n+2 do?A. Converges: it behaves like ∑1n2Keep the biggest powers: nn3=1n2. Then an1/n2=n3+3n2n3−n+2→1, a positive finite limit, so it converges like the p=2 series. "Its terms go to 0" is true but proves nothing, as the harmonic series shows.
  228. 15. Sequences and series · Ratio and root tests: is it eventually geometric? · QuizWhat does the ratio test say about ∑1n2?A. Nothing: L=1n2(n+1)2→1, so the test is silent, even though the series converges (it is a p-series). The same L=1 comes from the divergent ∑1n. Each ratio being below 1 is not enough; the limit must be below 1. For powers of n, use p-series and comparison instead.
  229. 15. Sequences and series · Alternating series converge if the terms shrink · Code challengeWrite alternating(N): it returns an Estimate holding sum, the partial sum of 1 - 1/2 + 1/3 - ... up to the term ±1N, and bound, the most it can be off by, 1N+1.One solution:
    Solution
    struct Estimate {  double sum;  double bound;};
    Estimate alternating(int N) {  double sum = 0;  for (int n = 1; n <= N; n++) {    double sign = n % 2 == 1 ? 1 : -1;    sum += sign / n;  }  return {sum, 1.0 / (N + 1)};}
    Odd n add, even n subtract. The bound holds because the limit is always trapped between two consecutive partial sums, which differ by exactly the next term. Notice how slowly it converges: a thousand terms buy only three digits of ln⁡2. Testing the bound on many N, odd and even, catches a sign started the wrong way round. sign is a double, and the bound is 1.0 / (N + 1), for one reason: 1 / (N + 1) divides two ints and is 0, and an int sign / n would be 0 for every n above 1.
  230. 15. Sequences and series · Alternating series converge if the terms shrink · ProblemHow many terms of ∑n=1∞(−1)n+1n2=1−14+19−… does the alternating series bound say you need, for an error below 0.001?N= 31
    1. 1(N+1)2<0.001⟺(N+1)2>1000⟺N+1≥32(312=961, 322=1024)
    2. So N=31 terms. The series 1−12+13−… needs 1000 terms for the same guarantee: terms that shrink faster converge faster.
  231. 15. Sequences and series · Absolute and conditional convergence · QuizClassify ∑(−1)nn and ∑(−1)nn2.A. The first converges conditionally; the second absolutely1n decreases to 0, so the first converges by the alternating test, but ∑1n is a p=12 series, which diverges: conditional. ∑1n2 converges, so the second converges absolutely. Test ∑|an| first; reach for the alternating test only if that fails.
  232. 15. Sequences and series · Absolute and conditional convergence · Put in orderPut these in a sensible order to try on a series you do not recognise.1. Divergence test: do the terms go to 0? 2. Is it geometric, telescoping or a p-series? 3. Compare with one of those (direct or limit comparison) 4. Ratio or root test, for factorials and powers cn 5. Alternating series test, if the signs alternateThe divergence test is free, so it goes first. Then the series you can sum or decide outright, then comparisons with them. Ratio and root are next, being powerful for factorials and exponentials. The alternating test comes last because it can show convergence but never absolute convergence, so you use it once the tests on ∑|an| have failed.
  233. 15. Sequences and series · Practice problems · ProblemFind the limit L of the ratio test for ∑n23n, and for ∑n!nn.L for ∑n23n 1/3 ≈ 0.333333; L for ∑n!nn 1/e ≈ 0.367879
    1. (n+1)2/3n+1n2/3n=13(1+1n)2→13
    2. (n+1)!/(n+1)n+1n!/nn=(n+1)nn(n+1)n+1=(nn+1)n=1(1+1n)n→1e
    3. Both limits are below 1, so both series converge.
  234. 15. Sequences and series · Practice problems · ProblemA ball is dropped from a height of 2 m. Each bounce takes it back up to 34 of the height it last fell from. If it bounces forever, what total distance does it travel, up and down?Distance 14 m
    1. The first fall is 2 m. Then the ball rises and falls 1.5 m, then 1.125 m, and so on, each height 34 of the last.
    2. D=2+2(1.5+1.5⋅34+1.5⋅(34)2+…)=2+2⋅1.51−3/4=2+2⋅6=14m
    3. Infinitely many bounces, a finite distance.
  235. 15. Sequences and series · Practice problems · ProblemTo test ∑2n+1n3+4, compare it with ∑1n2. What is limn→∞⁡anbn, with an=2n+1n3+4 and bn=1n2?2
    1. anbn=2n3+n2n3+4=2+1/n1+4/n3→2
    2. The limit is positive and finite, so the series does what ∑1n2 does: it converges.
  236. 15. Sequences and series · Practice problems · ProblemFind ∑n=1∞2n+3n6n.3/2 ≈ 1.5
    1. ∑n=1∞(13)n+∑n=1∞(12)n=1/31−1/3+1/21−1/2=12+1=32
    2. Splitting one series into two is allowed here because both parts converge.
  237. 16. Taylor series · A power series is a polynomial that never ends · QuizFor which x does ∑n=0∞(2x)n converge, and to what?A. |x|<12, to 11−2xIt is geometric with ratio r=2x, which converges exactly when |2x|<1, that is |x|<12, to 11−r=11−2x. The formula 11−2x makes sense at x=1, but the series does not converge there: a function and its series can disagree outside the interval.
  238. 16. Taylor series · Every power series has a radius of convergence · QuizWhat is the interval of convergence of ∑n=0∞(x−2)n3n?A. (−1,5)It is geometric with ratio x−23, so it converges when |x−2|<3: radius 3, centred at 2, so −1<x<5. At both endpoints the ratio is ±1 and the terms do not go to 0, so they are excluded. (−3,3) forgets the centre.
  239. 16. Taylor series · Taylor series: match every derivative · QuizIn the Maclaurin series of f, the coefficient of x3 is 5. What is f′′′(0)?A. 30c3=f′′′(0)3!, so f′′′(0)=3!⋅5=30. This works both ways: a series you already know hands you all the derivatives at the centre for free. Forgetting the factorial is the usual slip.
  240. 16. Taylor series · Taylor series: match every derivative · ProblemFind the Taylor polynomial T2 of f(x)=x centred at a=4, and use it to estimate 4.2 to 4 decimal places.T2(x)= 2 + (x - 4)/4 - (x - 4)^2/64; 4.2≈ 2.04937
    1. f(4)=2,f′(4)=12⋅2=14,f′′(4)=−14⋅8=−132
    2. T2(x)=2+14(x−4)−132⋅2!(x−4)2=2+x−44−(x−4)264
    3. At x=4.2: 2+0.05−0.0464=2.049375. The true value is 2.049390…, an error of about 1.5×10−5.
  241. 16. Taylor series · Five series worth knowing by heart · QuizDifferentiate the series for sin⁡x term by term. What do you get?A. 1−x22!+x44!−…, the series for cos⁡xInside its interval, a power series can be differentiated term by term. ddxx2n+1(2n+1)!=x2n(2n)!, since (2n+1) cancels against the top of (2n+1)!. So the series of sin⁡ differentiates into the series of cos⁡, as it must.
  242. 16. Taylor series · Build new series from old ones · QuizStart from 11+x2, whose integral is arctan⁡x. What is the Maclaurin series of arctan⁡x?A. x−x33+x55−…Put −x2 into the geometric series: 11+x2=1−x2+x4−… for |x|<1. Integrate term by term from 0 (and arctan⁡0=0): x−x33+x55−…. At x=1 it gives π4=1−13+15−…, a famous but very slow way to compute π. The factorials belong to sin⁡, not here.
  243. 16. Taylor series · Build new series from old ones · Code challengeWrite expSeries(x, N): the Taylor polynomial of ex at 0 up to the term xNN!. Build each term from the one before (multiply by x / n) instead of computing powers and factorials.One solution:
    Solution
    double expSeries(double x, int N) {  double term = 1;  double sum = 1;  for (int n = 1; n <= N; n++) {    term *= x / n;    sum += term;  }  return sum;}
    Since xnn!=xn−1(n−1)!⋅xn, each term is the last times xn: one multiplication, and no huge intermediate numbers like 20! or x20 that can overflow. Factorials grow faster than any power, so the terms eventually shrink for every x, which is why the radius is infinite.
  244. 16. Taylor series · Build new series from old ones · ProblemSubstitute into the series for cos⁡ to get the Maclaurin series of f(x)=cos⁡(x2). What is the coefficient of x4, and what is f(8)(0)?Coefficient of x4 -1/2 ≈ −0.5; f(8)(0)= 1680
    1. cos⁡(x2)=1−x42!+x84!−⋯=1−x42+x824−…
    2. The coefficient of x4 is −12. The coefficient of x8 is 124=f(8)(0)8!, so f(8)(0)=4032024=1680, without differentiating eight times.
  245. 16. Taylor series · Taylor's remainder: how much to trust a polynomial · QuizOn [0,1], every derivative of ex is at most e<3. What is the smallest N that guarantees |e−TN(1)|<10−6 by the bound 3(N+1)!?A. N=9You need (N+1)!>3×106. 9!=362880 is too small, 10!=3628800 is enough, so N+1=10 and N=9. The bound is honest: the actual error at N=8 is about 3×10−6, still too big. Off-by-one slips between N and N+1 are the usual mistake.
  246. 16. Taylor series · Taylor's remainder: how much to trust a polynomial · ProblemEstimate sin⁡(0.5) with the Maclaurin polynomial T3(x)=x−x36 (exactly, or to 4 decimal places), and bound its error with the remainder formula for N=3, using |f(4)|≤1.T3(0.5)= 23/48 ≈ 0.479167; Error bound 1/384 ≈ 0.00260417
    1. T3(0.5)=0.5−0.1256=2348≈0.479167
    2. |R3(0.5)|≤14!(0.5)4=0.062524=1384≈0.0026
    3. The true error, sin⁡0.5−T3(0.5)≈0.00026, is ten times smaller. The x4 term of the sine series is 0, so T3 is also T4, and the N=4 bound, (0.5)55!≈0.00026, is much sharper.
  247. 16. Taylor series · How a calculator computes sin · Code challengeThe starter sums the series directly. Fix sinTaylor(x) by first reducing x to r = x - 2π · round(x / 2π), which lies in [−π,π] and has the same sine, and summing the series at r. std::round and std::numbers::pi will help.One solution:
    Solution
    #include <cmath>#include <numbers>
    double sinTaylor(double x) {  double twoPi = 2 * std::numbers::pi;  double r = x - twoPi * std::round(x / twoPi);  double term = r;  double sum = r;  for (int k = 1; k < 40; k++) {    term *= (-r * r) / (2 * k * (2 * k + 1));    sum += term;  }  return sum;}
    sin⁡ repeats every 2π, so sin⁡x=sin⁡r. With |r|≤π the largest term is about π36≈5, so there is no catastrophic cancellation, and 40 terms are far more than needed. The step from each term to the next multiplies by −r22k(2k+1): the next odd power over the next two factors of the factorial. Production libraries reduce further, to [−π4,π4], and use a carefully tuned polynomial rather than the plain Taylor one.
  248. 16. Taylor series · Euler's formula: e to an imaginary power · QuizWhat is eiπ/2?A. icos⁡π2+isin⁡π2=0+i⋅1=i: a quarter turn round the circle. Engineers use this to turn every oscillation cos⁡ωt into the real part of eiωt, because exponentials are far easier to differentiate and multiply than sines and cosines.
  249. 16. Taylor series · Euler's formula: e to an imaginary power · ProblemWrite eiπ/3 as a+bi, with a and b real.a= 1/2 ≈ 0.5; b= sqrt(3)/2 ≈ 0.866025
    1. eiπ/3=cos⁡π3+isin⁡π3=12+32i
    2. It is the point a sixth of the way round the unit circle.
  250. 16. Taylor series · When a Taylor series lets you down · Quiz11+x2 is smooth for every real x, yet its Maclaurin series only converges for |x|<1. Why?A. In the complex numbers it blows up at x=±i, at distance 1 from 0The radius of convergence is the distance from the centre to the nearest point where the function misbehaves, counting complex points. 1+x2=0 at x=±i, at distance 1. Real calculus cannot see this; complex analysis explains it, and it is one of the reasons that subject exists.
  251. 16. Taylor series · Practice problems · ProblemFind the radius of convergence of ∑n=1∞(x−1)nn2n. The series converges at exactly one of the two endpoints: which x?Radius R= 2; It converges at the endpoint x= -1
    1. The ratio tends to |x−1|2, which is below 1 when |x−1|<2: centre 1, radius 2, endpoints −1 and 3.
    2. x=3: ∑2nn2n=∑1n diverges;x=−1: ∑(−2)nn2n=∑(−1)nn converges
    3. So the interval of convergence is [−1,3).
  252. 16. Taylor series · Practice problems · ProblemUse the Maclaurin series of cos⁡x to find limx→0⁡1−cos⁡x−x22x4.-1/24 ≈ −0.0416667
    1. 1−cos⁡x−x22=1−(1−x22+x424−x6720+…)−x22=−x424+x6720−…
    2. 1−cos⁡x−x2/2x4=−124+x2720−⋯→−124
  253. 16. Taylor series · Practice problems · Problemsin⁡(x2) has no elementary antiderivative. Estimate ∫01sin⁡(x2)dx to 5 decimal places from the first three nonzero terms of its Maclaurin series, and give the alternating series bound on the error.Estimate 1/3 - 1/42 + 1/1320 ≈ 0.310281; Error bound 1/75600 ≈ 0.0000132275
    1. sin⁡(x2)=x2−x66+x10120−x145040+…
    2. ∫01sin⁡(x2)dx=13−142+11320−175600+⋯≈0.33333−0.02381+0.00076=0.31028
    3. The error is at most the first term left out, 115⋅5040=175600≈1.3×10−5.
  254. 17. Differential equations · An equation whose unknown is a function · QuizWhich function solves y′=−2y with y(0)=5?A. 5e−2tddt5e−2t=−10e−2t=−2⋅5e−2t, and it is 5 at t=0. e−2t+4 has the right start value, but its derivative is −2e−2t, not −2(e−2t+4): always check the equation and the condition.
  255. 17. Differential equations · A slope field shows every solution at once · QuizFor y′=x−y, along which line are the segments flat?A. y=xFlat means slope 0: x−y=0, so y=x. Curves where the slope is constant are called isoclines; finding them is the quickest way to sketch a field by hand. The line y=x−1 is different: it is itself a solution, with slope 1 everywhere, and x−y=1 on it.
  256. 17. Differential equations · Separable equations: split the variables and integrate · QuizSolve y′=y2 with y(0)=1. What happens?A. y=11−x, which blows up at x=1Separate: ∫y−2dy=∫dx, so −1y=x+C; y(0)=1 gives C=−1, so y=11−x. The solution reaches infinity at the finite time x=1: blow-up. Nothing in the innocent-looking equation warns you; growth proportional to y2 is faster than exponential. Solutions of DEs need not exist forever.
  257. 17. Differential equations · Separable equations: split the variables and integrate · ProblemSolve dydx=xy with y(0)=2, for y>0.y= sqrt(x^2 + 4)
    1. ∫ydy=∫xdx⇒y22=x22+C⇒y2=x2+2C
    2. At x=0, y=2: 4=2C. So y2=x2+4, and taking the positive root, y=x2+4.
    3. Check: y′=xx2+4=xy, and y(0)=4=2.
  258. 17. Differential equations · Exponential growth and decay · QuizA substance has a half-life of 8 days. What fraction is left after 24 days?A. 1824 days is three half-lives: 12⋅12⋅12=18. Exponential decay removes the same fraction in each equal time, not the same amount. 13 is just 824, a ratio of the times, which is not how decay works; and a straight-line decline, half gone every 8 days, would be empty by day 16.
  259. 17. Differential equations · Exponential growth and decay · ProblemA bacteria culture doubles every 3 hours, starting from 500 cells. How many cells are there after 10 hours, and when does the culture reach 10000 cells?After 10 hours 500*2^(10/3) ≈ 5039.68 cells; It reaches 10000 at t= 3*ln(20)/ln(2) ≈ 12.9658 h
    1. With a doubling time of 3 hours, N(t)=500⋅2t/3, the same as 500ekt with k=ln⁡23.
    2. N(10)=500⋅210/3≈500⋅10.08≈5040 cells
    3. 500⋅2t/3=10000⟺2t/3=20⟺t=3ln⁡20ln⁡2≈13.0h
  260. 17. Differential equations · Logistic growth levels off · QuizAt what population does a logistic population grow fastest?A. K2The growth rate rP(1−PK) is a downward parabola in P, zero at P=0 and P=K, with its peak halfway, at K2. That is the inflection point of the S-curve. Fisheries use this: harvesting at K2 gives the largest sustainable yield.
  261. 17. Differential equations · Newton's law of cooling · QuizIn that example, how long until the coffee reaches 50 °C?A. ln⁡(7/3)0.05≈16.9 minutesSolve 20+70e−0.05t=50: e−0.05t=3070=37, so t=ln⁡(7/3)0.05≈16.9 minutes. ln⁡(9/5)0.05 uses 9050, temperatures instead of differences from the room. The exponential is in T−Ts, never in T itself.
  262. 17. Differential equations · Newton's law of cooling · ProblemSoup at 85 °C is left in a room at 25 °C. After 10 minutes it is at 65 °C. Find the cooling constant k, and the temperature after 30 minutes.k= ln(3/2)/10 ≈ 0.0405465 per minute; After 30 minutes 385/9 ≈ 42.7778 °C
    1. 25+60e−10k=65⇒e−10k=4060=23⇒k=ln⁡(3/2)10≈0.0405 min−1
    2. T(30)=25+60(23)3=25+1609=3859≈42.8∘C
    3. Every 10 minutes the difference from the room shrinks by the same factor, 23: 60, 40, 26.7, then 17.8 degrees.
  263. 17. Differential equations · Euler's method: follow the slope a step at a time · Code challengeWrite euler(f, t0, y0, h, steps): take steps Euler steps of size h for y' = f(t, y) from (t0, y0), and return the final y. f is a function of two numbers, a std::function<double(double, double)>.One solution:
    Solution
    #include <functional>
    double euler(std::function<double(double, double)> f,             double t0, double y0, double h, int steps) {  double t = t0;  double y = y0;  for (int i = 0; i < steps; i++) {    y += h * f(t, y);    t += h;  }  return y;}
    Each step uses the slope at the start of the step, then moves both t and y. Updating t before computing the slope is a common slip that quietly shifts every step. The error is proportional to h: with h=10−4, Euler gets e to about 4 digits, after 10 000 steps. Better methods (Runge–Kutta, below) sample several slopes per step and do far better.
  264. 17. Differential equations · Second order: a mass on a spring · QuizA 0.5 kg mass hangs on a spring with k=50 N/m, no damping. What is its period?A. 2π10≈0.63 sω=50/0.5=100=10 rad/s, and one full swing takes T=2πω≈0.63 s. 10 is ω itself, in radians per second, not a time. Forgetting the square root gives 2π100.
  265. 17. Differential equations · Systems: several quantities changing together · Code challengeThe starter steps a spring (x′=v, v′=−x) with plain Euler, and its energy grows without limit. Change it to semi-implicit Euler: update v first, then update x using the new v. Return the State, x and v, after steps steps of size h.One solution:
    Solution
    struct State {  double x;  double v;};
    State spring(double x0, double h, int steps) {  double x = x0;  double v = 0;  for (int i = 0; i < steps; i++) {    v += h * -x;    x += h * v;  }  return {x, v};}
    Plain Euler multiplies the energy by 1+h2 every step, so over 105 steps of h=0.01 it grows by about e10≈22000 times (the swing by e5≈150): the simulated spring swings wider and wider. Using the new velocity makes the method symplectic: it keeps a slightly altered energy exactly, so the error wobbles but never builds up. Game physics engines and planetary simulations use this one-line change for exactly that reason.
  266. 17. Differential equations · Practice problems · ProblemTake two Euler steps of size h=0.1 for y′=x+y, y(0)=1, to estimate y(0.2). The exact solution is y=2ex−x−1: how big is Euler's error at x=0.2?Euler estimate of y(0.2) 1.22; Error, exact minus Euler 2*e^0.2 - 2.42 ≈ 0.0228055
    1. y1=1+0.1(0+1)=1.1y2=1.1+0.1(0.1+1.1)=1.22
    2. The exact value is y(0.2)=2e0.2−1.2≈1.24281, so the error is about 0.0228. Euler falls short because the solution curves upward, and each step follows the slope at its start.
  267. 17. Differential equations · Practice problems · ProblemA population follows dPdt=0.4P(1−P1000), with P(0)=100 and t in years. When does it reach 500, and what is its fastest rate of growth?t= ln(9)/0.4 ≈ 5.49306 years; Fastest growth 100 per year
    1. P(t)=10001+9e−0.4t=500⟺9e−0.4t=1⟺t=ln⁡90.4≈5.49 years
    2. dPdt|P=500=0.4⋅500⋅(1−5001000)=100 per year
    3. The two answers belong together: the population grows fastest at the moment it passes K2=500.
  268. 17. Differential equations · Practice problems · ProblemA 2kg mass on a spring with k=18N/m, without damping, is pulled 0.1m from its rest position and let go. Find its angular frequency, its period, and its greatest speed.ω= 3 rad/s; Period 2*pi/3 ≈ 2.0944 s; Greatest speed 0.3 m/s
    1. ω=182=3 rad/s,T=2π3≈2.09s
    2. Starting at x=0.1 with velocity 0 gives A=0.1 and B=0: x(t)=0.1cos⁡3t. Then x′(t)=−0.3sin⁡3t, whose size is at most 0.3m/s, reached each time the mass passes the middle.
  269. 17. Differential equations · Practice problems · ProblemA piece of charcoal has 20% of the carbon-14 that living wood has. Carbon-14 has a half-life of 5730 years. How old is the charcoal?Age 5730*ln(5)/ln(2) ≈ 13304.6 years
    1. 2−t/5730=0.2=15⟺t5730=log2⁡5=ln⁡5ln⁡2≈2.32
    2. t=5730⋅ln⁡5ln⁡2≈13300 years
    3. A little more than two half-lives: two would leave 25%, three 12.5%.
  270. 18. Functions of several variables · A function of two variables is a surface · QuizFor f(x,y)=x2y−y, what is f(2,3)?A. 922⋅3−3=12−3=9. Order matters: f(2,3) means x=2, y=3. With them swapped, f(3,2)=18−2=16.
  271. 18. Functions of several variables · Level curves: the contour map of a surface · QuizWhat do the level curves of f(x,y)=2x+y look like?A. Parallel straight lines y=c−2x2x+y=c is the line y=c−2x: slope −2 for every c, shifted up as c grows. Evenly spaced c give evenly spaced lines, because the surface is a tilted plane with the same steepness everywhere.
  272. 18. Functions of several variables · A partial derivative changes one variable at a time · QuizFor f(x,y)=x3y2+ex, what is fy?A. 2x3yTreat x as a number: x3y2 is a constant times y2, giving 2x3y, and ex is a constant, giving 0. Keeping ex is the usual slip: a term with no y in it does not change as y changes.
  273. 18. Functions of several variables · A partial derivative changes one variable at a time · ProblemLet f(x,y)=x2e3y. Find fx and fy, and the mixed partial fxy at (1,0).fx= 2*x*e^(3*y); fy= 3*x^2*e^(3*y); fxy(1,0)= 6
    1. fx=2xe3y,fy=x2⋅3e3y=3x2e3y
    2. fxy=∂∂y(2xe3y)=6xe3y,fxy(1,0)=6
    3. Differentiating fy with respect to x gives 6xe3y too, as Clairaut's theorem promises.
  274. 18. Functions of several variables · The gradient points straight uphill · Code challengeWrite gradient(f, x, y): the gradient (fx,fy) at (x, y), as a std::pair {fx, fy}, each by a central difference with h = 1e-5.One solution:
    Solution
    #include <functional>#include <utility>
    using F2 = std::function<double(double, double)>;
    std::pair<double, double> gradient(F2 f, double x, double y) {  double h = 1e-5;  double fx = (f(x + h, y) - f(x - h, y)) / (2 * h);  double fy = (f(x, y + h) - f(x, y - h)) / (2 * h);  return {fx, fy};}
    Each partial derivative moves only its own variable and holds the other fixed, exactly as in the definition. This is how you check a hand-computed gradient, and it costs 4 evaluations of f in two dimensions; with a million variables (a neural network) it would cost two million, which is why the mastery module comes back to automatic differentiation.
  275. 18. Functions of several variables · Directional derivatives: the slope in any direction · QuizFor f=x2+3y at (1,2), someone uses the direction (3,4) as it is and gets Df=18. What went wrong?A. (3,4) has length 5; dividing by it gives the right answer, 3.6The formula needs a unit vector. (3,4) has length 9+16=5, so the answer comes out 5 times too big. Divide by the length first: 𝐮=(35,45), giving 3.6. For comparison, the fastest rate here is |∇f|=13≈3.61, so this direction is almost straight uphill.
  276. 18. Functions of several variables · Directional derivatives: the slope in any direction · ProblemLet f(x,y)=x2y+y3. At the point (1,2), find the rate of change of f in the direction towards the point (4,6), and the fastest rate of change there.Towards (4,6) 64/5 ≈ 12.8; Fastest rate sqrt(185) ≈ 13.6015
    1. ∇f=(2xy, x2+3y2)=(4,13) at (1,2),𝐮=(3,4)5
    2. D𝐮f=4⋅3+13⋅45=645=12.8,|∇f|=16+169=185≈13.6
  277. 18. Functions of several variables · The tangent plane is the best flat approximation · QuizA box's volume is V=xyz. At x=2, y=3, z=4 (metres), about how much does V change if x grows by 0.01 m and the rest stay fixed?A. 0.12 m³Vx=yz=12, so ΔV≈12×0.01=0.12 m³. The partial derivative is the exchange rate between a small change in one input and the change in the output. 0.24 is the volume itself, 24, times 0.01: a size, not a rate; 0.08 uses xz=8, the partial in y instead of x.
  278. 18. Functions of several variables · The tangent plane is the best flat approximation · ProblemFind the tangent plane to f(x,y)=x2+y2 at (3,4), and use it to estimate f(3.1,3.9).z= 5 + 0.6*(x - 3) + 0.8*(y - 4); f(3.1,3.9)≈ 4.98
    1. f(3,4)=5,fx(3,4)=35,fy(3,4)=45
    2. z=5+0.6(x−3)+0.8(y−4),f(3.1,3.9)≈5+0.06−0.08=4.98
    3. The exact value is 24.82≈4.982.
  279. 18. Functions of several variables · Critical points: minima, maxima and saddles · QuizClassify the critical point of f=x2−y2 at the origin.A. A saddlefxx=2, fyy=−2, fxy=0, so D=−4<0: a saddle. Along the x-axis f=x2 curves up; along the y-axis f=−y2 curves down. Checking only fxx>0 and calling it a minimum is the classic mistake: one direction is not enough.
  280. 18. Functions of several variables · Lagrange multipliers: the best point on a constraint · QuizWhat is the largest value of x+y on the circle x2+y2=2?A. 2∇(x+y)=(1,1) and ∇(x2+y2)=(2x,2y). Parallel means 1=2λx and 1=2λy, so x=y; then 2x2=2, x=±1. At (1,1), x+y=2 (the maximum); at (−1,−1), −2 (the minimum). The method finds both; compare the values to tell which is which.
  281. 18. Functions of several variables · Gradient descent: walk downhill · Code challengeWrite descend(grad, start, eta, steps): run gradient descent from start for steps steps, where grad({x, y}) returns the gradient as {gx, gy}, and return the final point. A point is a Vec, a std::pair<double, double>.One solution:
    Solution
    #include <functional>#include <utility>
    using Vec = std::pair<double, double>;
    Vec descend(std::function<Vec(Vec)> grad, Vec start,            double eta, int steps) {  auto [x, y] = start;  for (int i = 0; i < steps; i++) {    auto [gx, gy] = grad({x, y});    x -= eta * gx;    y -= eta * gy;  }  return {x, y};}
    Compute the whole gradient at the current point, then move both coordinates; updating x first and then using the new x for y's gradient is a different (though here harmless) method. In y each step multiplies the distance to −1 by 1−4η: for η=0.1 that is 0.6, which shrinks, and for η=0.6 it is −1.4, which grows. The steepest direction sets the largest safe learning rate, η<2L where L is the largest curvature: here 24=0.5.
  282. 18. Functions of several variables · Practice problems · ProblemFind the critical point of f(x,y)=x2+xy+y2−3x, classify it with the second derivative test, and give the value of f there.x= 2; y= -1; The value of f -3
    1. From fy=0, x=−2y. Then fx=−4y+y−3=0 gives y=−1, and x=2.
    2. fxx=2, fyy=2, fxy=1⇒D=4−1=3>0 and fxx>0: a minimum
    3. f(2,−1)=4−2+1−6=−3
  283. 18. Functions of several variables · Practice problems · ProblemA farmer has 400 m of fence for a rectangular field along a straight river; the side on the river needs no fence. With x the two sides at right angles to the river and y the side parallel to it, maximise the area xy subject to 2x+y=400.x= 100 m; y= 200 m; Largest area 20000 m²
    1. (y,x)=λ(2,1)⇒y=2λ, x=λ⇒y=2x
    2. 2x+2x=400⇒x=100m,y=200m,area=20000m2
    3. The side along the river is twice as long as each of the other two.
  284. 18. Functions of several variables · Practice problems · ProblemTake two steps of gradient descent on f(x,y)=(x−1)2+3y2 from (3,1), with learning rate η=0.1. Where are you after two steps?x= 2.28; y= 0.16
    1. ∇f(3,1)=(4,6)⇒(x1,y1)=(3−0.4, 1−0.6)=(2.6, 0.4)∇f(2.6,0.4)=(3.2,2.4)⇒(x2,y2)=(2.6−0.32, 0.4−0.24)=(2.28, 0.16)
    2. Each step multiplies the distance to the minimum, (1,0), by 0.8 in x and by 0.4 in y: the steeper direction closes in faster.
  285. 19. Multiple integrals and vector calculus · A double integral adds up over a region · QuizWhat is ∫01∫03xydxdy?A. 94The inner integral, in x from 0 to 3, is y⋅92. Then ∫0192ydy=92⋅12=94. When f is a product g(x)h(y) over a rectangle, the double integral is just the product ∫g⋅∫h=92⋅12. Reading the limits in the right order matters: the inner dx goes with the inner limits.
  286. 19. Multiple integrals and vector calculus · A double integral adds up over a region · ProblemEvaluate ∬R(x+2y)dA over the rectangle R where 0≤x≤3 and 1≤y≤2.27/2 ≈ 13.5
    1. ∫12(x+2y)dy=[xy+y2]12=(2x+4)−(x+1)=x+3
    2. ∫03(x+3)dx=92+9=272
  287. 19. Multiple integrals and vector calculus · Over a curved region, the inner limits are functions · QuizWritten with x inside instead, ∫01∫x2xfdydx=∫01∫??fdxdy. What are the inner limits?A. From x=y to x=yFix a height y between 0 and 1. The line y=x gives the left edge, x=y; the parabola y=x2 gives the right edge, x=y (for 0<y<1, y>y). Draw the region first: reversing the curves without a picture is how the limits end up backwards.
  288. 19. Multiple integrals and vector calculus · Over a curved region, the inner limits are functions · Problemey2 has no elementary antiderivative, so ∫01∫x1ey2dydx cannot be done in this order. Swap the order of integration and evaluate it exactly.(e - 1)/2 ≈ 0.859141
    1. The region is the triangle with corners (0,0), (0,1) and (1,1). For each y in [0,1], x runs from 0 to y.
    2. ∫01∫0yey2dxdy=∫01yey2dy=[12ey2]01=e−12≈0.859
  289. 19. Multiple integrals and vector calculus · Triple integrals add up over a solid · QuizA unit cube [0,1]3 has density ρ=x kg/m³. What is its mass?A. 12 kg∫01∫01∫01xdzdydx=12⋅1⋅1=12. The density goes from 0 on one face to 1 on the opposite face, so on average it is 12, times a volume of 1 m³. Mass is the average density times the volume, which is what the triple integral computes.
  290. 19. Multiple integrals and vector calculus · Polar coordinates: dA becomes r dr dθ · Code challengeWrite polarIntegral(f, R, n): the integral of f(x, y) over the disc of radius R, using an n by n grid of midpoints in r (from 0 to R) and θ (from 0 to 2π), each weighted by r · Δr · Δθ.One solution:
    Solution
    #include <cmath>#include <functional>#include <numbers>
    using F2 = std::function<double(double, double)>;
    double polarIntegral(F2 f, double R, int n) {  double dr = R / n;  double dth = 2 * std::numbers::pi / n;  double sum = 0;  for (int i = 0; i < n; i++) {    double r = (i + 0.5) * dr;    for (int j = 0; j < n; j++) {      double th = (j + 0.5) * dth;      sum += f(r * std::cos(th), r * std::sin(th)) * r * dr * dth;    }  }  return sum;}
    The grid is rectangular in (r,θ) but each cell is a curved patch of area about rΔrΔθ, so every sample is weighted by its own r. Leave out r and the disc of radius 2 still gives 4π, by coincidence: R⋅2π equals πR2 only when R=2. The other two tests expose it. The midpoint in r makes the area test exact, because the integrand r is a straight line.
  291. 19. Multiple integrals and vector calculus · Polar coordinates: dA becomes r dr dθ · ProblemA circular plate of radius 3 m has density ρ=4−r kg/m², where r is the distance from its centre in metres. Find its mass.Mass 18*pi ≈ 56.5487 kg
    1. M=∫02π∫03(4−r)rdrdθ=2π[2r2−r33]03=2π(18−9)=18π≈56.5kg
    2. Without the extra r you would get 2π⋅7.5=15π, which gives the big outer rings no more weight than the small inner ones.
  292. 19. Multiple integrals and vector calculus · The Jacobian: how a change of variables stretches area · QuizThe map x=2u, y=3v stretches the plane. What is its Jacobian, and what does it mean?A. 6: every region's area is multiplied by 6J=det⁡(2003)=6. A unit square becomes a 2 by 3 rectangle, of area 6. So the ellipse x24+y29≤1, the image of the unit disc, has area 6π. Adding the stretches instead of multiplying them is the slip.
  293. 19. Multiple integrals and vector calculus · A vector field puts an arrow at every point · QuizWhich field is the gradient of φ=xy?A. (y,x)∇(xy)=(φx,φy)=(y,x). So (y,x) is conservative, with potential xy. The rotation field (−y,x) is not the gradient of anything: if it were, φx=−y and φy=x would give φxy=−1 and φyx=1, breaking Clairaut's theorem.
  294. 19. Multiple integrals and vector calculus · Line integrals add up a field along a path · Code challengeWrite lineIntegral(F, r, a, b, n): split [a, b] into n steps; for each, take the step dr = r(t1) - r(t0) and add F(midpoint) · dr, where the midpoint is halfway between r(t0) and r(t1). A point or a vector is a Vec, a std::pair<double, double>.One solution:
    Solution
    #include <functional>#include <utility>
    using Vec = std::pair<double, double>;
    double lineIntegral(std::function<Vec(double, double)> F,                    std::function<Vec(double)> r,                    double a, double b, int n) {  double h = (b - a) / n;  double sum = 0;  for (int i = 0; i < n; i++) {    auto [x0, y0] = r(a + i * h);    auto [x1, y1] = r(a + (i + 1) * h);    auto [P, Q] = F((x0 + x1) / 2, (y0 + y1) / 2);    sum += P * (x1 - x0) + Q * (y1 - y0);  }  return sum;}
    Each term is the dot product 𝐅⋅Δ𝐫=PΔx+QΔy, the work for one small step, so you never need 𝐫′(t). The last test walks a parabola, not a straight line, and still gets φ(1,1)−φ(0,0)=1: for a gradient field only the ends matter. Using the field at the start of each step instead of the middle works too, but converges more slowly.
  295. 19. Multiple integrals and vector calculus · Divergence measures spreading, curl measures spinning · QuizFor 𝐅=(x2,xy), what are the divergence and the curl?A. Divergence 3x, curl yP=x2, Q=xy. Divergence Px+Qy=2x+x=3x. Curl Qx−Py=y−0=y. Mixing up which variable goes with which component is the usual slip: divergence pairs P with x; curl pairs each component with the other variable. Any gradient field has curl 0, by Clairaut.
  296. 19. Multiple integrals and vector calculus · Green's theorem: the boundary knows what is inside · QuizUsing Green's theorem, what is ∮C(−y3dx+x3dy) anticlockwise round the unit circle? (Hint: in polar, ∬(x2+y2)dA over the unit disc is π2.)A. 3π2Qx−Py=3x2−(−3y2)=3(x2+y2). So the line integral is 3∬(x2+y2)dA=3⋅π2=3π2. Green turned a fiddly integral round a curve into an easy one over a disc. The sign of Py is where it usually goes wrong: −(−3y2)=+3y2.
  297. 19. Multiple integrals and vector calculus · Stokes' and the divergence theorem: one idea in every dimension · QuizWhat is the flux of 𝐅=(x,y,z) out of the unit sphere?A. 4π∇⋅𝐅=1+1+1=3, so by the divergence theorem the flux is 3× the volume 43π=4π. Directly: on the unit sphere 𝐅 is the outward unit normal, so 𝐅⋅𝐧=1 and the flux is the surface area, 4π. Two routes, one number. 4π3 forgets the divergence is 3, not 1.
  298. 19. Multiple integrals and vector calculus · Practice problems · ProblemFind ∫C𝐅⋅d𝐫 for 𝐅=(x2,xy) along the parabola 𝐫(t)=(t,t2), 0≤t≤1.11/15 ≈ 0.733333
    1. 𝐅(𝐫(t))=(t2, t⋅t2)=(t2,t3),𝐫′(t)=(1,2t)
    2. ∫01(t2+2t4)dt=13+25=1115
    3. This field is not a gradient (its curl is y, not 0), so another path between the same ends could give a different answer.
  299. 19. Multiple integrals and vector calculus · Practice problems · ProblemUse Green's theorem to find ∮C(2ydx+5xdy) anticlockwise round the circle of radius 3 centred at the origin.27*pi ≈ 84.823
    1. ∮C2ydx+5xdy=∬D(5−2)dA=3⋅π⋅32=27π≈84.8
  300. 19. Multiple integrals and vector calculus · Practice problems · ProblemFor 𝐅=(xy, y2−x), find the divergence and the curl at the point (2,1).Divergence 3; Curl -3
    1. Px+Qy=y+2y=3y=3,Qx−Py=−1−x=−3
    2. At (2,1) the flow spreads out (a source) and turns clockwise (negative curl).
  301. 19. Multiple integrals and vector calculus · Practice problems · ProblemThe field 𝐅=(2xy+1, x2+3y2) is conservative. Find its potential φ with φ(0,0)=0, and the work ∫C𝐅⋅d𝐫 along any path from (0,0) to (1,2).φ(x,y)= x^2*y + x + y^3; Work 11
    1. φ=∫(2xy+1)dx=x2y+x+g(y),φy=x2+g′(y)=x2+3y2⇒g(y)=y3+C
    2. φ(0,0)=0 gives C=0, so φ=x2y+x+y3. By the gradient theorem, the work is φ(1,2)−φ(0,0)=2+1+8=11, whatever the path.
  302. 20. Rigour and the frontier · What a limit really means · QuizFor limx→2⁡(3x−1)=5 with ε=0.3, which values of δ work? Choose every one.A. δ=0.1; B. δ=0.05You need 3δ≤0.3, so δ≤0.1. Any smaller δ works too: once a strip is narrow enough, narrower strips are as well. δ=0.2 lets x=2.15 in, where f=5.45, outside the band. The definition asks for some δ, not the biggest one.
  303. 20. Rigour and the frontier · What a limit really means · ProblemFor limx→2⁡(5x+3)=13 with ε=0.01, what is the largest δ that works? And for limx→3⁡x2=9 with ε=0.07, what δ does the recipe δ=min⁡(1,ε7) (under Go deeper) give?Largest δ for 5x+3 0.002; δ for x2 0.01
    1. |f(x)−13|=5|x−2|<0.01 exactly when |x−2|<0.002, so δ=0.002; any bigger δ lets in an x whose value is outside the band.
    2. For x2: 0.077=0.01<1, so δ=0.01. Check: if |x−3|<0.01 then |x+3|<6.01, and |x2−9|<0.01⋅6.01<0.07.
  304. 20. Rigour and the frontier · The order of the quantifiers is the whole point · Put in orderPut the parts of the definition of limx→a⁡f(x)=L in order.1. For every ε>0 2. there is a δ>0 3. such that for all x with 0<|x−a|<δ 4. we have |f(x)−L|<εThe challenge (ε) comes first, then the response (δ), then the promise about every x in the strip, then the conclusion. Putting "there is a δ" before "for every ε" would demand one δ that works for all tolerances at once, which only constant functions near a satisfy.
  305. 20. Rigour and the frontier · The order of the quantifiers is the whole point · QuizWhich statement means "limx→a⁡f(x)≠L"?A. There is an ε>0 such that for every δ>0 some x with 0<|x−a|<δ has |f(x)−L|≥εNegating swaps each "for every" with "there is" and negates the end: ¬∀ε∃δ∀xP becomes ∃ε∀δ∃x¬P. The second option is far too strong: it says f is never close to L at all. One bad ε and one bad x per δ are enough.
  306. 20. Rigour and the frontier · The real numbers have no gaps · QuizWhich set has no least upper bound within the rational numbers?A. {x∈ℚ:x2<2}Its least upper bound would be 2, which is not rational; any rational upper bound can be beaten by a smaller one. The others have rational suprema: 2, 1 and 1. A supremum need not belong to the set (2 is not below 2), it only has to be the smallest bound.
  307. 20. Rigour and the frontier · A proof that is also an algorithm · Code challengeWrite bisect(f, a, b, tol): f(a) and f(b) have opposite signs. Halve the interval, keeping the half with a sign change, until it is shorter than tol; return its midpoint.One solution:
    Solution
    #include <functional>
    double bisect(std::function<double(double)> f,              double a, double b, double tol) {  double fa = f(a);  while (b - a > tol) {    double m = (a + b) / 2;    double fm = f(m);    if (fm * fa > 0) {  // the same sign      a = m;      fa = fm;    } else {      b = m;    }  }  return (a + b) / 2;}
    The invariant is "f changes sign on [a,b]"; each step keeps it and halves the length, so after k steps the root is pinned within b−a2k: about 40 steps for 12 digits. Comparing signs, rather than assuming f(a)<0, makes the third test pass. This is the IVT proof almost line for line, and it shows why completeness matters: the loop produces a sequence of nested intervals, and only completeness guarantees there is a number inside all of them.
  308. 20. Rigour and the frontier · A proof that is also an algorithm · ProblemBisection starts on [1,2], where x2−2 changes sign. How many halvings are needed before the interval is shorter than 10−6?Halvings 20
    1. 2−k<10−6⟺2k>106,219=524288<106<220=1048576
    2. So k=20. Each halving buys about 0.3 decimal digits, whatever the function: steady, but slower than Newton's method (module 8), which roughly doubles the digits each step once it is close to a root.
  309. 20. Rigour and the frontier · Uniform continuity: one δ for the whole interval · QuizWhich of these is uniformly continuous on the given set?A. sin⁡x on ℝ|sin⁡x−sin⁡y|≤|x−y| (by the mean value theorem, since |cos⁡|≤1), so δ=ε works everywhere. x2 gets ever steeper as x grows, so δ must shrink far out; 1x and sin⁡1x misbehave near 0. A bounded derivative is a quick test for uniform continuity.
  310. 20. Rigour and the frontier · The chain of theorems behind the fundamental theorem · Put in orderPut these in the order in which each is used to prove the next.1. Completeness of the real numbers 2. Extreme value theorem 3. Rolle's theorem 4. Mean value theorem 5. Fundamental theorem of calculusCompleteness guarantees a maximum and a minimum exist on [a,b]; Rolle locates a flat point between equal values; the MVT is Rolle applied to f minus its chord; the FTC follows by the MVT on each piece of a partition, as above. Without completeness the very first step fails, and with it the whole chain.
  311. 20. Rigour and the frontier · Beyond Riemann: measure and the Lebesgue integral · QuizWhy do the rational numbers in [0,1] have measure 0?A. They can be listed q1,q2,…; cover qn by an interval of length ε2n, total at most ε, for any εRationals are countable: they can be put in a list. Covering the $n$th by an interval of length ε2n uses total length ε(12+14+…)=ε, which can be as small as you like. They are infinitely many and dense, which is what makes the result surprising.
  312. 20. Rigour and the frontier · Calculus of variations: the best function · QuizApplied to L=12mx˙2−12kx2 (with t in place of x as the variable and x as the function), what does the Euler–Lagrange equation give?A. mx¨=−kx, the spring equation∂L∂x=−kx and ∂L∂x˙=mx˙, whose time derivative is mx¨. So −kx−mx¨=0: Newton's law for a spring. This L, kinetic minus potential energy, is the Lagrangian, and "nature makes ∫Ldt stationary" (the principle of least action) reproduces all of classical mechanics.
  313. 20. Rigour and the frontier · Automatic differentiation: the chain rule as code · Code challengeA Dual carries a value v and a derivative d. add is done. Finish mul (the product rule) and sin (the chain rule), so that f(variable(x)).d is exactly f'(x).One solution:
    Solution
    #include <cmath>
    struct Dual {  double v;  // the value  double d;  // its derivative};Dual variable(double x) { return {x, 1}; }Dual constant(double c) { return {c, 0}; }
    Dual add(Dual a, Dual b) { return {a.v + b.v, a.d + b.d}; }
    Dual mul(Dual a, Dual b) {  return {a.v * b.v, a.d * b.v + a.v * b.d};}
    Dual sin(Dual a) { return {std::sin(a.v), std::cos(a.v) * a.d}; }
    The derivative part of a product is a′b+ab′, and of sin⁡ of something is cos⁡ of the value times the inner derivative, never cos⁡ of the derivative. With those two rules every composition differentiates itself, exactly, to the last digit: no h, no rounding trade-off. C++ lets you go one step further and name them operator* and operator+, so that x * sin(x) on Duals reads like the maths; C++ autodiff libraries work that way. Machine learning libraries do the same, in reverse mode, for tensors with billions of entries.
  314. 20. Rigour and the frontier · Automatic differentiation: the chain rule as code · ProblemPush the dual number π2+ε through f(x)=x2sin⁡x by hand, with ε2=0. What are the value part and the ε part of the result?Value part pi^2/4 ≈ 2.4674; ε part pi ≈ 3.14159
    1. x2=π24+πε,sin⁡x=1+0⋅ε
    2. x2sin⁡x=(π24+πε)(1+0⋅ε)=π24+πε
    3. The ε part is f′(π2). By the product rule, f′(x)=2xsin⁡x+x2cos⁡x, which is π+0=π there.
  315. 20. Rigour and the frontier · Problems that mix everything · QuizWhat is limx→0⁡sin⁡x−xx3?A. −16Taylor: sin⁡x=x−x36+x5120−…, so sin⁡x−xx3=−16+x2120−⋯→−16. L'Hôpital three times gives the same, more slowly. Series are often the fastest way through a 00.
  316. 20. Rigour and the frontier · Problems that mix everything · QuizWhat is ∫0∞xe−xdx?A. 1By parts, ∫0Rxe−xdx=[−xe−x]0R+∫0Re−xdx=−Re−R+1−e−R. Since exponentials beat powers, Re−R→0, leaving 1. It is the mean waiting time for the exponential density with λ=1, and it is Γ(2)=1!: the gamma function extends factorials this way.
  317. 20. Rigour and the frontier · Problems that mix everything · QuizWhat is ddx∫0x2e−t2dt?A. 2xe−x4With G(u)=∫0ue−t2dt, the fundamental theorem gives G′(u)=e−u2, and the integral is G(x2). The chain rule: G′(x2)⋅2x=2xe−x4. You never need the antiderivative, which does not exist in elementary form anyway.
  318. 20. Rigour and the frontier · Problems that mix everything · QuizWhat is ∑n=1∞n2n? (Hint: differentiate ∑xn=11−x and multiply by x.)A. 2Differentiating gives ∑nxn−1=1(1−x)2, so ∑nxn=x(1−x)2 for |x|<1. At x=12: 1/21/4=2. The ratio test said it converges; power series tell you to what.
  319. 20. Rigour and the frontier · Practice problems that mix everything · ProblemEvaluate ∫0∞x2e−xdx.2
    1. ∫0Rx2e−xdx=[−x2e−x]0R+2∫0Rxe−xdx
    2. As R→∞ the first term goes to 0, and ∫0∞xe−xdx=1 (the problem set above), so the integral is 2⋅1=2. It is Γ(3)=2!.
  320. 20. Rigour and the frontier · Practice problems that mix everything · ProblemFor x>0, find ddx∫xx3ln⁡tdt.The derivative 9*x^2*ln(x) - ln(x)
    1. ddx∫1x3ln⁡tdt=ln⁡(x3)⋅3x2=9x2ln⁡x,ddx∫1xln⁡tdt=ln⁡x
    2. ddx∫xx3ln⁡tdt=9x2ln⁡x−ln⁡x=(9x2−1)ln⁡x
  321. 20. Rigour and the frontier · Practice problems that mix everything · ProblemWhich a>0 makes ∫0a(4x−x3)dx as large as possible, and what is that largest value?a= 2; Largest value 4
    1. A′(a)=4a−a3=a(2−a)(2+a), positive for 0<a<2 and negative after: A is largest at a=2, where the curve dips below the axis.
    2. A(2)=[2x2−x44]02=8−4=4
  322. 20. Rigour and the frontier · More practice problems that mix everything · ProblemThe solution of y′=x+y2 with y(0)=1 has no simple formula. Find y′(0) and y′′(0) from the equation, and use the Taylor polynomial T2 at 0 to estimate y(0.1).y′(0)= 1; y′′(0)= 3; y(0.1)≈ 1.115
    1. y′(0)=0+12=1,y′′=1+2yy′⇒y′′(0)=1+2⋅1⋅1=3
    2. T2(x)=1+x+32x2,y(0.1)≈1+0.1+0.015=1.115
    3. A careful numerical solution gives 1.1165. The next term, y′′′(0)3!x3=86(0.1)3≈0.0013, accounts for most of the difference.
  323. 20. Rigour and the frontier · More practice problems that mix everything · ProblemWhat is the average value of f(x,y)=x2+y2 over the unit disc x2+y2≤1?1/2 ≈ 0.5
    1. ∬D(x2+y2)dA=∫02π∫01r2⋅rdrdθ=2π⋅14=π2
    2. f‾=π/2π=12
    3. Not the 13 you get by averaging r2 over 0≤r≤1: the outer rings are bigger, so they count for more.