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

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
}