Uploaded by FELIPE NICOLAS BILDER CORDERO

Optional Assignment 02.ipynb - Colaboratory

advertisement
Optional_Assignment_02.ipynb - Colaboratory
1 of 4
https://colab.research.google.com/drive/1G-7I_XOMFDs8JSTyk95gbh...
 Optional Assignment #2
Part I: Implementation
Given an arbitrary dataset (xi , yi ), i = 0, … , n, develop Python functions to perform
polynomial interpolation using three different techniques:
1. Monomial interpolation.
2. Lagrange polynomial interpolation.
3. Divided differences interpolation.
Part II: Testing
Test your implementation on the dataset {(0, 1), (1, 2), (2, 1.5), (3, 3), (4, 2.5)} . For each
method, visualize the dataset as points and the interpolated function as a solid line on the same
plot.
import numpy as np
from matplotlib import pyplot as plt
def lagrange_interpolation(points):
n = len(points)
polynomial = np.poly1d(0)
for i in range(n):
xi, yi = points[i]
term = np.poly1d(yi)
for j in range(n):
if i != j:
xj, _ = points[j]
term *= np.poly1d([1, -xj]) / (xi - xj)
polynomial += term
return polynomial
def monomial_interpolation(points):
n = len(points)
A = np.zeros((n, n))
b = np.zeros(n)
for i in range(n):
xi, yi = points[i]
b[i] = yi
for j in range(n):
A[i, j] = xi ** j
3/20/2024, 11:18 PM
Optional_Assignment_02.ipynb - Colaboratory
2 of 4
https://colab.research.google.com/drive/1G-7I_XOMFDs8JSTyk95gbh...
# Solve the system of equations to find coefficients
coefficients = np.linalg.solve(A, b)
return np.poly1d(coefficients[::-1]) # Coefficients need to be reversed for np.poly1d
def divided_differences_interpolation(x, y):
n = len(x)
if len(set(x)) != n:
raise ValueError("Duplicate values of x are not allowed.")
# Calculate divided differences table
table = [[None] * n for _ in range(n)]
for i in range(n):
table[i][0] = y[i]
for j in range(1, n):
for i in range(n - j):
table[i][j] = (table[i + 1][j - 1] - table[i][j - 1]) / (x[i + j] - x[i])
# Construct interpolating polynomial
def interpolating_polynomial(x_val):
result = 0
for i in range(n):
term = table[0][i]
for j in range(i):
term *= (x_val - x[j])
result += term
return result
return interpolating_polynomial
# Dataset
dataset = {
0: 1,
1: 2,
2: 1.5,
3: 3,
4: 2.5
}
x_data = list(dataset.keys())
y_data = list(dataset.values())
3/20/2024, 11:18 PM
Optional_Assignment_02.ipynb - Colaboratory
3 of 4
https://colab.research.google.com/drive/1G-7I_XOMFDs8JSTyk95gbh...
# Interpolation
interpolating_function = divided_differences_interpolation(x_data, y_data)
# Visualization
x_values = np.linspace(min(x_data), max(x_data), 100)
y_values = [interpolating_function(x_val) for x_val in x_values]
plt.figure(figsize=(8, 6))
plt.plot(x_values, y_values, label='Interpolated Function', color='blue')
plt.scatter(x_data, y_data, label='Data Points', color='red')
plt.title('Divided Differences Interpolation')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True)
plt.show()
# Given points
points = [(0, 1), (1, 2), (2, 1.5), (3, 3), (4, 2.5)]
# Creating Lagrange polynomial
polynomial = lagrange_interpolation(points)
print("Lagrange Polynomial:")
print(polynomial)
polynomial=monomial_interpolation((points))
print("Monomial Interpolating Polynomial:")
print(polynomial)
3/20/2024, 11:18 PM
Optional_Assignment_02.ipynb - Colaboratory
4 of 4
https://colab.research.google.com/drive/1G-7I_XOMFDs8JSTyk95gbh...
Lagrange Polynomial:
4
3
2
-0.3125 x + 2.458 x - 5.938 x + 4.792 x + 1
Monomial Interpolating Polynomial:
4
3
2
-0.3125 x + 2.458 x - 5.938 x + 4.792 x + 1
3/20/2024, 11:18 PM
Download