forked from Zura1101/NUMERICAL-METHODS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Divided_interpolation.py
75 lines (56 loc) · 1.67 KB
/
Divided_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
# Python3 program for implementing
# Newton divided difference formula
from sympy import Symbol
# Function to find the product term
def proterm(i, value, x):
pro = 1;
for j in range(i):
pro = pro * (value - x[j]);
return pro;
# Function for calculating
# divided difference table
def dividedDiffTable(x, y, n):
for i in range(1, n):
for j in range(n - i):
y[j][i] = ((y[j][i - 1] - y[j + 1][i - 1]) /
(x[j] - x[i + j]));
return y;
# Function for applying Newton's
# divided difference formula
def applyFormula(value, x, y, n):
sum = y[0][0];
for i in range(1, n):
sum = sum + (proterm(i, value, x) * y[0][i]);
return sum;
# Function for displaying divided
# difference table
def printDiffTable(y, n):
for i in range(n):
for j in range(n - i):
print(round(y[i][j], 4), "\t",
end=" ");
print("");
# Driver Code
# number of inputs given ----------------------------
n = 8;
y = [[0 for i in range(10)]
for j in range(10)];
x = [2,5,6,8,9,10,13,14] #------------------------
# y[][] is used for divided difference
# table where y[][0] is used for input -------------------
y[0][0] = 2;
y[1][0] = 8;
y[2][0] = 14;
y[3][0] = 39;
y[4][0] = 50;
y[5][0] = 65;
y[6][0] = 87;
y[7][0] = 98;
# value to be interpolated --------------------------------
value =7
# calculating divided difference table
y = dividedDiffTable(x, y, n);
# displaying divided difference table
printDiffTable(y, n);
print("\nValue at", value, "is",
round(applyFormula(value, x, y, n), 2))