7.8 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 HEREor "YOUR ANSWER HERE".
Submission¶
- Name all team members in the the cell below
- make sure everything runs as expected
- restart the kernel (in the menubar, select Kernel$\rightarrow$Restart)
- run all cells (in the menubar, select Cell$\rightarrow$Run All)
- Check all outputs (Out[*]) for errors and resolve them if necessary
- submit your solutions in time (before the deadline)
Polynomial Interpolation¶
In the following you will construct the interpolating polynomial for the pairs $x_k = [2,3,4,5,6]$ and $y_k = [2,5,5,5,6]$ using the "inversion method" via the Vandermonde matrix, as discussed in the lecture.
# Import packages here ... # YOUR CODE HERE raise NotImplementedError()
Task 1¶
Set up the Vandermonde matrix and calculate its determinant using $\text{numpy.linalg.det()}$.
def GetVDMMatrix(xk): """Don't forget to write a docstring ... """ # YOUR CODE HERE raise NotImplementedError()
# print the Vandermonde matrix here in an appropriate format and calculate the determinant # YOUR CODE HERE raise NotImplementedError()
"""Check that GetVDMMatrix returns the correct output""" assert np.allclose( GetVDMMatrix([2.0, 4.0]), [[1.0, 2.0], [1.0, 4.0]] )
Task 2¶
Write a function that constructs the interpolating polynomial for $\text{x}$ (a user-defined array of $x$ values) using the Vandermonde matrix from the previous task and $\text{numpy.linalg.inv()}$.
def interpVDM(xk, yk, x): # YOUR CODE HERE raise NotImplementedError()
"""Check that interpVDM returns the correct output""" assert np.allclose( interpVDM([1.0, 3.0], [4.0, 6.0], [2.5]), [5.5] ) assert np.allclose( interpVDM([5.0, 14.0], [12.0, -3.0], [6.5]), [9.5] )
Task 3¶
Plot the interpolating polynomial from $x=2$ to $x=6$ using $x$-step-sizes of $0.01$.
# YOUR CODE HERE raise NotImplementedError()