Initial commit with templates

This commit is contained in:
2022-02-03 09:59:28 +01:00
commit b908cfa2e9
6 changed files with 1547 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.ipynb_checkpoints

View File

@ -0,0 +1,136 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "f5670d18",
"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",
"id": "b4d38256",
"metadata": {},
"source": [
"team_members = \"\""
]
},
{
"cell_type": "markdown",
"id": "18455dd1",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "c456df7b5cf52f0d051cc84cad93236f",
"grade": false,
"grade_id": "cell-test",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Task 0\n",
"\n",
"This is how a task will look like ..."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "819e01f1",
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "16f1fabf9d014928f751bd29f3ba4273",
"grade": true,
"grade_id": "cell-67a8ece92150e416",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [
{
"ename": "NotImplementedError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_3976074/1067236896.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# YOUR CODE HERE\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNotImplementedError\u001b[0m: "
]
}
],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86e78dbb",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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": 5
}

View File

@ -0,0 +1,468 @@
{
"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": "dc812007347efe07d1810b04b82aecc2",
"grade": false,
"grade_id": "cell-aceb5d89f37bc6d8",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Rounding and Truncation Error Analysis\n",
"\n",
"Euler's number $e$ can be represented as the infinite series $e = \\sum_{n=0}^{\\infty} \\frac{1}{n!}$. In order to evaluate it in Python we need to truncate the series. Furthermore, we learned that every number representation and floating-point operation introduces a finite error. Thus, let's analyze the truncated series \n",
"\n",
"$$\\tilde{e} = \\sum_{n=0}^{N} \\frac{1}{n!}$$ \n",
"\n",
"in more detail."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "a9566335240ff82540477cab102633c8",
"grade": true,
"grade_id": "cell-5bceef638759678c",
"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": "b3c9dca0773c20141a49ea997ed80b08",
"grade": false,
"grade_id": "cell-ca1da7ba4e529d85",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 1\n",
"\n",
"Calculate $\\tilde{e}$ with Python and plot the relative error $ \\delta = \\left| \\frac{\\tilde{e} - e}{e} \\right|$ as a function of $N$ (use a log-scale for the $y$ axis)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "96d02220ab1baff89c6a0b55c2a51989",
"grade": true,
"grade_id": "cell-c9a37605f309b484",
"locked": false,
"points": 3,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"def getEuler(N):\n",
" \"\"\"Don't forget to write a docstring ...\n",
" \"\"\"\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()\n",
"\n",
"def getEulerErr(eApprox):\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": "b68691bcdc667dcc59baeb0066264df0",
"grade": true,
"grade_id": "cell-08269af9f0d0215f",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# do your plotting here ...\n",
"\n",
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "603875c052d6cd5387018a586cc12517",
"grade": false,
"grade_id": "cell-99271f78a2313ff7",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that getEuler and getEulerErr return the correct output for several inputs\"\"\"\n",
"assert np.allclose(getEuler(0), 1)\n",
"assert np.allclose(getEuler(1), 2)\n",
"assert np.allclose(getEuler(2), 2.5)\n",
"assert np.allclose(getEuler(10), np.e)\n",
"\n",
"assert np.allclose(getEulerErr(0), 1, rtol=1e-16)\n",
"assert np.allclose(getEulerErr(np.e), 0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "4bd01bcb178d7114a2fa2d190ee9901e",
"grade": false,
"grade_id": "cell-94d8858f406f2f38",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 2 \n",
"\n",
"Compare the relative errors $\\delta$ for different floating point precisions as a function of $N$. To this end, we define each element of the series $e_n = \\frac{1}{n!}$ and convert it to double-precision (64 bit) and single-precision (32 bit) floating points **before** adding them up. This can be done by using Numpy's functions $\\text{numpy.float64(e_n)}$ and $\\text{numpy.float32(e_n)}$, respectively."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "0a6f03fcf93ee75868d35fa49c0c9df1",
"grade": true,
"grade_id": "cell-295e934126bd083b",
"locked": false,
"points": 3,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"def getEulerSinglePrecision(N):\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()\n",
"\n",
"def getEulerDoublePrecision(N):\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "bc075ba57ede54360af8f3a3e813be9d",
"grade": true,
"grade_id": "cell-8a70d1055f710205",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# do your plotting here ...\n",
"\n",
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "7bb3d88d86391242a02c95e9f0d1b46d",
"grade": false,
"grade_id": "cell-3c271c843a5614ed",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that getEulerSinglePrecision and getEulerDoublePrecision return the correct output for several inputs\"\"\"\n",
"assert np.allclose(getEulerSinglePrecision(0), 1)\n",
"assert np.allclose(getEulerSinglePrecision(1), 2)\n",
"assert np.allclose(getEulerSinglePrecision(2), 2.5)\n",
"assert np.allclose(getEulerSinglePrecision(10), np.e)\n",
"\n",
"assert np.allclose(getEulerDoublePrecision(0), 1)\n",
"assert np.allclose(getEulerDoublePrecision(1), 2)\n",
"assert np.allclose(getEulerDoublePrecision(2), 2.5)\n",
"assert np.allclose(getEulerDoublePrecision(10), np.e)\n",
"\n",
"eeAppr_32bit = getEulerSinglePrecision(50)\n",
"eeAppr_64bit = getEulerDoublePrecision(50)\n",
"assert getEulerErr(eeAppr_32bit) > getEulerErr(eeAppr_64bit)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "2bb7d28dbc43973a9e78b2ee51047f60",
"grade": false,
"grade_id": "cell-13d15413f8f75465",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 3\n",
"\n",
"Compare the relative errors $\\delta$ for different rounding accuracies as a function of $N$. Use Python's $\\text{round(e_n, d)}$ function, where $d$ is the number of returned digits, to round each $e_n$ element before adding them up. Plot $\\delta$ vs. $N$ for $d = 1,2,3,4,5$ and add a corresponding legend."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "385cdd2874fb0313907deb17a8121b9d",
"grade": true,
"grade_id": "cell-7630c5ab8dcbb8ec",
"locked": false,
"points": 3,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"def getEulerRounding(N, d):\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "ef8842f1f9c732efe6ee85c9fa05de3a",
"grade": true,
"grade_id": "cell-0ef9b81c2acc546b",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# do your plotting here\n",
"\n",
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "9443eef390a2a436c772305c76f50dca",
"grade": false,
"grade_id": "cell-488fe0a8acdfe430",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"outputs": [],
"source": [
"\"\"\"Check that getEulerRounding returns the correct output for several inputs\"\"\"\n",
"assert np.allclose(getEulerRounding(0,3), 1)\n",
"assert np.allclose(getEulerRounding(1,5), 2)\n",
"assert np.allclose(getEulerRounding(2,6), 2.5)\n",
"assert np.allclose(getEulerRounding(10,2), np.e, rtol=1e-3)\n",
"assert np.allclose(getEulerRounding(10,6), np.e, rtol=1e-5)\n",
"\n",
"eeAppr_4d = getEulerRounding(50, 4)\n",
"eeAppr_8d = getEulerRounding(50, 8)\n",
"assert getEulerErr(eeAppr_4d) > getEulerErr(eeAppr_8d)"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "5a0cd9b95d380fb7f3c1560ba21442df",
"grade": false,
"grade_id": "cell-dc9fc12cd1291a10",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Some examples\n",
"```python\n",
"import numpy as np\n",
"\n",
"# using float32\n",
"a = 0.1234\n",
"b = np.float32(a)\n",
"\n",
"# using round\n",
"c = round(a, 2)\n",
"```"
]
}
],
"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
}

