mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-23 22:23:35 +00:00
Create newton_forward_interpolation.py (#333)
* Create newton_forward_interpolation.py This code is for calculating newton forward difference interpolation for fixed difference. * Add doctests and reformat with black
This commit is contained in:
parent
92268561a5
commit
54830644a2
55
arithmetic_analysis/newton_forward_interpolation.py
Normal file
55
arithmetic_analysis/newton_forward_interpolation.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
# https://www.geeksforgeeks.org/newton-forward-backward-interpolation/
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
# for calculating u value
|
||||||
|
def ucal(u, p):
|
||||||
|
"""
|
||||||
|
>>> ucal(1, 2)
|
||||||
|
0
|
||||||
|
>>> ucal(1.1, 2)
|
||||||
|
0.11000000000000011
|
||||||
|
>>> ucal(1.2, 2)
|
||||||
|
0.23999999999999994
|
||||||
|
"""
|
||||||
|
temp = u
|
||||||
|
for i in range(1, p):
|
||||||
|
temp = temp * (u - i)
|
||||||
|
return temp
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
n = int(input("enter the numbers of values"))
|
||||||
|
y = []
|
||||||
|
for i in range(n):
|
||||||
|
y.append([])
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(n):
|
||||||
|
y[i].append(j)
|
||||||
|
y[i][j] = 0
|
||||||
|
|
||||||
|
print("enter the values of parameters in a list")
|
||||||
|
x = list(map(int, input().split()))
|
||||||
|
|
||||||
|
print("enter the values of corresponding parameters")
|
||||||
|
for i in range(n):
|
||||||
|
y[i][0] = float(input())
|
||||||
|
|
||||||
|
value = int(input("enter the value to interpolate"))
|
||||||
|
u = (value - x[0]) / (x[1] - x[0])
|
||||||
|
|
||||||
|
# for calculating forward difference table
|
||||||
|
|
||||||
|
for i in range(1, n):
|
||||||
|
for j in range(n - i):
|
||||||
|
y[j][i] = y[j + 1][i - 1] - y[j][i - 1]
|
||||||
|
|
||||||
|
summ = y[0][0]
|
||||||
|
for i in range(1, n):
|
||||||
|
summ += (ucal(u, i) * y[0][i]) / math.factorial(i)
|
||||||
|
|
||||||
|
print("the value at {} is {}".format(value, summ))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
x
Reference in New Issue
Block a user