TExcelTAD
Home › Calculus in Depth › Calculus in Depth Exercises

Calculus in Depth Exercises

Ten exercises that run across the whole course: precise limits, Newton's method, Simpson's rule, a logistic model, polar area, series error bounds, curvature, Lagrange multipliers, a Monte Carlo triple integral and the fundamental theorem for line integrals. Each exercise has a starter program that runs but does not do the job yet. Replace the TODO parts, press ▶ Run, and compare your result with the expected output. The output block under each starter shows what the unfinished starter prints.

Tip: Every exercise has an exact answer you can find on paper. Work it out first, then let the code confirm it. When the two disagree, one of them has a bug, and finding out which is the real exercise.

# Exercise Practises
1 Find a δ\delta for an ε\varepsilon the precise definition of a limit
2 Where cos⁡x=x\cos x = x Newton's method
3 Simpson's rule for e−x2e^{-x^2} approximate integration
4 A logistic population by Euler population models, Euler's method
5 The area of a cardioid areas in polar coordinates
6 How many terms for ln⁡1.5\ln 1.5? alternating series
7 Curvature of a helix arc length and curvature
8 The biggest box under a plane Lagrange multipliers
9 Two crossing cylinders triple integrals, Monte Carlo
10 Work done by a gradient field the fundamental theorem for line integrals

Exercise 1: Find a δ\delta for an ε\varepsilon

For lim⁡x2=9\lim x^2 = 9 as x→3x \to 3 and ε=0.01\varepsilon = 0.01, find the largest δ\delta such that ∣x−3∣<δ\lvert x - 3 \rvert < \delta guarantees ∣x2−9∣<ε\lvert x^2 - 9 \rvert < \varepsilon. The condition ∣x2−9∣<ε\lvert x^2 - 9 \rvert < \varepsilon means 9−ε<x<9+ε\sqrt{9 - \varepsilon} < x < \sqrt{9 + \varepsilon} (for xx near 3), so δ\delta is the smaller distance from 3 to the ends of that interval. The starter leaves both ends at 3, which gives δ=0\delta = 0, and the grid check passes trivially because every test point is 3.

python · try it yourself
import math

eps, a = 0.01, 3
left = a      # TODO: left end of the interval where |x^2 - 9| < eps
right = a     # TODO: right end
delta = min(a - left, right - a)
print(f"delta = {delta:.7f}")
xs = [a - delta + 2 * delta * k / 10000 for k in range(1, 10000)]
print("all inside:", all(abs(x * x - 9) < eps for x in xs))
delta = 0.0000000
all inside: True

Expected output when solved:

text
delta = 0.0016662
all inside: True

Hint: The right end is closer to 3, because x2x^2 grows faster to the right of 3 than it falls to the left. Compare with the rough rule δ≈ε/∣f′(3)∣=0.01/6\delta \approx \varepsilon / \lvert f'(3) \rvert = 0.01 / 6.

Show solution
python · try it yourself
import math

eps, a = 0.01, 3
left = math.sqrt(9 - eps)
right = math.sqrt(9 + eps)
delta = min(a - left, right - a)
print(f"delta = {delta:.7f}")
xs = [a - delta + 2 * delta * k / 10000 for k in range(1, 10000)]
print("all inside:", all(abs(x * x - 9) < eps for x in xs))
delta = 0.0016662
all inside: True

δ=9.01−3≈0.0016662\delta = \sqrt{9.01} - 3 \approx 0.0016662, slightly less than ε/6≈0.0016667\varepsilon/6 \approx 0.0016667.

Exercise 2: Where cos⁡x=x\cos x = x

Solve cos⁡x=x\cos x = x with Newton's method, xn+1=xn−f(xn)/f′(xn)x_{n+1} = x_n - f(x_n)/f'(x_n) for f(x)=cos⁡x−xf(x) = \cos x - x, starting at x0=1x_0 = 1. The starter's derivative is a placeholder 0.

python · try it yourself
import math

f = lambda x: math.cos(x) - x
df = lambda x: 0.0          # TODO: the derivative of cos(x) - x
x = 1.0
for i in range(5):
    if df(x) == 0:
        print("f'(x) = 0, cannot take a Newton step")
        break
    x = x - f(x) / df(x)
    print(i + 1, f"{x:.12f}")
f'(x) = 0, cannot take a Newton step

Expected output when solved:

text
1 0.750363867840
2 0.739112890911
3 0.739085133385
4 0.739085133215
5 0.739085133215

Hint: The number of correct digits roughly doubles every step: Newton's method converges quadratically near a simple root.

