TExcelTAD
Home › Calculus Essentials › Calculus Exercises

Calculus Exercises

Time to practise across the whole course: limits, derivatives, Newton's method, optimization, integrals, series, partial derivatives, double integrals and differential equations. 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: Work out the answer on paper first, even roughly, then let the code confirm it. If the code and your paper disagree, one of them has a bug, and finding out which is the real exercise.

# Exercise Practises
1 Sneaking up on a limit limits, numeric tables
2 Check a derivative product rule, central differences
3 Cube root by Newton Newton's method
4 The biggest box optimization with SymPy
5 Simpson's rule numerical integration
6 How many Taylor terms? Taylor series, approximation error
7 Gradient and slope in a direction gradient and directional derivative
8 A double integral on a grid double integrals
9 When is the coffee drinkable? separable equations, Euler's method

Exercise 1: Sneaking up on a limit

Estimate lim⁡h→0(eh−1)/h\lim_{h \to 0} (e^h - 1)/h by evaluating the expression for h=0.1,0.01,0.001h = 0.1, 0.01, 0.001 and 0.00010.0001. The starter prints 0 for each row.

python · try it yourself
import math

for h in [0.1, 0.01, 0.001, 0.0001]:
    value = 0.0   # TODO: compute (e^h - 1) / h
    print(f"h = {h:<7} value = {value:.6f}")
h = 0.1     value = 0.000000
h = 0.01    value = 0.000000
h = 0.001   value = 0.000000
h = 0.0001  value = 0.000000

Expected output when solved:

text
h = 0.1     value = 1.051709
h = 0.01    value = 1.005017
h = 0.001   value = 1.000500
h = 0.0001  value = 1.000050

Hint: The values approach the slope of exe^x at x=0x = 0. The error is roughly h/2h/2.

Show solution
python · try it yourself
import math

for h in [0.1, 0.01, 0.001, 0.0001]:
    value = (math.exp(h) - 1) / h
    print(f"h = {h:<7} value = {value:.6f}")
h = 0.1     value = 1.051709
h = 0.01    value = 1.005017
h = 0.001   value = 1.000500
h = 0.0001  value = 1.000050

The limit is 1, which is why (ex)′=ex(e^x)' = e^x: the slope of exe^x at 0 is exactly 1.

Exercise 2: Check a derivative

Differentiate f(x)=x3sin⁡(x)f(x) = x^3 \sin(x) by hand, then confirm your formula two ways at x=1x = 1: with SymPy and with the central difference (f(x+h)−f(x−h))/2h(f(x + h) - f(x - h)) / 2h for h=10−5h = 10^{-5}.

python · try it yourself
import math
import sympy as sp

x = sp.symbols("x")
f = x**3 * sp.sin(x)
by_hand = 0 * x          # TODO: your derivative of x^3 sin x
print("SymPy  :", sp.diff(f, x))
print(f"by hand at 1: {float(by_hand.subs(x, 1)):.6f}")
fn = lambda t: t**3 * math.sin(t)
h = 1e-5
central = 0.0            # TODO: central difference at 1
print(f"central at 1: {central:.6f}")
SymPy  : x**3*cos(x) + 3*x**2*sin(x)
by hand at 1: 0.000000
central at 1: 0.000000

Expected output when solved: SymPy prints x**3*cos(x) + 3*x**2*sin(x), and both numbers are 3.064715.

Hint: Product rule: (u⋅v)′=u′⋅v+u⋅v′(u \cdot v)' = u' \cdot v + u \cdot v' with u=x3u = x^3 and v=sin⁡xv = \sin x.

Show solution
python · try it yourself
import math
import sympy as sp

x = sp.symbols("x")
f = x**3 * sp.sin(x)
by_hand = 3 * x**2 * sp.sin(x) + x**3 * sp.cos(x)
print("SymPy  :", sp.diff(f, x))
print(f"by hand at 1: {float(by_hand.subs(x, 1)):.6f}")
fn = lambda t: t**3 * math.sin(t)
h = 1e-5
central = (fn(1 + h) - fn(1 - h)) / (2 * h)
print(f"central at 1: {central:.6f}")
SymPy  : x**3*cos(x) + 3*x**2*sin(x)
by hand at 1: 3.064715
central at 1: 3.064715

