An archive of Mark's Spring 2018 Numerical Analysis course.

Section 1.1 Question 19

nathan

As it turns out, e is defined by e=\sum_{n=0}^{\infty} (1/n!). Find the relative and absolute errors in the following approximations of e:

e_{5}=\sum_{n=0}^{5} (1/n!)

and

e_{10}=\sum_{n=0}^{10} (1/n!).
nathan

Solving this problem will require the following python packages:

import numpy as np
from scipy.special import factorial

NumPy has an exponential function so we can define e as:

e_real = np.exp(1)

Next, we can imitate a summation with a loop to find e_{5}.

e5 = 0
for i in range(5):
    e5 = e5 + 1/factorial(i, exact = True)
e5  

# Out: 2.708333333333333

Absolute error: e_{5} - e_{real} = 0.0099484951257120535
Relative error: (e_{5}-e_{real})/e_{real} = 0.0036598468273437682

Now let’s try an additional five iterations.

e10 = 0
for i in range(10):
    e10 = e10 + 1/factorial(i, exact = True)
e10

# Out: 2.7182815255731922

Absolute error: e_{10} - e_{real} = 3.0288585284310443e-07
Relative error: (e_{10}-e_{real})/e_{real} = 1.1142547828265698e-07

As we can see, using just five additional terms leads to an e approximation that is four orders of magnitude more accurate.

mark

That’s great!! I edited your code slightly to use a useful technique to include a small amount of numeric output. It’s really a matter of taste, but I rather like it.