View File

@ -0,0 +1,325 @@
{
"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
}

View File

@ -0,0 +1,339 @@
{
"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": "469ccfeb53bd5b83e495642528e99e1d",
"grade": false,
"grade_id": "cell-5ddd89073409a7a8",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Lagrange Polynomial Interpolation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "3e8dc3118576d1fd31e93a9b7cd5d54d",
"grade": true,
"grade_id": "cell-e0c1868b4b81d084",
"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": "3ffe20a02520c3d6a1497e068b845cfa",
"grade": false,
"grade_id": "cell-b65f0fc40d1d652d",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 1\n",
"\n",
"Write your own Lagrange polynomial interpolation routine which calculates \n",
"\n",
"$$P(x) = \\sum_{k=0}^n f(x_k) L_{n,k}(x)$$\n",
"\n",
"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 \n",
"\n",
"$$L_{n,k}(x) = \\prod_{\\substack{i=0 \\\\ i\\neq k}}^n \\frac{x-x_i}{x_k - x_i}$$\n",
"\n",
"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. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "5b77f606a88a179ce1efe6a25f34323f",
"grade": true,
"grade_id": "cell-a501cccbea15bc5b",
"locked": false,
"points": 3,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"def myLagrangePolynomials(xk, k, x):\n",
" \"\"\"Don't forget to write a docstring ...\n",
" \"\"\"\n",
" \n",
" L = np.ones(np.size(x), dtype=np.float64)\n",
" \n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()\n",
" \n",
" return L\n",
"\n",
"def myLagrange(xk, yk, x):\n",
" \"\"\"Don't forget to write a docstring ...\n",
" \"\"\"\n",
" p = np.zeros(np.size(x), dtype=np.float64)\n",
" \n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()\n",
" \n",
" return p"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "4cc2302dbf93595c9d3992cfea9ce27a",
"grade": false,
"grade_id": "cell-a81702a2644eb7ed",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 2\n",
"\n",
"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$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "57cae9cbb8ab1cf2476751ca2a82b895",
"grade": true,
"grade_id": "cell-47c936437cb6ff13",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "8d9cacf68528b7180b7d483b09896912",
"grade": false,
"grade_id": "cell-3d0a2e5f464f406d",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 3\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "43719a879df67d8f0d60f25ed65ab34e",
"grade": true,
"grade_id": "cell-73bf6b345fdc3c51",
"locked": false,
"points": 1,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "19cfb28d4cbf1d27c3afbbf294a55cd8",
"grade": false,
"grade_id": "cell-1ddb948f96a8c7d8",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 4\n",
"\n",
"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. \n",
"\n",
"For example, the following code implements a test for the function $\\text{sqr}$\n",
"```python\n",
"\n",
"def sqr(a):\n",
" return a**2.0\n",
"\n",
"def test_sqr():\n",
" assert np.allclose(sqr(0), 0.0)\n",
" assert np.allclose(sqr(5), 25.0)\n",
" assert np.allclose(sqr(-3), 9.0)\n",
" \n",
"test_sqr()\n",
"```\n",
"\n",
"Implement a simple test function to test your Langrange interpolation routine."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "02b6893a4fda5442910c820fb3542f82",
"grade": true,
"grade_id": "cell-cf0a15360b429eae",
"locked": false,
"points": 3,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"def test_myLagrange():\n",
" \"\"\" don't forget to write a doc string ...\n",
" \"\"\"\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()\n",
" \n",
"test_myLagrange()"
]
}
],
"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
}