Exercise 3: Cube root by Newton

Find 103\sqrt[3]{10} as the root of g(x)=x3−10g(x) = x^3 - 10 with Newton's method x←x−g(x)/g′(x)x \leftarrow x - g(x)/g'(x), starting at x=2x = 2. Print each of 5 iterations. The starter never moves.

python · try it yourself
x = 2.0
for i in range(1, 6):
    # TODO: one Newton step for g(x) = x^3 - 10
    print(f"iteration {i}: x = {x:.10f}")
print(f"check: x^3 = {x**3:.10f}")
iteration 1: x = 2.0000000000
iteration 2: x = 2.0000000000
iteration 3: x = 2.0000000000
iteration 4: x = 2.0000000000
iteration 5: x = 2.0000000000
check: x^3 = 8.0000000000

Expected output when solved:

text
iteration 1: x = 2.1666666667
iteration 2: x = 2.1545036160
iteration 3: x = 2.1544346922
iteration 4: x = 2.1544346900
iteration 5: x = 2.1544346900
check: x^3 = 10.0000000000

Hint: g′(x)=3x2g'(x) = 3x^2. The number of correct digits roughly doubles each iteration.

Show solution
python · try it yourself
x = 2.0
for i in range(1, 6):
    x = x - (x**3 - 10) / (3 * x**2)
    print(f"iteration {i}: x = {x:.10f}")
print(f"check: x^3 = {x**3:.10f}")
iteration 1: x = 2.1666666667
iteration 2: x = 2.1545036160
iteration 3: x = 2.1544346922
iteration 4: x = 2.1544346900
iteration 5: x = 2.1544346900
check: x^3 = 10.0000000000

Exercise 4: The biggest box

From a 30 cm×30 cm30\ \text{cm} \times 30\ \text{cm} sheet of card, cut a square of side xx from each corner and fold up the sides into an open box. Its volume is V(x)=x(30−2x)2V(x) = x(30 - 2x)^2. Use SymPy to find the critical points, choose the one in 0<x<150 < x < 15, and print the maximum volume.

python · try it yourself
import sympy as sp

x = sp.symbols("x")
V = x * (30 - 2 * x)**2
critical = []            # TODO: solve V'(x) = 0
print("critical points:", critical)
best = 0                 # TODO: the critical point inside (0, 15)
print("best cut:", best, "cm   volume:", V.subs(x, best), "cm^3")
critical points: []
best cut: 0 cm   volume: 0 cm^3

Expected output when solved: critical points [5, 15], best cut 5 cm, volume 2000 cm3\text{cm}^3.

Hint: x=15x = 15 gives a box with no bottom (V=0V = 0). Check that V′′(5)<0V''(5) < 0 to confirm a maximum.

Show solution
python · try it yourself
import sympy as sp

x = sp.symbols("x")
V = x * (30 - 2 * x)**2
critical = sp.solve(sp.diff(V, x), x)
print("critical points:", critical)
best = [c for c in critical if 0 < c < 15][0]
print("best cut:", best, "cm   volume:", V.subs(x, best), "cm^3")
print("V''(5) =", sp.diff(V, x, 2).subs(x, best))
critical points: [5, 15]
best cut: 5 cm   volume: 2000 cm^3
V''(5) = -120

V′(x)=(30−2x)2−4x(30−2x)=(30−2x)(30−6x)V'(x) = (30 - 2x)^2 - 4x(30 - 2x) = (30 - 2x)(30 - 6x), so x=5x = 5 or x=15x = 15, and V′′(5)=−120<0V''(5) = -120 < 0 confirms the maximum.

Exercise 5: Simpson's rule

Approximate ∫0πsin⁡x dx\int_0^\pi \sin x\,dx (exactly 2) with Simpson's rule on n=10n = 10 subintervals: h3[f0+4f1+2f2+4f3+⋯+4f9+f10]\frac{h}{3}[f_0 + 4f_1 + 2f_2 + 4f_3 + \cdots + 4f_9 + f_{10}].

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 for odd i and 2*f for even i, i = 1 .. n-1
    return h / 3 * total

