0% found this document useful (0 votes)
74 views3 pages

Newton's Forward and Backward Interpolation Formula Code

The document contains Python code for implementing Newton's Forward and Backward Interpolation methods. It includes functions to create forward and backward difference tables and calculate interpolated values based on given x and y values. An example is provided to demonstrate the interpolation at a specific x value using both methods.
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)
74 views3 pages

Newton's Forward and Backward Interpolation Formula Code

The document contains Python code for implementing Newton's Forward and Backward Interpolation methods. It includes functions to create forward and backward difference tables and calculate interpolated values based on given x and y values. An example is provided to demonstrate the interpolation at a specific x value using both methods.
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
You are on page 1/ 3

Newton's Forward and Backward Difference Code

import numpy as np

def forward_difference_table(y_values):

"""Creates the forward difference table."""

n = len(y_values)
table = np.zeros((n, n))
table[:, 0] = y_values # First column is y values

for j in range(1, n):


for i in range(n - j):
table[i][j] = table[i + 1][j - 1] - table[i][j - 1]

return table

def newton_forward_interpolation(x_values, y_values, x):


"""Performs Newton's Forward Interpolation."""
n = len(x_values)
h = x_values[1] - x_values[0] # Step size
p = (x - x_values[0]) / h # p-value

table = forward_difference_table(y_values)
result = y_values[0] # First term

# Compute terms iteratively


factorial = 1
term = 1
for i in range(1, n):
factorial *= i
term *= (p - (i - 1))
result += (term * table[0][i]) / factorial

return result

def backward_difference_table(y_values):
"""Creates the backward difference table."""
n = len(y_values)
table = np.zeros((n, n))
table[:, 0] = y_values # First column is y values

for j in range(1, n):


for i in range(n - 1, j - 1, -1):
table[i][j] = table[i][j - 1] - table[i - 1][j - 1]

return table

def newton_backward_interpolation(x_values, y_values, x):


"""Performs Newton's Backward Interpolation."""
n = len(x_values)
h = x_values[1] - x_values[0] # Step size
p = (x - x_values[-1]) / h # p-value

table = backward_difference_table(y_values)
result = y_values[-1] # Last value

# Compute terms iteratively


factorial = 1
term = 1
for i in range(1, n):
factorial *= i
term *= (p + (i - 1))
result += (term * table[-1][i]) / factorial

return result
# Example data: Given x and y values
x_values = [0, 10, 20, 30, 40]
y_values = [0, 0.1763, 0.3492, 0.5171, 0.6804] # Example function values
# Find interpolation at x = 25
x_to_find = 25
# Apply Newton's Forward and Backward Interpolation
forward_result = newton_forward_interpolation(x_values, y_values, x_to_find)
backward_result = newton_backward_interpolation(x_values, y_values, x_to_find)

print(f"Newton's Forward Interpolation at x={x_to_find}: {forward_result:.6f}")


print(f"Newton's Backward Interpolation at x={x_to_find}: {backward_result:.6f}")

You might also like