09: Concept 7

This commit is contained in:
2022-03-08 17:27:32 +01:00
parent 77b46a6515
commit 3ecaea25d4

View File

@ -730,7 +730,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 10,
"metadata": { "metadata": {
"deletable": false, "deletable": false,
"nbgrader": { "nbgrader": {
@ -747,8 +747,38 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# YOUR CODE HERE\n", "def ODEF(x, y):\n",
"raise NotImplementedError()" " # TODO: Docstring.\n",
" return np.array([y[1], -np.sin(y[0])])\n",
"\n",
"h = .02\n",
"x = np.arange(0, 2, h)\n",
"y = (x + 1)**2 - 0.5*np.exp(x)\n",
"eta = np.zeros(len(x))\n",
"\n",
"fig = plt.figure(figsize=(12, 8))\n",
"gs = gridspec.GridSpec(2, 1, height_ratios=[2, 1])\n",
"\n",
"ax0 = plt.subplot(gs[0])\n",
"ax0.plot(x, y, label=\"exact solution\")\n",
"ax0.set_yscale(\"log\")\n",
"ax0.set_title(\"Solutions to $y' = y - x^2 + 1.0$\")\n",
"\n",
"\n",
"ax1 = plt.subplot(gs[1])\n",
"ax1.set_ylabel(\"$\\delta(x) = |\\~y(x) - y(x)|$\")\n",
"\n",
"\n",
"for alg, alg_name in algos:\n",
" eta = integrator(x, y0, ODEF, alg)\n",
" ax0.plot(x, eta, label=alg_name)\n",
" ax1.plot(x, np.abs(eta - y), label=alg_name)\n",
"\n",
"ax0.legend()\n",
"plt.xlabel(\"x\")\n",
"ax0.set_ylabel(\"y(x)\")\n",
"ax1.legend()\n",
"plt.show()"
] ]
} }
], ],