09: Draft task 1 before I change too much

This commit is contained in:
2022-03-08 11:33:11 +01:00
parent 4fd1c63aa0
commit 159607197f

View File

@ -39,7 +39,7 @@
"cell_type": "raw",
"metadata": {},
"source": [
"team_members = \"\""
"team_members = \"Koen Vendrig, Kees van Kempen\""
]
},
{
@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"deletable": false,
"nbgrader": {
@ -90,10 +90,7 @@
},
"outputs": [],
"source": [
"# Import packages here ...\n",
"\n",
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
"import numpy as np"
]
},
{
@ -122,7 +119,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"deletable": false,
"nbgrader": {
@ -140,8 +137,30 @@
"outputs": [],
"source": [
"def integrator(x, y0, f, phi):\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()"
" \"\"\"\n",
" Numerically solves the initial value problem given by ordinary differential equation\n",
" f(x, y) = y' with initial value y0 using the Euler method.\n",
"\n",
" Args:\n",
" x: size n + 1 numerical array\n",
" y0: an initial value to the function f\n",
" f: a callable function with signature (x, y), with x and y the current state\n",
" of the system\n",
" phi: a callable function with signature (x, y, f, i), with x and y the current state\n",
" of the system, i the step number, and f as above\n",
"\n",
" Returns:\n",
" An n + 1 numerical array representing an approximate solution to y' = f(x, y)\n",
" given initial value y0 and steps from x\n",
" \"\"\"\n",
" \n",
" y = np.zeros(len(x))\n",
" y[0] = y0\n",
" \n",
" for i in range(len(y)):\n",
" y[i] = y[i - 1] + (x[i] - x[i - 1])*f(x[i - 1], y[i - 1])\n",
" \n",
" return y"
]
},
{
@ -164,8 +183,28 @@
"outputs": [],
"source": [
"def phi_euler(x, y, f, i):\n",
" # YOUR CODE HERE\n",
" raise NotImplementedError()"
" \"\"\"\n",
" Numerically solves the initial value problem given by ordinary differential equation\n",
" f(x, y) = y' with initial value y0 using the Euler method.\n",
"\n",
" Args:\n",
" x: a size n + 1 numerical array\n",
" y: a size n + 1 numerical array with y[0] the initial value corresponding to x[0]\n",
" f: a callable function with signature (x, y), with x and y the current state\n",
" of the system\n",
" i: a callable function with signature (x, y, f, i), with x and y the current state\n",
" of the system, i the step number, and f as above\n",
"\n",
" Returns:\n",
" An n + 1 numerical array representing an approximate solution to y' = f(x, y)\n",
" given initial value y0 and steps from x\n",
" \"\"\"\n",
" \n",
" for i in range(len(y)):\n",
" y[i] = y[i - 1] + (x[i] - x[i - 1])*f(x[i - 1], y[i - 1])\n",
" \n",
" return y\n",
" "
]
},
{
@ -542,7 +581,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},