Lab 7 - Adv Math
Lab 7 - Adv Math
EXERCISE #
7
Lagrange Interpolation
I. OBJECTIVES:
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.
1. With the given function below for Lagrange Interpolation, copy the code and
understand its algorithm.
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:
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)
3. Plot the system of equation by using Lagrange Interpolation and display the
value of f(10)
4. Plot the system of equation by using Lagrange Interpolation and display the
value of f(7)
5. Plot the system of equation by using Lagrange Interpolation and display the
value of f(10)
1. import numpy as np
import matplotlib.pyplot as plt
sorted_indices = np.argsort(x_points)
x_points = x_points[sorted_indices]
y_points = y_points[sorted_indices]
x=1
sorted_indices = np.argsort(x_points)
x_points = x_points[sorted_indices]
y_points = y_points[sorted_indices]
x=10
4. import numpy as np
import matplotlib.pyplot as plt
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
sorted_indices = np.argsort(x_points)
x_points = x_points[sorted_indices]
y_points = y_points[sorted_indices]
x=10