Show solution
python · try it yourself
import math

f = lambda x: math.cos(x) - x
df = lambda x: -math.sin(x) - 1
x = 1.0
for i in range(5):
    if df(x) == 0:
        print("f'(x) = 0, cannot take a Newton step")
        break
    x = x - f(x) / df(x)
    print(i + 1, f"{x:.12f}")
1 0.750363867840
2 0.739112890911
3 0.739085133385
4 0.739085133215
5 0.739085133215

Exercise 3: Simpson's rule for e−x2e^{-x^2}

∫01e−x2 dx\int_0^1 e^{-x^2}\,dx has no elementary antiderivative; its exact value is (π/2)⋅erf⁡(1)(\sqrt{\pi}/2) \cdot \operatorname{erf}(1). Complete Simpson's rule with n=10n = 10: h3[f(x0)+4f(x1)+2f(x2)+4f(x3)+⋯+4f(xn−1)+f(xn)]\frac{h}{3}[f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + \cdots + 4f(x_{n-1}) + f(x_n)].

python · try it yourself
import math

def simpson(f, a, b, n):
    h = (b - a) / n
    total = f(a) + f(b)
    # TODO: add 4*f(x_k) for odd k and 2*f(x_k) for even k, 0 < k < n
    return total * h / 3

approx = simpson(lambda x: math.exp(-x * x), 0, 1, 10)
exact = math.sqrt(math.pi) / 2 * math.erf(1)
print(f"Simpson n=10: {approx:.10f}")
print(f"exact       : {exact:.10f}")
print(f"error       : {abs(approx - exact):.2e}")
Simpson n=10: 0.0455959814
exact       : 0.7468241328
error       : 7.01e-01

Expected output when solved:

text
Simpson n=10: 0.7468249483
exact       : 0.7468241328
error       : 8.15e-07

Hint: Simpson's error is O(h4)O(h^4), so even with h=0.1h = 0.1 the error should be below 10−610^{-6}.

Show solution
python · try it yourself
import math

def simpson(f, a, b, n):
    h = (b - a) / n
    total = f(a) + f(b)
    for k in range(1, n):
        total += (4 if k % 2 else 2) * f(a + k * h)
    return total * h / 3

approx = simpson(lambda x: math.exp(-x * x), 0, 1, 10)
exact = math.sqrt(math.pi) / 2 * math.erf(1)
print(f"Simpson n=10: {approx:.10f}")
print(f"exact       : {exact:.10f}")
print(f"error       : {abs(approx - exact):.2e}")
Simpson n=10: 0.7468249483
exact       : 0.7468241328
error       : 8.15e-07

Exercise 4: A logistic population by Euler

A population follows P′=0.5⋅P⋅(1−P/1000)P' = 0.5 \cdot P \cdot (1 - P/1000) with P(0)=100P(0) = 100. Simulate it with Euler's method (h=0.1h = 0.1) up to t=10t = 10 and compare with the exact solution P(t)=10001+9e−0.5tP(t) = \frac{1000}{1 + 9e^{-0.5t}}.

python · try it yourself
import math

r, K, P0, h, T = 0.5, 1000, 100, 0.1, 10
P = P0
for step in range(round(T / h)):
    dP = 0.0                 # TODO: the logistic rate r * P * (1 - P / K)
    P += h * dP
exact = K / (1 + (K / P0 - 1) * math.exp(-r * T))
print(f"Euler P(10) = {P:.3f}")
print(f"exact P(10) = {exact:.3f}")
Euler P(10) = 100.000
exact P(10) = 942.826

Expected output when solved:

text
Euler P(10) = 943.527
exact P(10) = 942.826

Hint: Euler's method overshoots slightly during the fast-growth phase. Halving hh should roughly halve the error.

Show solution
python · try it yourself
import math

r, K, P0, h, T = 0.5, 1000, 100, 0.1, 10
P = P0
for step in range(round(T / h)):
    dP = r * P * (1 - P / K)
    P += h * dP
exact = K / (1 + (K / P0 - 1) * math.exp(-r * T))
print(f"Euler P(10) = {P:.3f}")
print(f"exact P(10) = {exact:.3f}")
Euler P(10) = 943.527
exact P(10) = 942.826

Exercise 5: The area of a cardioid

Find the area enclosed by r=1+cos⁡θr = 1 + \cos\theta numerically with A=12∫02πr2 dθA = \frac{1}{2}\int_0^{2\pi} r^2\,d\theta (midpoint sums, 1000 sectors) and compare with the exact 3π/23\pi/2.

