Last active
December 11, 2021 00:40
-
-
Save komasaru/2024aaf644835cdbc7e8cbf45834228e to your computer and use it in GitHub Desktop.
Python script to interpolate with Newton method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/python3.6 | |
""" | |
Interpolation with Newton method | |
""" | |
import sys | |
import traceback | |
class InterpolateNewton: | |
X = [0.0, 2.0, 3.0, 5.0, 8.0] | |
Y = [0.8, 3.2, 2.8, 4.5, 1.9] | |
def __init__(self): | |
self.n = len(self.X) | |
def compute(self): | |
""" Computation of interpolation with Newton method """ | |
try: | |
print(" x y") | |
for a in range(int(self.X[-1]) * 2 + 1): | |
t = 0.5 * a | |
print("{:7.2f}{:7.2f}".format(t, self.__interpolate(t))) | |
except Exception as e: | |
raise | |
def __interpolate(self, t): | |
""" Interpoalation with Newton method | |
:param float t | |
""" | |
try: | |
c = [0 for _ in range(self.n)] | |
w = [0 for _ in range(self.n)] | |
for i in range(0, self.n): | |
w[i] = self.Y[i] | |
for j in reversed(range(i)): | |
w[j] = (w[j + 1] - w[j]) / (self.X[i] - self.X[j]) | |
c[i] = w[0] | |
s = c[self.n - 1] | |
for i in reversed(range(self.n - 1)): | |
s = s * (t - self.X[i]) + c[i] | |
return s | |
except Exception as e: | |
raise | |
if __name__ == '__main__': | |
try: | |
obj = InterpolateNewton() | |
obj.compute() | |
except Exception as e: | |
traceback.print_exc() | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Newton interpolation is a method of finding an interpolated polynomial that passes through all given pairs of (x, y).
It is not intended to be applied to images.