0% found this document useful (0 votes)
4 views

Lab 7 - Adv Math

Uploaded by

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

Lab 7 - Adv Math

Uploaded by

danger gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Name: Date Performed:

Group: Date Submitted:

EXERCISE #

7
Lagrange Interpolation

I. OBJECTIVES:

1. To grasp the theoretical foundations of Lagrange Interpolation and its


application in solving systems of linear equations
2. Learn how to translate the mathematical steps of Lagrange Interpolation into
a Python program.
3. Apply the implemented algorithm to solve various systems of linear
equations.
4. Identify and resolve issues in the implementation, and verify the correctness
of the solutions.
5.
II. INSTRUCTION:

1. Open Jupyter Notebook or any IDE for Python on your computer.

2. Use Python code to implement the Lagrange Interpolation method for finding
the root of the given function.
3. Record your observations and answers in the spaces provided.

PROCEDURE 1. Understanding the Lagrange Interpolation Method

1. With the given function below for Lagrange Interpolation, copy the code and
understand its algorithm.

def lagrange_basis(x, i, x_points):


basis = 1
for j in range(len(x_points)):
if j != i:
basis *= (x - x_points[j]) / (x_points[i] - x_points[j])
return basis

def lagrange_interpolation(x, x_points, y_points):


result = 0
for i in range(len(x_points)):
result += y_points[i] * lagrange_basis(x, i, x_points)
return result
2.

a) To plot this system of equation by using the function in #1, use the following
code:

Ex: x 0 1 2 3 4
f(x 1 2 0 2 1
)
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
# Define the known data points
x_points = np.array([0, 1, 2, 3, 4])
# Define=the
y_points known data
np.array([1, 2, points
0, 2, 1])
x_points = np.array([0, 1, 2, 3, 4])
y_points
# = np.array([1,
Interpolate a new set 2,
of 0, 2, 1])
points
x_new = np.linspace(0, 4, 100)
# Calculate
y_new f(x)
= [lagrange_interpolation(x, x_points, y_points) for x
f_x = lagrange_interpolation(x,
in x_new] x_points, y_points)
print(f"The value of f(x) is: {f_x}")
# Plot the results
plt.scatter(x_points, y_points, color='red', label='Data
Points')
plt.plot(x_new, y_new, label='Lagrange Interpolation')
plt.legend()
plt.show()
b) To estimate the f(x) value of the system in #2 by using the function in #1,
use the following code:

PROCEDURE 2. PROBLEM SOLVING

Reuse or rewrite the code whatever the requirement is: Put all your answer in the
Data and Results with Codes section.

1. Plot the system of equation by using Lagrange Interpolation and display the
value of f(5)

(x0, y0) = (9, 2)


(x1, y1) = (3, 10)
2. Plot the system of equation by using Lagrange Interpolation and display the
value of f(1)

(x0, y0) = (1, 6)


(x1, y1) = (3, 4)
(x2, y2) = (2, 5)

3. Plot the system of equation by using Lagrange Interpolation and display the
value of f(10)

(x0, y0) = (9, 6)


(x1, y1) = (3, 5)
(x2, y2) = (1, 12)

4. Plot the system of equation by using Lagrange Interpolation and display the
value of f(7)

(x0, y0) = (1, 10)


(x1, y1) = (2, 4)
(x2, y2) = (3, 4)
(x3, y3) = (5, 7)

5. Plot the system of equation by using Lagrange Interpolation and display the
value of f(10)

(x0, y0) = (5, 12)


(x1, y1) = (6, 13)
(x2, y2) = (7, 14)
(x3, y3) = (8, 15)

Data and Results with Codes:

1. import numpy as np
import matplotlib.pyplot as plt

def lagrange_basis(x, i, x_points):


basis = 1
for j in range(len(x_points)):
if j != i:
basis *= (x - x_points[j]) / (x_points[i] - x_points[j])
return basis

def lagrange_interpolation(x, x_points, y_points):