View File

@ -0,0 +1,278 @@
{
"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": "5f5bac1cd87e461d9f71bc953ede6e88",
"grade": false,
"grade_id": "cell-6332ff6a3f2e689c",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"## Runge's Phenomenom\n",
"\n",
"Use your own Lagrange interpolation routine to interpolate the function \n",
"\n",
"$$f(x) = \\frac{1}{1+25x^2}$$ \n",
"\n",
"between $x=-1$ and $x=+1$, using the $x_k$ below. Plot the results and briefly discuss their differences."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "f8cf534735416c308208b1e6ecd2876f",
"grade": true,
"grade_id": "cell-3f6954db97af64ac",
"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": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "7a225be5c561586c3b856647471fcc8a",
"grade": true,
"grade_id": "cell-b2343ef4f99a9b76",
"locked": false,
"points": 0,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# Paste your Lagrange interpolation routine here ...\n",
"\n",
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "364ec951d2f6f2f29b57cc4f078f5b2a",
"grade": false,
"grade_id": "cell-f44830695dd3d257",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 1\n",
"\n",
"Use equidistant $x_k = \\frac{2k}{n} - 1$ with $k \\in \\{0, 1, \\dots, n \\}$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "8eb435d8d318786d191583d6f3fcd737",
"grade": true,
"grade_id": "cell-943d2b180794f480",
"locked": false,
"points": 4,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "7dfe7433a2609135e3042be4f236a9fc",
"grade": false,
"grade_id": "cell-52109e0ec59901e0",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 2\n",
"\n",
"Use Chebychev nodes $x_k = \\operatorname{cos}\\left(\\frac{2k-1}{2n}\\pi \\right)$ with $k \\in \\{1, \\dots, n \\}$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "8b5b10ede0329670decb875bc2feecf0",
"grade": true,
"grade_id": "cell-3f5e14bdad9903fe",
"locked": false,
"points": 4,
"schema_version": 3,
"solution": true,
"task": false
}
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
]
},
{
"cell_type": "markdown",
"metadata": {
"deletable": false,
"editable": false,
"nbgrader": {
"cell_type": "markdown",
"checksum": "177c1b4e396a12322b975a70a0d4fd83",
"grade": false,
"grade_id": "cell-48ed473fe29dd3b9",
"locked": true,
"schema_version": 3,
"solution": false,
"task": false
}
},
"source": [
"### Task 3\n",
"\n",
"Use $n$ randomly chosen points $x_k$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"deletable": false,
"nbgrader": {
"cell_type": "code",
"checksum": "4d58b33e15ef5873ab11f66d7c0241f8",
"grade": true,
"grade_id": "cell-644d6105d3263df1",
"locked": false,
"points": 4,
"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
}