approx = simpson(math.sin, 0, math.pi, 10)
print(f"Simpson n=10: {approx:.8f}   error {abs(approx - 2):.2e}")
Simpson n=10: 0.00000000   error 2.00e+00

Expected output when solved:

text
Simpson n=10: 2.00010952   error 1.10e-04

Hint: total += (4 if i % 2 else 2) * f(a + i * h).

Show solution
python · try it yourself
import math

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

approx = simpson(math.sin, 0, math.pi, 10)
print(f"Simpson n=10: {approx:.8f}   error {abs(approx - 2):.2e}")
Simpson n=10: 2.00010952   error 1.10e-04

With only 11 function values the error is about 10−410^{-4}; doubling nn cuts it about 16 times (Simpson is fourth order).

Exercise 6: How many Taylor terms?

e=∑1/k!e = \sum 1/k! for k=0,1,2,…k = 0, 1, 2, \ldots Add terms until the partial sum is within 10−610^{-6} of math.e, and print how many terms it took.

python · try it yourself
import math

total, k, term = 0.0, 0, 1.0
# TODO: loop while abs(total - math.e) >= 1e-6, adding term = 1/k! and increasing k
print(f"terms used: {k}   sum = {total:.8f}   error = {abs(total - math.e):.1e}")
terms used: 0   sum = 0.00000000   error = 2.7e+00

Expected output when solved: terms used: 10, the sum 2.71828153, error about 3.0e-07.

Hint: Update the term with term /= k after increasing kk instead of calling math.factorial.

Show solution
python · try it yourself
import math

total, k, term = 0.0, 0, 1.0
while abs(total - math.e) >= 1e-6:
    total += term          # adds 1/k!
    k += 1
    term /= k
print(f"terms used: {k}   sum = {total:.8f}   error = {abs(total - math.e):.1e}")
terms used: 10   sum = 2.71828153   error = 3.0e-07

The terms k=0…9k = 0 \ldots 9 leave out the tail 1/10!+1/11!+⋯≈2.76⋅10−7+2.5⋅10−8+⋯≈3.0⋅10−71/10! + 1/11! + \cdots \approx 2.76 \cdot 10^{-7} + 2.5 \cdot 10^{-8} + \cdots \approx 3.0 \cdot 10^{-7}, which is exactly the error printed.

Exercise 7: Gradient and slope in a direction

For f(x,y)=x2y+y3f(x, y) = x^2 y + y^3, compute the gradient at (1,2)(1, 2) with central differences, then the directional derivative in the direction of the vector (3,4)(3, 4).

python · try it yourself
f = lambda x, y: x**2 * y + y**3
h = 1e-6
fx = 0.0   # TODO: central difference in x at (1, 2)
fy = 0.0   # TODO: central difference in y at (1, 2)
print(f"gradient = ({fx:.4f}, {fy:.4f})")
u = (3, 4)  # TODO: normalise u first
print(f"directional derivative = {fx * u[0] + fy * u[1]:.4f}")
gradient = (0.0000, 0.0000)
directional derivative = 0.0000

Expected output when solved: gradient = (4.0000, 13.0000) and directional derivative = 12.8000.

Hint: ∇f=(2xy,x2+3y2)\nabla f = (2xy, x^2 + 3y^2). The unit vector is (3,4)/5(3, 4)/5.

Show solution
python · try it yourself
f = lambda x, y: x**2 * y + y**3
h = 1e-6
fx = (f(1 + h, 2) - f(1 - h, 2)) / (2 * h)
fy = (f(1, 2 + h) - f(1, 2 - h)) / (2 * h)
print(f"gradient = ({fx:.4f}, {fy:.4f})")
u = (3 / 5, 4 / 5)
print(f"directional derivative = {fx * u[0] + fy * u[1]:.4f}")
gradient = (4.0000, 13.0000)
directional derivative = 12.8000