result = 0
for i in range(len(x_points)):
result += y_points[i] * lagrange_basis(x, i, x_points)
2. import numpy as np
import matplotlib.pyplot as plt

def lagrange_basis(x, i, x_points):


basis = 1
for j in range(len(x_points)):
if j != i:
basis *= (x - x_points[j]) / (x_points[i] - x_points[j])
return basis

def lagrange_interpolation(x, x_points, y_points):


result = 0
for i in range(len(x_points)):
result += y_points[i] * lagrange_basis(x, i, x_points)
return result
x_points = np.array([1, 3, 2])
y_points = np.array([6, 4, 5])

sorted_indices = np.argsort(x_points)
x_points = x_points[sorted_indices]
y_points = y_points[sorted_indices]

x_new = np.linspace(min(x_points), max(x_points), 100)


y_new = [lagrange_interpolation(x, x_points, y_points) for x in x_new]

plt.scatter(x_points, y_points, color='red', label='Data Points')


plt.plot(x_new, y_new, label='Lagrange Interpolation')
plt.legend()
plt.show()

x=1

f_x = lagrange_interpolation(x, x_points, y_points)


3. import numpy as np
import matplotlib.pyplot as plt

def lagrange_basis(x, i, x_points):


basis = 1
for j in range(len(x_points)):
if j != i:
basis *= (x - x_points[j]) / (x_points[i] - x_points[j])
return basis

def lagrange_interpolation(x, x_points, y_points):


result = 0
for i in range(len(x_points)):
result += y_points[i] * lagrange_basis(x, i, x_points)
return result

x_points = np.array([9, 3, 1])


y_points = np.array([6, 5, 12])

sorted_indices = np.argsort(x_points)
x_points = x_points[sorted_indices]
y_points = y_points[sorted_indices]

x_new = np.linspace(min(x_points), max(x_points), 100)


y_new = [lagrange_interpolation(x, x_points, y_points) for x in x_new]

plt.scatter(x_points, y_points, color='red', label='Data Points')


plt.plot(x_new, y_new, label='Lagrange Interpolation')
plt.legend()
plt.show()

x=10

f_x = lagrange_interpolation(x, x_points, y_points)


print(f"The value of f(1) is: {f_x}")

4. import numpy as np
import matplotlib.pyplot as plt

def lagrange_basis(x, i, x_points):


basis = 1
for j in range(len(x_points)):
if j != i:
basis *= (x - x_points[j]) / (x_points[i] - x_points[j])
return basis

def lagrange_interpolation(x, x_points, y_points):


result = 0
for i in range(len(x_points)):
result += y_points[i] * lagrange_basis(x, i, x_points)
return result

x_points = np.array([1, 2, 3, 5])


y_points = np.array([10, 4, 4, 7])

sorted_indices = np.argsort(x_points)
x_points = x_points[sorted_indices]
y_points = y_points[sorted_indices]
5. import numpy as np
import matplotlib.pyplot as plt

def lagrange_basis(x, i, x_points):


basis = 1
for j in range(len(x_points)):
if j != i:
basis *= (x - x_points[j]) / (x_points[i] - x_points[j])
return basis

def lagrange_interpolation(x, x_points, y_points):


result = 0
for i in range(len(x_points)):
result += y_points[i] * lagrange_basis(x, i, x_points)
return result

x_points = np.array([5, 6, 7, 8])


y_points = np.array([12, 13, 14, 15])

sorted_indices = np.argsort(x_points)
x_points = x_points[sorted_indices]
y_points = y_points[sorted_indices]

x_new = np.linspace(min(x_points), max(x_points), 100)


y_new = [lagrange_interpolation(x, x_points, y_points) for x in x_new]

plt.scatter(x_points, y_points, color='red', label='Data Points')


plt.plot(x_new, y_new, label='Lagrange Interpolation')
plt.legend()
plt.show()

x=10

f_x = lagrange_interpolation(x, x_points, y_points)


print(f"The value of f(10) is: {f_x}")

You might also like