{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Shooting Method\n", "\n", "Copyright (C) 2020 Andreas Kloeckner\n", "\n", "
\n", "MIT License\n", "Permission is hereby granted, free of charge, to any person obtaining a copy\n", "of this software and associated documentation files (the \"Software\"), to deal\n", "in the Software without restriction, including without limitation the rights\n", "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n", "copies of the Software, and to permit persons to whom the Software is\n", "furnished to do so, subject to the following conditions:\n", "\n", "The above copyright notice and this permission notice shall be included in\n", "all copies or substantial portions of the Software.\n", "\n", "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n", "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n", "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n", "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n", "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n", "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n", "THE SOFTWARE.\n", "
" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as pt" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "def rk4_step(y, t, h, f):\n", " k1 = f(t, y)\n", " k2 = f(t+h/2, y + h/2*k1)\n", " k3 = f(t+h/2, y + h/2*k2)\n", " k4 = f(t+h, y + h*k3)\n", " return y + h/6*(k1 + 2*k2 + 2*k3 + k4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Want to solve:\n", "\n", "$$w''(t)=\\frac 32w^2$$\n", "\n", "with $w(0)=4$ and $w(1)=1$. (Example due to Stoer and Bulirsch)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "def f(t, y):\n", " w, w_prime = y\n", " return np.array([w_prime, 3/2*w**2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following function carries out the shooting method for a given $w'(0)$ using RK4:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [ "def shoot(w_prime):\n", " times = [0]\n", " y_values = [np.array([4, w_prime])]\n", " \n", " h = 1/2**7\n", " t_end = 1\n", " \n", " while times[-1] < t_end:\n", " y_values.append(rk4_step(y_values[-1], times[-1], h, f))\n", " times.append(times[-1]+h)\n", " \n", " y_values = np.array(y_values)\n", " \n", " # actually floating-point-equal due to power-of-2 h\n", " assert times[-1] == t_end\n", " \n", " print(\"w'(0) = %g -> w(1)= %.5g\" % (w_prime, y_values[-1,0]))\n", "\n", " pt.plot(times, y_values[:, 0], label=\"$w'(0)=%.2g$\" % w_prime)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Call `shoot` to see if you can solve the boundary value problem.\n", "\n", "Start with $w'(0)=0$.\n", "\n", "(You may call `pt.legend` to take advantage of automatic labeling.)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See if you can find another solution to the boundary value problem by starting with $w'(0)=-30$.\n", "\n", "(You may call `pt.legend` to take advantage of automatic labeling.)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }