Week 9: Exercise Solutions

Exercise 9.1

The approximation gives the following. \[\int^1_0 x^2\, \mathrm{d}x\simeq 1/2(1/4)^2 + 1/2(3/4)^2 =1/32 +9/32 =10/32=5/16=0.3125.\] The actual answer is \[\int^1_0 x^2\, \mathrm{d}x = \left[\tfrac13 x^3\right]^1_0 =1/3 \simeq 0.33333.\]

Exercise 9.2

def trapezoidal_approximation(f, N, a, b):
    """
    Calculate an approximation to int_a^b f(x)
    with N trapeziums
    """
    approximation = 0
    for i in range(N):
        approximation += (b - a)/(2*N) * (f(a + (i)*(b - a)/N) +
                                          f(a + (i+1)*(b - a)/N))
    return approximation

Exercise 9.3

The function is the factorial function. It works because the factorial function satisfies the following two equations, which suffice to calculate it for any natural number: \[0\,! =1\qquad \text{and}\qquad n\,!=n\cdot (n-1)\,!\quad\text{for }n\ge 1.\]