Initial commit with templates
This commit is contained in:
468
Week 1/01 Rounding and Truncation Error Analysis.ipynb
Normal file
468
Week 1/01 Rounding and Truncation Error Analysis.ipynb
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user