-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinitediff.py
More file actions
31 lines (23 loc) · 705 Bytes
/
finitediff.py
File metadata and controls
31 lines (23 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from math import exp, sin, cos, log10
def f(x):
return exp(sin(2*x))
def fprime(x):
return 2*exp(sin(2*x))*cos(2*x)
def calc_fd(f,x,h):
fd = (f(x+h) - f(x))/h
return fd
def calc_cd(f,x,h):
cd = (f(x+h/2) - f(x-h/2))/h
return cd
if __name__ == '__main__':
x = 0.5
an = fprime(x)
hs = [10**(-i) for i in range(1,12)]
fds = [abs(calc_fd(f,x,h) - an) for h in hs]
cds = [abs(calc_cd(f,x,h) - an) for h in hs]
rowf = "{0:1.0e} {1:1.16f} {2:1.16f}"
print("h abs. error in fd abs. error in cd")
for h,fd,cd in zip(hs,fds,cds):
print(rowf.format(h,fd,cd))