python · try it yourself
import math

r = lambda th: 1 + math.cos(th)
n = 1000
dth = 2 * math.pi / n
area = 0.0
for k in range(n):
    th = (k + 0.5) * dth
    # TODO: add the area of the thin sector, (1/2) * r(th)^2 * dth
print(f"area ≈ {area:.6f}   3π/2 = {3 * math.pi / 2:.6f}")
area ≈ 0.000000   3π/2 = 4.712389

Expected output when solved:

text
area ≈ 4.712389   3π/2 = 4.712389

Hint: By hand: (1+cos⁡θ)2=1+2cos⁡θ+cos⁡2θ(1 + \cos\theta)^2 = 1 + 2\cos\theta + \cos^2\theta, and over a full turn the cos⁡θ\cos\theta term integrates to 0 while cos⁡2θ\cos^2\theta integrates to π\pi.

Show solution
python · try it yourself
import math

r = lambda th: 1 + math.cos(th)
n = 1000
dth = 2 * math.pi / n
area = 0.0
for k in range(n):
    th = (k + 0.5) * dth
    area += 0.5 * r(th) ** 2 * dth
print(f"area ≈ {area:.6f}   3π/2 = {3 * math.pi / 2:.6f}")
area ≈ 4.712389   3π/2 = 4.712389

12(2π+0+π)=3π/2\frac{1}{2}(2\pi + 0 + \pi) = 3\pi/2. The midpoint sum of a smooth periodic function over a full period is extremely accurate, so all six decimals agree.

Exercise 6: How many terms for ln⁡1.5\ln 1.5?

ln⁡(1+x)=x−x22+x33−⋯\ln(1 + x) = x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots For x=0.5x = 0.5 the series alternates with decreasing terms, so the error after stopping is less than the first omitted term. Add terms until the next term would be below 10−610^{-6}, and report how many you used.

python · try it yourself
import math

x, tol = 0.5, 1e-6
terms, s = 0, 0.0
# TODO: repeat: add the next term (-1)**(k+1) * x**k / k (k = terms + 1),
#       and stop once the following term x**(k+1) / (k+1) is below tol
print(f"terms used: {terms}")
print(f"sum = {s:.8f}   ln 1.5 = {math.log(1.5):.8f}   error = {abs(s - math.log(1.5)):.1e}")
terms used: 0
sum = 0.00000000   ln 1.5 = 0.40546511   error = 4.1e-01

Expected output when solved:

text
terms used: 15
sum = 0.40546576   ln 1.5 = 0.40546511   error = 6.5e-07

Hint: You need 0.5k+1/(k+1)<10−60.5^{k+1}/(k+1) < 10^{-6}; at k+1=16k + 1 = 16 it is 0.516/16≈9.5×10−70.5^{16}/16 \approx 9.5 \times 10^{-7}.

Show solution
python · try it yourself
import math

x, tol = 0.5, 1e-6
terms, s = 0, 0.0
while True:
    terms += 1
    s += (-1) ** (terms + 1) * x ** terms / terms
    if x ** (terms + 1) / (terms + 1) < tol:
        break
print(f"terms used: {terms}")
print(f"sum = {s:.8f}   ln 1.5 = {math.log(1.5):.8f}   error = {abs(s - math.log(1.5)):.1e}")
terms used: 15
sum = 0.40546576   ln 1.5 = 0.40546511   error = 6.5e-07

The actual error is below the bound 9.5×10−79.5 \times 10^{-7}, as the alternating series estimate promises.

Exercise 7: Curvature of a helix

For the helix r(t)=⟨2cos⁡t,2sin⁡t,t⟩r(t) = \langle 2\cos t, 2\sin t, t \rangle compute κ=∣r′×r′′∣/∣r′∣3\kappa = \lvert r' \times r'' \rvert / \lvert r' \rvert^3 at t=1.3t = 1.3. The starter has r′′=0r'' = 0, so it reports zero curvature, as for a straight line.

python · try it yourself
import numpy as np

t = 1.3
r1 = np.array([-2 * np.sin(t), 2 * np.cos(t), 1.0])    # r'(t)
r2 = np.array([0.0, 0.0, 0.0])                         # TODO: r''(t)
kappa = np.linalg.norm(np.cross(r1, r2)) / np.linalg.norm(r1) ** 3
print(f"curvature at t = {t}: {kappa:.6f}")
curvature at t = 1.3: 0.000000

Expected output when solved:

text
curvature at t = 1.3: 0.400000

