-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpolation.py
76 lines (54 loc) · 1.68 KB
/
interpolation.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np
def lagrange(data_x, data_y, x):
y_ans = 0
for i in range(data_x.size):
p = data_y[i]
for j in range(data_x.size):
if i != j:
p *= ((x - data_x[j]) / (data_x[i] - data_x[j]))
y_ans += p
return y_ans
def newton_divided_difference(data_x, data_y, x):
c = np.copy(data_y)
for j in range(1, data_x.size):
for i in range(data_x.size - 1, j - 1, -1):
c[i] = float(c[i] - c[i - 1]) / float(data_x[i] - data_x[i - j])
y_ans = 0
for i in range(data_x.size):
f = c[i]
for j in range(i):
f *= (x - data_x[j])
y_ans += f
return y_ans
def input_data():
data_x = np.array([])
data_y = np.array([])
n = int(input("Enter the number of points: "))
for i in range(n):
data_x = np.append(data_x, float(input("X[{}]: ".format(i))))
for i in range(n):
data_y = np.append(data_y, float(input("Y[{}]: ".format(i))))
x = float(input("Find Y for X = "))
return [data_x, data_y, x]
def test_lagrange():
"""
Example:
data_x = [1.0, 1.3, 1.5]
data_y = [0.841, 0.964, 0.997]
P(1.4) = 0.9854
"""
data_x, data_y, x = input_data()
y_ans = lagrange(data_x, data_y, x)
print("Using Lagrange Method:")
print("F({}) = {}".format(x, y_ans))
def test_newton():
"""
Example:
data_x = [1.0, 1.3, 1.5]
data_y = [0.841, 0.964, 0.997]
P(1.4) = 0.9854
"""
data_x, data_y, x = input_data()
y_ans = newton_divided_difference(data_x, data_y, x)
print("Using Newton Divided Difference Method:")
print("F({}) = {}".format(x, y_ans))