@@ -46,14 +46,42 @@ def newton(x, y, x_int):
4646 for i in range (m - 1 , k - 1 , - 1 ):
4747 del_y [i ] = (del_y [i ] - del_y [i - 1 ]) / (x [i ] - x [i - k ])
4848
49- # Evaluate the Newton polynomial
49+ # Evaluate the polynomial by Horner's method
5050 y_int = del_y [- 1 ]
5151 for i in range (m - 2 , - 1 , - 1 ):
5252 y_int = y_int * (x_int - x [i ]) + del_y [i ]
5353
5454 return [y_int ]
5555
5656
57+ def gregory_newton (x , y , x_int ):
58+ """Interpolates a value using the 'Gregory-Newton polynomial'.
59+
60+ Args:
61+ x (numpy.ndarray): x values.
62+ y (numpy.ndarray): y values.
63+ x_int (float): value to interpolate.
64+
65+ Returns:
66+ y_int (float): interpolated value.
67+ """
68+ m = x .size
69+ del_y = y .copy ()
70+
71+ # Calculate the finite differences
72+ for k in range (1 , m ):
73+ for i in range (m - 1 , k - 1 , - 1 ):
74+ del_y [i ] = del_y [i ] - del_y [i - 1 ]
75+
76+ # Evaluate the polynomial by Horner's method
77+ u = (x_int - x [0 ]) / (x [1 ] - x [0 ])
78+ y_int = del_y [- 1 ]
79+ for i in range (m - 2 , - 1 , - 1 ):
80+ y_int = y_int * (u - i ) / (i + 1 ) + del_y [i ]
81+
82+ return [y_int ]
83+
84+
5785def neville (x , y , x_int ):
5886 """Interpolates a value using the 'Neville polynomial'.
5987
0 commit comments