Hint: For the helix ⟨acos⁡t,asin⁡t,bt⟩\langle a\cos t, a\sin t, bt \rangle the curvature is constant, a/(a2+b2)a/(a^2 + b^2).

Show solution
python · try it yourself
import numpy as np

t = 1.3
r1 = np.array([-2 * np.sin(t), 2 * np.cos(t), 1.0])    # r'(t)
r2 = np.array([-2 * np.cos(t), -2 * np.sin(t), 0.0])   # r''(t)
kappa = np.linalg.norm(np.cross(r1, r2)) / np.linalg.norm(r1) ** 3
print(f"curvature at t = {t}: {kappa:.6f}")
curvature at t = 1.3: 0.400000

∣r′∣=5\lvert r' \rvert = \sqrt{5} and ∣r′×r′′∣=∣⟨2sin⁡t,−2cos⁡t,4⟩∣=20\lvert r' \times r'' \rvert = \lvert \langle 2\sin t, -2\cos t, 4 \rangle \rvert = \sqrt{20}, so κ=20/55=2/5\kappa = \sqrt{20} / 5\sqrt{5} = 2/5 at every tt.

Exercise 8: The biggest box under a plane

A box has one corner at the origin and the opposite corner (x,y,z)(x, y, z) on the plane x+2y+3z=18x + 2y + 3z = 18 (x,y,z>0x, y, z > 0). Maximise its volume V=xyzV = xyz with Lagrange multipliers: ∇V=λ∇g\nabla V = \lambda \nabla g and g=0g = 0, where g=x+2y+3z−18g = x + 2y + 3z - 18.

python · try it yourself
import sympy as sp

x, y, z, lam = sp.symbols('x y z lam', positive=True)
V = x * y * z
g = x + 2*y + 3*z - 18
equations = []     # TODO: the three equations dV/dx = lam*dg/dx, ... and g = 0
sols = sp.solve(equations, [x, y, z, lam], dict=True) if equations else []
print("solutions:", sols)
for s in sols:
    print("V =", V.subs(s))
solutions: []

Expected output when solved:

text
solutions: [{lam: 6, x: 6, y: 3, z: 2}]
V = 36

Hint: yz=λyz = \lambda, xz=2λxz = 2\lambda, xy=3λxy = 3\lambda. Dividing pairs gives x=2yx = 2y and x=3zx = 3z: the three terms x,2y,3zx, 2y, 3z are equal.

Show solution
python · try it yourself
import sympy as sp

x, y, z, lam = sp.symbols('x y z lam', positive=True)
V = x * y * z
g = x + 2*y + 3*z - 18
equations = [sp.Eq(sp.diff(V, v), lam * sp.diff(g, v)) for v in (x, y, z)] + [sp.Eq(g, 0)]
sols = sp.solve(equations, [x, y, z, lam], dict=True) if equations else []
print("solutions:", sols)
for s in sols:
    print("V =", V.subs(s))
solutions: [{lam: 6, x: 6, y: 3, z: 2}]
V = 36

x=2y=3zx = 2y = 3z and x+2y+3z=18x + 2y + 3z = 18 give x=6x = 6, y=3y = 3, z=2z = 2 and V=36V = 36.

Exercise 9: Two crossing cylinders

The solid inside both x2+y2≤1x^2 + y^2 \le 1 and x2+z2≤1x^2 + z^2 \le 1 has volume 16/316/3. Estimate it by Monte Carlo: sample points uniformly in the cube [−1,1]3[-1, 1]^3 (volume 8) and multiply 8 by the fraction that lands inside.

python · try it yourself
import numpy as np

rng = np.random.default_rng(2024)
N = 400_000
x, y, z = rng.uniform(-1, 1, size=(3, N))
inside = np.zeros(N, dtype=bool)     # TODO: x^2 + y^2 <= 1 and x^2 + z^2 <= 1
volume = 8 * inside.mean()
print(f"Monte Carlo volume ≈ {volume:.4f}   exact 16/3 = {16 / 3:.4f}")
Monte Carlo volume ≈ 0.0000   exact 16/3 = 5.3333

Expected output when solved:

text
Monte Carlo volume ≈ 5.3450   exact 16/3 = 5.3333

Hint: By hand: the cross-section at a fixed xx is a square of side 21−x22\sqrt{1 - x^2}, so V=∫−114(1−x2) dx=16/3V = \int_{-1}^1 4(1 - x^2)\,dx = 16/3.

Show solution
python · try it yourself
import numpy as np

