Files
cds-numerical-methods/Week 1/03 Lagrange Polynomial Interpolation.ipynb

8.7 KiB

CDS: Numerical Methods Assignments

  • See lecture notes and documentation on Brightspace for Python and Jupyter basics. If you are stuck, try to google or get in touch via Discord.

  • Solutions must be submitted via the Jupyter Hub.

  • Make sure you fill in any place that says YOUR CODE HERE or "YOUR ANSWER HERE".

Submission

  1. Name all team members in the the cell below
  2. make sure everything runs as expected
  3. restart the kernel (in the menubar, select Kernel$\rightarrow$Restart)
  4. run all cells (in the menubar, select Cell$\rightarrow$Run All)
  5. Check all outputs (Out[*]) for errors and resolve them if necessary
  6. submit your solutions in time (before the deadline)
team_members = ""

Lagrange Polynomial Interpolation

In [ ]:
# Import packages here ...

# YOUR CODE HERE
raise NotImplementedError()

Task 1

Write your own Lagrange polynomial interpolation routine which calculates

$$P(x) = \sum_{k=0}^n f(x_k) L_{n,k}(x)$$

Start with a function $\text{myLagrange(xk, yk, x)}$ which internally calls another function $\text{myLagrangePolynomials(xk, k, x)}$ that generates the Lagrange interpolation polynomials

$$L_{n,k}(x) = \prod_{\substack{i=0 \\ i\neq k}}^n \frac{x-x_i}{x_k - x_i}$$

Here $\text{xk}$ and $\text{yk}$ are arrays of the same size representing the $(x_k, y_k=f(x_k))$ pairs which we like to interpolate and $\text{x}$ is a user-defined array of $x$ values.

In [ ]:
def myLagrangePolynomials(xk, k, x):
    """Don't forget to write a docstring ...
    """
    
    L = np.ones(np.size(x), dtype=np.float64)
    
    # YOUR CODE HERE
    raise NotImplementedError()
    
    return L

def myLagrange(xk, yk, x):
    """Don't forget to write a docstring ...
    """
    p = np.zeros(np.size(x), dtype=np.float64)
    
    # YOUR CODE HERE
    raise NotImplementedError()
    
    return p

Task 2

Use your Lagrange interpolation routine to construct the interpolating polynomial for the pairs $x_k = [2,3,4,5,6]$ and $y_k = [2,5,5,5,6]$ and plot it from $x=2$ to $x=6$ using $x$-step-sizes of $0.01$.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()

Task 3

Make sure your result is identical to the one obtained from Scipy's Lagrange function $\text{scipy.interpolate.lagrange()}$ and to the results obtained in Task 1.3. Plot all results in the same figure.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()

Task 4

Tests are used to verify that your functions do what they are supposed to do. They can for example be used to make sure the code still returns correct value after changes have been made. Here we will use assertions to implement simple tests.

For example, the following code implements a test for the function $\text{sqr}$

def sqr(a):
    return a**2.0

def test_sqr():
    assert np.allclose(sqr(0), 0.0)
    assert np.allclose(sqr(5), 25.0)
    assert np.allclose(sqr(-3), 9.0)

test_sqr()

Implement a simple test function to test your Langrange interpolation routine.

In [ ]:
def test_myLagrange():
    """ don't forget to write a doc string ...
    """
    # YOUR CODE HERE
    raise NotImplementedError()
    
test_myLagrange()