From a7eff0ba6d4697027d36b482ff7b7fe3a76f3adc Mon Sep 17 00:00:00 2001 From: Kees van Kempen Date: Tue, 8 Mar 2022 16:46:25 +0100 Subject: [PATCH] 09: Draft task 6 --- .../9 Ordinary Differential Equations.ipynb | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Week 5/9 Ordinary Differential Equations.ipynb b/Week 5/9 Ordinary Differential Equations.ipynb index aaa82c5..951e6bd 100644 --- a/Week 5/9 Ordinary Differential Equations.ipynb +++ b/Week 5/9 Ordinary Differential Equations.ipynb @@ -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()" ] }, {