{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 21 Newtin-cotes integration formula" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex : 21.1 Pg : 612" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Error Et= 1.468\n", "The percent relative error et= 89.467 %\n", "The approximate error estimate without using the true value= 2.56\n" ] } ], "source": [ "from __future__ import division\n", "def f(x):\n", " y=(0.2+25*x-200*x**2+675*x**3-900*x**4+400*x**5)\n", " return y\n", "tval=1.640533#\n", "a=0#\n", "b=0.8#\n", "fa=f(a)#\n", "fb=f(b)#\n", "l=(b-a)*((fa+fb)/2)#\n", "Et=tval-l##error\n", "et=Et*100/tval##percent relative error\n", "\n", "#by using approximate error estimate\n", "\n", "#the second derivative of f\n", "def g(x):\n", " y=-400+4050*x-10800*x**2+8000*x**3\n", " return y\n", "\n", "from sympy.mpmath import quad\n", "f2x=quad(g,[0,0.8])/(b-a)##average value of second derivative\n", "Ea=-(1/12)*(f2x)*(b-a)**3#\n", "print \"The Error Et=\",round(Et,3)\n", "print \"The percent relative error et=\",round(et,3),\"%\"\n", "print \"The approximate error estimate without using the true value=\",Ea" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex : 21.2 Pg : 613" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Error Et= 0.572\n", "The percent relative error et= 34.85 %\n", "The approximate error estimate without using the true value= 0.64\n" ] } ], "source": [ "def f(x):\n", " y=(0.2+25*x-200*x**2+675*x**3-900*x**4+400*x**5)\n", " return y\n", "a=0#\n", "b=0.8#\n", "tval=1.640533#\n", "n=2#\n", "h=(b-a)/n#\n", "fa=f(a)#\n", "fb=f(b)#\n", "fh=f(h)#\n", "l=(b-a)*(fa+2*fh+fb)/(2*n)#\n", "Et=tval-l##error\n", "et=Et*100/tval##percent relative error\n", "\n", "#by using approximate error estimate\n", "\n", "#the second derivative of f\n", "def g(x):\n", " y=-400+4050*x-10800*x**2+8000*x**3\n", " return y\n", "f2x=quad(g,[0,0.8])/(b-a)##average value of second derivative\n", "Ea=-(1/12)*(f2x)*(b-a)**3/(n**2)#\n", "print \"The Error Et=\",round(Et,3)\n", "print \"The percent relative error et=\",round(et,3),\"%\"\n", "print \"The approximate error estimate without using the true value=\",Ea" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Ex :21.3 Pg : 614" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "No. of segments= 10\n", "Segment size= 1.0\n", "Estimated d= 288.749146143 m\n", "0.237014701487 et(%)\n", "---------------------------------------------------------\n", "No. of segments= 20\n", "Segment size= 0.5\n", "Estimated d= 289.263574224 m\n", "0.0592795228803 et(%)\n", "---------------------------------------------------------\n", "No. of segments= 50\n", "Segment size= 0.2\n", "Estimated d= 298.382319223 m\n", "et(%) -3.09125177877\n", "---------------------------------------------------------\n", "No. of segments= 100\n", "Segment size= 0.1\n", "Estimated d= 293.915596452 m\n", "et(%) -1.54799665905\n", "---------------------------------------------------------\n", "No. of segments= 100\n", "Segment size= 0.1\n", "Estimated d= 293.915596452 m\n", "et(%) -1.54799665905\n", "---------------------------------------------------------\n", "No. of segments= 200\n", "Segment size= 0.05\n", "Estimated d= 289.43343055 m\n", "et(%) 0.000594070904571\n", "---------------------------------------------------------\n", "No. of segments= 200\n", "Segment size= 0.05\n", "Estimated d= 289.43343055 m\n", "et(%) 0.000594070904571\n", "---------------------------------------------------------\n", "No. of segments= 500\n", "Segment size= 0.02\n", "Estimated d= 290.332334709 m\n", "et(%) -0.309977799375\n", "---------------------------------------------------------\n", "No. of segments= 1000\n", "Segment size= 0.01\n", "Estimated d= 289.883809248 m\n", "et(%) -0.155012011658\n", "---------------------------------------------------------\n", "No. of segments= 2000\n", "Segment size= 0.005\n", "Estimated d= 289.435129352 m\n", "et(%) 7.13401428866e-06\n", "---------------------------------------------------------\n", "No. of segments= 2000\n", "Segment size= 0.005\n", "Estimated d= 289.435129352 m\n", "et(%) 7.13401428866e-06\n", "---------------------------------------------------------\n", "No. of segments= 5000\n", "Segment size= 0.002\n", "Estimated d= 289.435143766 m\n", "et(%) 2.15393877364e-06\n", "---------------------------------------------------------\n", "No. of segments= 5000\n", "Segment size= 0.002\n", "Estimated d= 289.435143766 m\n", "et(%) 2.15393877364e-06\n", "---------------------------------------------------------\n", "No. of segments= 10000\n", "Segment size= 0.001\n", "Estimated d= 289.480018962 m\n", "et(%) -0.0155022506708\n", "---------------------------------------------------------\n" ] } ], "source": [ "g=9.8##m/s**2# acceleration due to gravity\n", "m=68.1##kg\n", "c=12.5##kg/sec# drag coefficient\n", "def f(t):\n", " from numpy import exp\n", " v=g*m*(1-exp(-c*t/m))/c\n", " return v\n", "tval=289.43515##m\n", "a=0#\n", "b=10#\n", "fa=f(a)#\n", "fb=f(b)#\n", "from numpy import arange, exp\n", "for i in arange(10,21,10):\n", " n=i#\n", " h=(b-a)/n#\n", " print \"No. of segments=\",i\n", " print \"Segment size=\",h\n", " j=a+h#\n", " s=0#\n", " while j