Objective: This activity is designed to help you understand the concept of Lagrange interpolation, a polynomial interpolation method used in numerical analysis and applied mathematics.
Material:
- Internet access
- Pen and paper
- A computer for running Python code
Instructions:
Part 1: Understanding Lagrange Interpolation
- Research: Start by researching the definition and basics of Lagrange interpolation. Some key points to understand are:
- What is Lagrange interpolation?
- How does it work?
- What is its mathematical formula?
- When is it used?
- Conceptual Understanding: Once you’ve read about Lagrange interpolation, try to explain the concept to someone else or write down your understanding. This helps consolidate your learning.
- Examples: Look up examples of Lagrange interpolation. This could be mathematical examples where you’re given a set of points and you need to find the interpolating polynomial using the Lagrange formula.
Part 2
- Setup your development environment. Open your web browser and go to https://replit.com/. Sign up for a new account if you don’t already have one, or log in.
- Start a new Python project. Click on the “+ New Repl” button on the top right, select “Python” as the language, give your project a name, and click on “Create Repl”.
- Write the Lagrange interpolation function. In the code editor, write a function named
lagrange_interpolation
. This function should take in three parameters: a list of x values (x_values
), a list of y values (y_values
), and the x value to interpolate (x
). Use the provided code snippet for this step:
def lagrange_interpolation(x_values, y_values, x):
n = len(x_values)
y = 0
for i in range(n):
p = 1
for j in range(n):
if i != j:
p *= (x - x_values[j]) / (x_values[i] - x_values[j])
y += y_values[i] * p
return y
4. Test the Lagrange interpolation function. Still in the same Python file, create a list of x values (x_values
) and a list of y values (y_values
). Call the lagrange_interpolation
function with these lists and the x value to interpolate. Print the result. Use the provided code snippet for this step:
x_values = [0, 1, 2, 3]
y_values = [1, 2, 1, 10]
print(lagrange_interpolation(x_values, y_values, 2.5)) # Outputs: 3.75
- Run the code. Click the ‘Run’ button at the top of the page. The output of the code will be displayed on the right side of the page.
- Analyze the results.
- Try changing the
x
value in thelagrange_interpolation
function call. What happens when you interpolate at a value of x that is within your list of x values? What happens when you interpolate at a value of x that is outside your list of x values? - Try changing the
y_values
. How do the results change when you alter these values?
- Try changing the
- Write a short report. After performing the above steps, write a short report explaining your findings. Include the answers to the questions asked in the analysis step. Also, discuss any challenges you faced and how you overcame them.