Lab 9 NC GG

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

12/7/23, 5:18 PM Asaad

Numerical Computation

Name: Adnan ul islam

Roll No: FA20-BEE-020

Lab#9: Newton's Forward and Backward


Differences

Section: BEE-7A

Newton's Forward Differences:


Newton's Forward Differences is a numerical method used for interpolating values in a tabulated
function. It is a polynomial interpolation method named after Sir Isaac Newton, and it is
particularly useful when the data points are equally spaced. The method involves constructing a
set of divided difference coefficients from the given data points and then using these coefficients
to form a polynomial that represents the interpolated values.

Advantages:
Ease of Computation: The method is relatively straightforward to implement, and the
forward differences can be computed systematically.

Efficiency: Once the divided differences are calculated, evaluating the interpolating
polynomial at different points is computationally efficient.

Suitability for Equally Spaced Data: Newton's Forward Differences work well when the
data points are equally spaced, making it a practical choice for certain types of data sets.

Disadvantages:
Sensitivity to Data Perturbations: Newton's Forward Differences can be sensitive to small
changes in the input data, which might lead to significant variations in the interpolated
values.

Limited Applicability: The method is specifically designed for equally spaced data points,
and its accuracy may decrease if the data is not uniformly distributed.

file:///C:/Users/Computer Lab1/Downloads/Asaad (1).html 1/5


12/7/23, 5:18 PM Asaad

High-Degree Polynomials: As the degree of the interpolating polynomial increases, it may


oscillate wildly between data points, leading to numerical instability. This is known as
Runge's phenomenon.

In summary, Newton's Forward Differences is a useful interpolation method for equally spaced
data, offering simplicity and efficiency. However, its sensitivity to data perturbations and
limitations in handling unevenly spaced data are important considerations. It's essential to
choose interpolation methods based on the specific characteristics of the given data.

Newton's Backward Differences:


Newton's Backward Differences is another numerical method for polynomial interpolation,
similar to Newton's Forward Differences. Like the forward differences method, it is named after
Sir Isaac Newton and is particularly useful when the data points are equally spaced. The main
difference lies in the way differences are calculated. In Newton's Backward Differences, backward
differences are used.

Advantages:
Ease of Computation: Similar to Newton's Forward Differences, the backward differences
method is relatively easy to compute, and the differences can be calculated systematically.

Efficiency: Once the divided differences are calculated, evaluating the interpolating
polynomial at different points is computationally efficient.

Suitability for Equally Spaced Data: Newton's Backward Differences work well when the
data points are equally spaced, making it a practical choice for certain types of data sets.

Disadvantages:
Sensitivity to Data Perturbations: Similar to Newton's Forward Differences, the backward
differences method can be sensitive to small changes in the input data, leading to variations
in the interpolated values.

Limited Applicability: The method is specifically designed for equally spaced data points,
and its accuracy may decrease if the data is not uniformly distributed.

High-Degree Polynomials: As the degree of the interpolating polynomial increases, it may


oscillate wildly between data points, leading to numerical instability (Runge's phenomenon).

In summary, Newton's Backward Differences is a useful interpolation method for equally spaced
data, offering simplicity and efficiency. However, its sensitivity to data perturbations and
limitations in handling unevenly spaced data are important considerations. It's crucial to choose
interpolation methods based on the specific characteristics of the given data.

file:///C:/Users/Computer Lab1/Downloads/Asaad (1).html 2/5


12/7/23, 5:18 PM Asaad

Code:
In [7]:
import numpy as np
import matplotlib.pyplot as plt

def forward_differences(x, y):


n = len(y)
F = np.zeros((n, n))
F[:, 0] = y

for j in range(1, n):


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

return F

def backward_differences(x, y):


n = len(y)
B = np.zeros((n, n))
B[:, 0] = y

for j in range(1, n):


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

return B

def newton_forward(x, y, xi):


n = len(x)
h = x[1] - x[0]
t = (xi - x[0]) / h

F = forward_differences(x, y)
result = y[0]
term = 1

equations = [f"{y[0]:.2f}"]
for i in range(1, n):
term *= (t - i + 1) / i
result += term * F[0, i]
equations.append(f"{term * F[0, i]:.2f}")

return result, equations

def newton_backward(x, y, xi):


n = len(x)
h = x[1] - x[0]
t = (xi - x[-1]) / h

B = backward_differences(x, y)
result = y[-1]
term = 1

equations = [f"{y[-1]:.2f}"]
for i in range(1, n):
term *= (t + i - 1) / i
result += term * B[n - i - 1, i]
equations.append(f"{term * B[n - i - 1, i]:.2f}")

return result, equations

# Sample data

file:///C:/Users/Computer Lab1/Downloads/Asaad (1).html 3/5


12/7/23, 5:18 PM Asaad
x_values = [0, 1, 2, 3, 4]
y_values = [11, -2, 13, -5, -18]

# User input for interpolation value


x_interp_forward = float(input("Enter the interpolation value for forward interpolat
x_interp_backward = float(input("Enter the interpolation value for backward interpol

# Calculate Pn(x) using Newton's forward differences


result_forward, equations_forward = newton_forward(x_values, y_values, x_interp_forw

# Calculate Pn(x) using Newton's backward differences


result_backward, equations_backward = newton_backward(x_values, y_values, x_interp_b

# Displaying results in tabular form


print("\nForward Interpolation:")
print(f"Interpolation value at x = {x_interp_forward}: {result_forward}")
print("Equations used:")
for i, eq in enumerate(equations_forward):
print(f"Term {i}: {eq}")

print("\nBackward Interpolation:")
print(f"Interpolation value at x = {x_interp_backward}: {result_backward}")
print("Equations used:")
for i, eq in enumerate(equations_backward):
print(f"Term {i}: {eq}")

# Plotting Pn(x) along with original data points


x_plot = np.linspace(min(x_values), max(x_values), 100)
y_plot_forward = [newton_forward(x_values, y_values, xi)[0] for xi in x_plot]
y_plot_backward = [newton_backward(x_values, y_values, xi)[0] for xi in x_plot]

plt.figure(figsize=(8, 6))
plt.scatter(x_values, y_values, label='Original Data Points')
plt.plot(x_plot, y_plot_forward, label='Newton Forward Interpolation', linestyle='--
plt.plot(x_plot, y_plot_backward, label='Newton Backward Interpolation', linestyle='
plt.scatter([x_interp_forward], [result_forward], color='red')
plt.scatter([x_interp_backward], [result_backward], color='blue')
plt.xlabel('x')
plt.ylabel('Pn(x)')
plt.title('Newton Interpolation')
plt.legend()
plt.grid(True)
plt.show()

Enter the interpolation value for forward interpolation: 3


Enter the interpolation value for backward interpolation: 3

Forward Interpolation:
Interpolation value at x = 3.0: -5.0
Equations used:
Term 0: 11.00
Term 1: -39.00
Term 2: 84.00
Term 3: -61.00
Term 4: 0.00

Backward Interpolation:
Interpolation value at x = 3.0: -5.0
Equations used:
Term 0: -18.00
Term 1: 13.00
Term 2: -0.00
Term 3: -0.00
Term 4: -0.00

file:///C:/Users/Computer Lab1/Downloads/Asaad (1).html 4/5


12/7/23, 5:18 PM Asaad

file:///C:/Users/Computer Lab1/Downloads/Asaad (1).html 5/5

You might also like