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

326 lines
7.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "4ec40081b048ce2f34f3f4fedbb0be10",
"grade": false,
"grade_id": "cell-98f724ece1aacb67",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"# CDS: Numerical Methods Assignments\n",
"\n",
"- 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.\n",
"\n",
"- Solutions must be submitted via the Jupyter Hub.\n",
"\n",
"- Make sure you fill in any place that says `YOUR CODE HERE` or \"YOUR ANSWER HERE\".\n",
"\n",
"## Submission\n",
"\n",
"1. Name all team members in the the cell below\n",
"2. make sure everything runs as expected\n",
"3. **restart the kernel** (in the menubar, select Kernel$\\rightarrow$Restart)\n",
"4. **run all cells** (in the menubar, select Cell$\\rightarrow$Run All)\n",
"5. Check all outputs (Out[\\*]) for errors and **resolve them if necessary**\n",
"6. submit your solutions **in time (before the deadline)**"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"team_members = \"\""
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "8d0c880a11cb08c365c8a5a0bc8750c1",
"grade": false,
"grade_id": "cell-1b0eaa56e11e9ee9",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Polynomial Interpolation\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "2feb26161cc0cb2cd68dfcdb824c5034",
"grade": true,
"grade_id": "cell-03d68671ffed6d3f",
"locked": false,
"points": 0,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# Import packages here ...\n",
"\n",
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "8471a9dfca81ede1c2a5a3e96bd9a5b1",
"grade": false,
"grade_id": "cell-a043a4a71f63e04c",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 1\n",
"\n",
"Set up the Vandermonde matrix and calculate its determinant using $\\text{numpy.linalg.det()}$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "75732084734cc585054a50eb35a45827",
"grade": true,
"grade_id": "cell-5422fc642d5dd1a2",
"locked": false,
"points": 3,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"def GetVDMMatrix(xk):\n",
" \"\"\"Don't forget to write a docstring ...\n",
" \"\"\"\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "9a83af8015896074e35c9a31957ea086",
"grade": true,
"grade_id": "cell-20597ab664ee3afc",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# print the Vandermonde matrix here in an appropriate format and calculate the determinant\n",
"\n",
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "b41de80100548f69650f668d59f707c2",
"grade": false,
"grade_id": "cell-05663ca6826fc2c9",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that GetVDMMatrix returns the correct output\"\"\"\n",
"assert np.allclose( GetVDMMatrix([2.0, 4.0]), [[1.0, 2.0], [1.0, 4.0]] )"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "96ebac1440af331d7a371bd30481acad",
"grade": false,
"grade_id": "cell-f82a1e148106b1b2",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 2\n",
"\n",
"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()}$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "ecc32ad2047b7640f5aa1468928cd40b",
"grade": true,
"grade_id": "cell-2a4f7faec05fc668",
"locked": false,
"points": 3,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"def interpVDM(xk, yk, x):\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "e55a10a7432f2d237665d21ef40eeeab",
"grade": false,
"grade_id": "cell-acca6ad272b2c314",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that interpVDM returns the correct output\"\"\"\n",
"assert np.allclose( interpVDM([1.0, 3.0], [4.0, 6.0], [2.5]), [5.5] )\n",
"assert np.allclose( interpVDM([5.0, 14.0], [12.0, -3.0], [6.5]), [9.5] )"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "e8bd4c442b5a3c4a460cbfe6dcf7902b",
"grade": false,
"grade_id": "cell-fbcb6833bbbfd55b",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 3\n",
"\n",
"Plot the interpolating polynomial from $x=2$ to $x=6$ using $x$-step-sizes of $0.01$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "7611dec42118b986d676adb6f74acce9",
"grade": true,
"grade_id": "cell-ee4e03d4534c01b4",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}