-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNewtonDivDiff.py
51 lines (39 loc) · 1.22 KB
/
NewtonDivDiff.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
43
44
45
46
47
48
49
50
51
# Code by Hatilima
# 16.12.2019
import numpy as np
def finiteDividedDiffCoeffs(x, y):
# x and y are arrays of data points
# you can write a small function to enter them one by
# one or to read them from some file (xls, txt, csv, etc)
n = len(x)
x.astype(float)
y.astype(float)
F = np.zeros((n, n), dtype=float)
b = np.zeros(n)
for i in range(n):
F[i, 0] = y[i]
for i in range(1, n):
for j in range(i, n):
F[j, i] = float(F[j, i-1]-F[j-1, i-1])/float(x[j]-x[j-i])
for i in range(n):
b[i] = F[i, i]
return np.array(b)
def interpol(b, x, input_value):
# b is array of coeffs
# x is array of x data points
# input_value is the x value whose output is to be interpolated
x.astype(float)
n = len(b)-1
temp = b[n]
yamba = b[3]
print(yamba)
for i in range(n-1, -1, -1):
temp = temp*(input_value-x[i]) + b[i]
return temp
# The following code just tests if the interpolation works
x = np.array([5, 6, 9, 11])
y = np.array([12, 13, 14, 16])
b = finiteDividedDiffCoeffs(x, y)
y_interpo = interpol(b, x, 7)
print("X value of 7")
print("Y value is {}".format(y_interpo))