Ex1.a added
This commit is contained in:
parent
dc70917946
commit
9268f96ac9
3 changed files with 37 additions and 1 deletions
|
@ -1,3 +1,3 @@
|
||||||
# cds-numerical-methods
|
# cds-numerical-methods
|
||||||
|
|
||||||
Coursework CDS:Numerical Methods
|
Coursework CDS:Numerical Methods 2019-2020
|
||||||
|
|
36
week1/ex1.py
Normal file
36
week1/ex1.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
#!/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
|
||||||
|
|
BIN
week1/week1.pdf
Normal file
BIN
week1/week1.pdf
Normal file
Binary file not shown.
Reference in a new issue