Numerical Analysis I

MATH 345 : Fall 2023

Department of Mathematics - SUNY Geneseo
⇐ Back

Homework 4 - Lagrange Interpolation

Due Date: October 26, 2023

Upload

Problems

  1. Consider the \((x,y)\) data \((-2, 1), (-1,\frac{5}{2}), (0, 5), (1, \frac{5}{2}), (2,1)\). Let \begin{equation*}P(x) = \sum_{k=0}^4 y_k L_k(x) \end{equation*} be the interpolating polynomial of the data.
    1. Compute the polynomials \(L_0,L_1,L_2,L_3,L_4\).
    2. Compute \(P(-0.5)\) and \(P(1.5)\).
  2. Repeat the procedure from the previous problem using the Chebyshev nodes \(x_0,x_1,x_2,x_3,x_4\) on the interval \([-2,2]\). For the \(y\)-values, use \(y_j = 5/(1+x_j^2)\), for \(j=0,1,2,3,4\). What is \(P(-0.5)\) and \(P(1.5)\) in this case?
  3. Create a Python function named LagrangeInterp that takes as input a list of nodes \(x=(x_0,x_1,\ldots,x_n)\), a list of \(y\)-values \(y=(y_0,y_1,\ldots,y_n)\), and a set of query points \(u=(u_1,u_2,\ldots,u_N)\), and returns as output a list of points \(v=(v_1,v_2,\ldots,v_N)\) where \(v_i\) is the value of the \(n\)th order Lagrange polynomial at the query point \(u_i\), i.e., \(v_i = P(u_i)\). Hence, a call to your function will look like:
    v = LagrangeInterp(x, y, u)
    Test your function with \(f(x)=\frac{1}{x^2}\), \(x = [1, 1.75, 3]\), \(y = [f(1), f(1.75), f(3)]\), and \(u = [1.5, 2]\). You should get \(v = [0.5057, 0.1927]\).
  4. Create a Python function named ChebyshevNodes that takes as input the end-points of an interval \([a,b]\) and a natural number \(n\geq 2\), and outputs the Chebyshev nodes \(x_0, x_1, \ldots, x_n\) on the interval \([a,b]\). Hence, a call to your function will look like:
    x = ChebyshevNodes(a, b, n)
  5. Consider the function \begin{equation*}f(x)=3\cos(2x)-\sin(0.5x)+3\sin(3.3x)+0.5\sin(10x). \end{equation*} on the interval \([-2,4]\). Open a new Python script, load the usual modules, and then create a new cell block:
    import numpy as np
    import matplotlib.pyplot as plt
    
    #%% The following block is for equally spaced nodes
    
    A quick way to define a function in Python is to use a lambda function:
    f = lambda x: 2*np.cos(2*x) - np.sin(0.5*x) + 3*np.sin(3.3*x) + 0.5*np.sin(10*x)
    For \(n=8\), create equally spaced nodes on the interval \([-2,4]\):
    a = -2
    b = 4
    n = 8
    x = np.linspace(a, b, n+1)
    The command np.linspace(a,b,n+1) creates \(n+1\) equally spaced points between \(a\) and \(b\). Next, use your LagrangeInterp function to evaluate \(P(x)\) at the the query points \(u=(-2, -1.99, -1.98, \ldots, 3.99, 4)\):
    u = np.linspace( a, b, 600)
    v = LagrangeInterp(x, f(x), u)
    Plot the set of points \((u_i, f(u_i))\), \((u_i, v_i)\), and the given data points \((x_i, y_i)\) all on the same plot. Give a title and legend to your plot, and save the figure as a PDF file:
    plt.plot(u, f(u), u, v)
    plt.plot(x, f(x), ".k", markersize=10)
    plt.legend( [ "f(x)", "P(x)" ] )
    plt.title( "Interpolation of f(x) with equally spaced nodes, n = " + str(n) )
    plt.savefig( "equal-spaced-n" + str(n) + ".pdf" )
    Now Compute the maximum error \(|f(u_i) - P(u_i) |\) over all \(i\):
    np.max( np.abs(f(u) - v) )
    What is the maximum error? Now repeat the entire process with \(n=16\) and then with \(n=32\) and report the maximum error for each case. Note that the only thing you have to change is where \(n\) is defined in your script and then all you need to do is run the script. Make sure to close the figure window before you run the script with a new \(n\).
  6. In this problem, we will repeat the procedure from the previous problem but instead use the Chebyshev nodes on the interval \([-2,4]\). For each \(n\in \{8, 16, 32\}\) do the following in Python. First create a new cell block in your script:
    #%% This section uses Chebyshev nodes.
    Next, create the Chebyshev nodes \(x_0, x_1,\ldots,x_n\):
    n = 8
    x = ChebyshevNodes(a, b, n)
    Now compute \(v=(v_1,v_2,\ldots,v_N)\) with your interpolating function:
    v = LagrangeInterp(x, f(x), u)
    Plot and save as before, this time changing the title:
    plt.plot(u, f(u), u, v)
    plt.plot(x, f(x), ".k", markersize=10)
    plt.legend( [ "f(x)", "P(x)" ] )
    plt.title( "Interpolation of f(x) with Chebyshev nodes, n = " + str(n) )
    plt.savefig( "Chebyshev-n" + str(n) + ".pdf" )
    Lastly, compute the maximum error \(|f(u_i)-P(u_i)|\) over all \(i\):
    np.max( np.abs(f(u) - v) )
    What is the maximum error? Now repeat the entire process with \(n=16\) and then with \(n=32\) and report the maximum error for each case. Comment on any improvements that you see with Chebyshev nodes over equally-spaced nodes.