Exactly: ∇f(1,2)=(2⋅1⋅2,1+12)=(4,13)\nabla f(1, 2) = (2 \cdot 1 \cdot 2, 1 + 12) = (4, 13) and (4⋅3+13⋅4)/5=64/5=12.8(4 \cdot 3 + 13 \cdot 4)/5 = 64/5 = 12.8.

Exercise 8: A double integral on a grid

Compute ∬(x+y2) dA\iint (x + y^2)\,dA over the rectangle 0≤x≤10 \le x \le 1, 0≤y≤20 \le y \le 2 with the midpoint rule on a 100×100100 \times 100 grid using NumPy, and compare with the exact value.

python · try it yourself
import numpy as np

n = 100
dx, dy = 1 / n, 2 / n
xs = (np.arange(n) + 0.5) * dx        # midpoints in x
ys = (np.arange(n) + 0.5) * dy        # midpoints in y
X, Y = np.meshgrid(xs, ys)
approx = 0.0                          # TODO: sum f at the midpoints times the cell area
print(f"midpoint: {approx:.6f}")
exact = 0.0                           # TODO: work it out by hand (iterated integral)
print(f"exact:    {exact:.6f}")
midpoint: 0.000000
exact:    0.000000

Expected output when solved: midpoint 3.666600, exact 3.666667 (=11/3= 11/3).

Hint: ∫01∫02x dy dx=1\int_0^1 \int_0^2 x\,dy\,dx = 1 and ∫01∫02y2 dy dx=8/3\int_0^1 \int_0^2 y^2\,dy\,dx = 8/3.

Show solution
python · try it yourself
import numpy as np

n = 100
dx, dy = 1 / n, 2 / n
xs = (np.arange(n) + 0.5) * dx
ys = (np.arange(n) + 0.5) * dy
X, Y = np.meshgrid(xs, ys)
approx = np.sum(X + Y**2) * dx * dy
print(f"midpoint: {approx:.6f}")
exact = 1 + 8 / 3
print(f"exact:    {exact:.6f}")
midpoint: 3.666600
exact:    3.666667

The xx part is integrated exactly by the midpoint rule (it is linear); the small error comes only from y2y^2.

Exercise 9: When is the coffee drinkable?

Coffee at 95 °C cools in a 20 °C room following T′=−0.1(T−20)T' = -0.1(T - 20) (tt in minutes). Step the equation with Euler's method (h=0.01h = 0.01) until TT drops below 60 °C, print the time, and compare with the exact time from the separable solution.

python · try it yourself
import math

T, t, h = 95.0, 0.0, 0.01
# TODO: while T >= 60, take an Euler step and advance t
print(f"Euler: below 60 °C at t = {t:.2f} min")
exact = 0.0  # TODO: solve 20 + 75 e^(-0.1 t) = 60 for t
print(f"exact: t = {exact:.2f} min")
Euler: below 60 °C at t = 0.00 min
exact: t = 0.00 min

Expected output when solved: Euler t = 6.29 min, exact t = 6.29 min.

Hint: T(t)=20+75e−0.1tT(t) = 20 + 75e^{-0.1t}, so t=10ln⁡(75/40)t = 10\ln(75/40).

Show solution
python · try it yourself
import math

T, t, h = 95.0, 0.0, 0.01
while T >= 60:
    T += h * (-0.1 * (T - 20))
    t += h
print(f"Euler: below 60 °C at t = {t:.2f} min")
exact = 10 * math.log(75 / 40)
print(f"exact: t = {exact:.2f} min")
Euler: below 60 °C at t = 6.29 min
exact: t = 6.29 min

Test yourself

In Exercise 3, why does Newton's method converge so quickly?

  1. Because x3−10x^3 - 10 is linear
  2. Near a simple root the error is roughly squared each step, so correct digits double
  3. Because it uses the second derivative
  4. It doesn't, it needs thousands of steps

In Exercise 4, why is x=15x = 15 rejected?

  1. It gives a negative volume
  2. It is a minimum: cutting 15 cm squares leaves no base, V=0V = 0
  3. SymPy got it wrong
  4. It is not a critical point

In Exercise 8, which part of the integrand does the midpoint rule integrate exactly?

  1. y2y^2
  2. xx
  3. Neither
  4. Both