{ "metadata": { "name": "", "signature": "sha256:39654f219b580cfcd333007be0af0e9ffb14a96403639fefec0f304f13f73eba" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Runge-Kutta Method" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solve $y'=y$, $y(0)=1$ using RK4 method." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import math\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "step = 1\n", "x = np.arange(0, 5, step)\n", "y = np.zeros(x.size)\n", "\n", "def derivative(x, y):\n", " return y\n", "\n", "y[0] = 1\n", "for i in range(x.size-1):\n", " k1 = derivative(x[i], y[i])\n", " k2 = derivative(x[i]+step/2.0, y[i]+0.5*k1*step)\n", " k3 = derivative(x[i]+step/2.0, y[i]+0.5*k2*step)\n", " k4 = derivative(x[i]+step, y[i]+k3*step)\n", " y[i+1] = y[i]+(step/6.0)*(k1+2*k2+2*k3+k4)\n", "\n", "plt.scatter(x,y)\n", "\n", "x = np.linspace(0, 5, 50)\n", "y = math.e**x\n", "plt.plot(x, y, 'r--')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "[]" ] }, { "metadata": {}, "output_type": "display_data", "png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAEACAYAAACnJV25AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGmdJREFUeJzt3XuQVPWd9/H3JxAEBEXjigIqrEoEowaNeNko7Q0Iqygh\n3kqz5LLJJibG+FAquJswqeQxuCmUXCR5lICYKJESMWDUYbx0FG+YFQ0KPOhTi4LKqHhX1AG+zx+n\nZcdxgJ7pnjndpz+vqi66T58z/Yk19clvfv075ygiMDOz7PhU2gHMzKy8XOxmZhnjYjczyxgXu5lZ\nxrjYzcwyxsVuZpYxRRW7pFmSGiUtb7H9QkkrJT0l6cpm2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vbKPZKmoqplhHA34ADJQ2U1I3kSpYLU85UM5ScNPF7YEVETE87T1tI\n2kNSn8LzHsApwLJ0UxUvIi6PiH0iYhBwDnBvNZW6pJ6Sehee7wyMJFnAURUiYj2wVtLgwqaTgadb\n2zft5Y7jJK0lWd3wF0l3ppmnGBGxCfg+UE+yMuDmiGj1m+lKJGku8BAwWNJaSV9PO1Mb/RNwPsmK\nkmWFR7Ws8tkbuLcwx/4osCgi7kk5UymqbVqyL/BAs//+t0fE4pQztdWFwI2SniRZFXNFazv5BCUz\ns4yppKkYMzMrAxe7mVnGuNjNzDLGxW5mljEudjOzjHGxm5lljIvdzCxjXOxmZhnz/wFTAKEKhiJW\njgAAAABJRU5ErkJggg==\n", "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Case Study: Spring-mass Damper System" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The system diagram:
\n", "\n", "\n", "A block is suspended freely using a spring. The mass of the block is M, the spring constant is K, and the damper coefficient be b. If we measure displacement from the static equilibrium position we need not consider gravitational force as it is balanced by tension in the spring at equilibrium.\n", "\n", "The equation of motion is $Ma = F_{s}+F_{d}$ where \n", "$F_{s}$ is the restoring force due to spring.\n", "$F_{d}$ is the damping force due to the damper.\n", "$a$ is the acceleration.\n", "\n", "The restoring force in the spring is given by \n", "$F_{s}= -Kx$ as the restoring force is proportional to displacement it is negative as it opposes the motion. The damping force in the damper is given by\n", "$F_{d}=-bv$ as damping force is directly proportional to velocity and also opposes motion.\n", "\n", "Therefore the equation of motion is $Ma=-Kx-bv$.\n", "\n", "Since $a=\\frac{dv}{dt}$ and $v=\\frac{dx}{dt}$ we get\n", "\n", "$M\\frac{dv}{dt}=-Kx-bv$ and $\\frac{dx}{dt}=v$." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# source: http://gafferongames.com/game-physics/integration-basics/\n", "import numpy as np\n", "import math\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "x = 100\n", "v = 0\n", "t = 0\n", "dt = 0.1\n", "\n", "def acceleration(x, v, t):\n", " k = 10\n", " b = 1\n", " return -k*x - b*v\n", "\n", "def evaluate1(x, v, t):\n", " dx = v\n", " dv = acceleration(x, v, t)\n", " return (dx, dv);\n", "\n", "def evaluate2(x, v, t, dt, derivatives):\n", " dx, dv = derivatives\n", " x += dx*dt\n", " v += dv*dt\n", " dx = v\n", " dv = acceleration(x, v, t+dt)\n", " return (dx, dv);\n", "\n", "def integrate(t, dt):\n", " global x\n", " global v\n", " k1 = evaluate1(x, v, t);\n", " k2 = evaluate2(x, v, t, dt*0.5, k1)\n", " k3 = evaluate2(x, v, t, dt*0.5, k2)\n", " k4 = evaluate2(x, v, t, dt, k3)\n", " k1_dx, k1_dv = k1\n", " k2_dx, k2_dv = k2\n", " k3_dx, k3_dv = k3\n", " k4_dx, k4_dv = k4\n", " dxdt = (1/6.0)*(k1_dx+2*(k2_dx+k3_dx)+k4_dx)\n", " dvdt = (1/6.0)*(k1_dv+2*(k2_dv+k3_dv)+k4_dv)\n", " x += dxdt*dt\n", " v += dvdt*dt\n", "\n", "xData = []\n", "vData = []\n", "while abs(x) > 0.001 or abs(v) > 0.001:\n", " integrate(t, dt)\n", " t += dt\n", " #print \"x=\",x,\" v=\",v\n", " #print x\n", " xData.append(x)\n", "\n", "plt.scatter(np.arange(len(xData)),xData)\n" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "" ] }, { "metadata": {}, "output_type": "display_data", "png": "iVBORw0KGgoAAAANSUhEUgAAAYMAAAEACAYAAABRQBpkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGPNJREFUeJzt3X+MnPV94PH3Z3G2bGIuxDgxBAhYBmrMcbBcSjjRiNWp\n3sX5w4lxr6G0J4dEoSgR1yObnHGsHD6wa3I6c23u2uRooDhJQ0uKnHOUZMdLlc0FpAZFGGLKEWwu\nRjE/T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wAAAABJRU5ErkJggg==\n", "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }