-
Notifications
You must be signed in to change notification settings - Fork 0
/
finite_difference_first.py
42 lines (31 loc) · 1.04 KB
/
finite_difference_first.py
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
32
33
34
35
36
37
38
39
40
41
42
import numpy
import math
def f(x):
return math.exp(-1*x) * math.cos(x)
def finite_difference_first(x, i, mode):
"""
note: this function does not check the validity of i
keep in mind that:
backward cant work at i = 0
forward cant work at i = len(x) - 1
central only work at interior points, 0 < i < len(x) - 1
"""
if mode == "backward":
h = x[i] - x[i-1]
return (f(x[i]) - f(x[i-1])) / h
if mode == "forward":
h = x[i+1] - x[i]
return (f(x[i+1]) - f(x[i])) / h
if mode == "central":
h = x[i+1] - x[i]
return (f(x[i+1]) - f(x[i-1])) / (2*h)
# making data (an evenly spaced data with step = 0.2)
x = numpy.arange(0.0, 2, 0.2).tolist()
# we are approximating f(1.4), the correct index is
# 7 since x[7] = 1.4
backward_diff = finite_difference_first(x, 7, mode="backward")
print(backward_diff)
forward_diff = finite_difference_first(x, 7, mode="forward")
print(forward_diff)
central_diff = finite_difference_first(x, 7, mode="central")
print(central_diff)