rng = np.random.default_rng(2024)
N = 400_000
x, y, z = rng.uniform(-1, 1, size=(3, N))
inside = (x**2 + y**2 <= 1) & (x**2 + z**2 <= 1)
volume = 8 * inside.mean()
print(f"Monte Carlo volume ≈ {volume:.4f}   exact 16/3 = {16 / 3:.4f}")
Monte Carlo volume ≈ 5.3450   exact 16/3 = 5.3333

The standard error of this estimate is 8⋅p(1−p)/N≈0.0068 \cdot \sqrt{p(1 - p)/N} \approx 0.006 with p=2/3p = 2/3, so the difference of 0.012 you see is about two standard errors: normal for one run. More samples shrink it like 1/N1/\sqrt{N}.

Exercise 10: Work done by a gradient field

F=⟨2xy,x2,cos⁡z⟩F = \langle 2xy, x^2, \cos z \rangle is the gradient of φ=x2y+sin⁡z\varphi = x^2y + \sin z. Compute the work ∫CF⋅dr\int_C F \cdot dr along the helix r(t)=⟨cos⁡t,sin⁡t,t/π⟩r(t) = \langle \cos t, \sin t, t/\pi \rangle, 0≤t≤2π0 \le t \le 2\pi, as a midpoint sum of F(r(t))⋅r′(t) dtF(r(t)) \cdot r'(t)\,dt, and compare with φ(end)−φ(start)\varphi(\text{end}) - \varphi(\text{start}). The starter's r′r' is zero.

python · try it yourself
import numpy as np

F = lambda x, y, z: np.array([2 * x * y, x**2, np.cos(z)])
phi = lambda x, y, z: x**2 * y + np.sin(z)
n = 20_000
dt = 2 * np.pi / n
t = (np.arange(n) + 0.5) * dt
r = np.array([np.cos(t), np.sin(t), t / np.pi])
dr = np.zeros_like(r)                   # TODO: r'(t), one row per component
work = float(np.sum(F(*r) * dr) * dt)
print(f"line integral ≈ {work:.6f}")
print(f"phi(end) - phi(start) = {phi(1, 0, 2) - phi(1, 0, 0):.6f}")
line integral ≈ 0.000000
phi(end) - phi(start) = 0.909297

Expected output when solved:

text
line integral ≈ 0.909297
phi(end) - phi(start) = 0.909297

Hint: r′(t)=⟨−sin⁡t,cos⁡t,1/π⟩r'(t) = \langle -\sin t, \cos t, 1/\pi \rangle. The path starts at (1,0,0)(1, 0, 0) and ends at (1,0,2)(1, 0, 2).

Show solution
python · try it yourself
import numpy as np

F = lambda x, y, z: np.array([2 * x * y, x**2, np.cos(z)])
phi = lambda x, y, z: x**2 * y + np.sin(z)
n = 20_000
dt = 2 * np.pi / n
t = (np.arange(n) + 0.5) * dt
r = np.array([np.cos(t), np.sin(t), t / np.pi])
dr = np.array([-np.sin(t), np.cos(t), np.full_like(t, 1 / np.pi)])
work = float(np.sum(F(*r) * dr) * dt)
print(f"line integral ≈ {work:.6f}")
print(f"phi(end) - phi(start) = {phi(1, 0, 2) - phi(1, 0, 0):.6f}")
line integral ≈ 0.909297
phi(end) - phi(start) = 0.909297

Both give sin⁡2≈0.909297\sin 2 \approx 0.909297: the work of a gradient field depends only on the endpoints, so the whole helix contributes exactly what a straight segment would.

Test yourself

In Exercise 2 the Newton errors were about 10−210^{-2}, 3imes10−53 imes 10^{-5}, then 2imes10−102 imes 10^{-10}. What property is this?

  1. Linear convergence
  2. Quadratic convergence
  3. Divergence
  4. Oscillation

Monte Carlo with NN samples estimated a volume with error about 0.006. How many samples are needed to reach about 0.0006?

  1. 10⋅N10 \cdot N
  2. N2N^2
  3. 100⋅N100 \cdot N
  4. 2⋅N2 \cdot N

Why is the work of F=∇φF = \nabla\varphi along the helix equal to φ(end)−φ(start)\varphi(\text{end}) - \varphi(\text{start})?

  1. Because the helix is closed
  2. Because of the fundamental theorem for line integrals: ddtφ(r(t))=∇φ⋅r′(t)\frac{d}{dt} \varphi(r(t)) = \nabla\varphi \cdot r'(t)
  3. Because FF is constant
  4. Because the curl of FF is non-zero