0% found this document useful (0 votes)
6 views10 pages

Python Programming Notes

The document contains Python code for calculating interpolated values using Newton's divided difference method. It defines two functions: 'divided_difference' to create a divided difference table, and 'newton_divided_difference' to compute the interpolated value at a given point. Example usages demonstrate the interpolation for two different sets of data points.

Uploaded by

nainaghodke35
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views10 pages

Python Programming Notes

The document contains Python code for calculating interpolated values using Newton's divided difference method. It defines two functions: 'divided_difference' to create a divided difference table, and 'newton_divided_difference' to compute the interpolated value at a given point. Example usages demonstrate the interpolation for two different sets of data points.

Uploaded by

nainaghodke35
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

def divided_difference(x, y):

n = len(x)
# Create a table with n rows and n columns initialized to 0
table = [[0 for _ in range(n)] for _ in range(n)]

# First column is y values


for i in range(n):
table[i][0] = y[i]

# Fill the divided difference columns


for j in range(1, n):
for i in range(n - j):
numerator = table[i + 1][j - 1] - table[i][j - 1]
denominator = x[i + j] - x[i]
table[i][j] = numerator / denominator

return table

def newton_divided_difference(x, y, value):


n = len(x)
table = divided_difference(x, y)
result = table[0][0]
product_term = 1

for i in range(1, n):


product_term *= (value - x[i - 1])
result += product_term * table[0][i]

print(f"Interpolated value at x = {value} is {result:.5f}")


return result

# Example usage
x = [5, 6, 9, 11]
y = [12, 13, 14, 16]
newton_divided_difference(x, y, 10)

Interpolated value at x = 10 is 14.66667


14.666666666666666

def divided_difference(x, y):


n = len(x)
# Create a table with n rows and n columns initialized to 0
table = [[0 for _ in range(n)] for _ in range(n)]

# First column is y values


for i in range(n):
table[i][0] = y[i]

# Fill the divided difference columns


for j in range(1, n):
for i in range(n - j):
numerator = table[i + 1][j - 1] - table[i][j - 1]
denominator = x[i + j] - x[i]
table[i][j] = numerator / denominator

return table

def newton_divided_difference(x, y, value):


n = len(x)
table = divided_difference(x, y)
result = table[0][0]
product_term = 1

for i in range(1, n):


product_term *= (value - x[i - 1])
result += product_term * table[0][i]

print(f"Interpolated value at x = {value} is {result:.5f}")


return result

# Example usage
x = [1, 2, 7, 8]
y = [1, 5, 5, 4]
newton_divided_difference(x, y, 6)

Interpolated value at x = 6 is 6.23810


6.238095238095239

You might also like