09: Draft task 6

This commit is contained in:
2022-03-08 16:46:25 +01:00
parent debd58eb56
commit a7eff0ba6d

View File

@ -497,6 +497,7 @@
" phi = (1/12)*( 23*f(x[i - 1], y[i - 1]) - 16*f(x[i - 2], y[i - 2]) + 5*f(x[i - 3], y[i - 3]) )\n",
" return phi\n",
"\n",
"# TODO: AB4 looks a bit off in the plots in task 5.\n",
"def phi_ab4(x, y, f, i):\n",
" # The first three y values are to be calculated using the Runga-Kutta method.\n",
" if i < 4:\n",
@ -651,8 +652,31 @@
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"raise NotImplementedError()"
"# We assume the definitions of the previous block as not to copy everything.\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()"
]
},
{