37 lines
651 B
Python
37 lines
651 B
Python
#!/usr/bin/env python3
|
|
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
# Truncation of Euler's Constant
|
|
e_OEIS = 2.7182818284590452353
|
|
N_max = 25
|
|
N_range = np.arange(0, N_max)
|
|
|
|
# a) Simple Relative Error Determine
|
|
e_approx = np.zeros(N_max)
|
|
rel_err = np.zeros(N_max)
|
|
|
|
prev_e = 0
|
|
for i, n in enumerate(N_range):
|
|
e_approx[i] = prev_e + 1/np.math.factorial(n)
|
|
|
|
rel_err[i] = abs( e_approx[i] - e_OEIS ) / e_OEIS
|
|
prev_e = e_approx[i]
|
|
|
|
print(e_approx)
|
|
|
|
fig, ax = plt.subplots()
|
|
ax.plot(N_range, rel_err)
|
|
ax.set_yscale('log')
|
|
ax.grid()
|
|
ax.set_ylabel("Relative Error")
|
|
ax.set_xlabel("N")
|
|
|
|
plt.show()
|
|
|
|
|
|
# b) Varying Floating Point Precision
|
|
|