{ "metadata": { "name": "", "signature": "sha256:c93392c7f30c8fa73a91e054919df1462e0ff8b760d8593d624f639d729986f9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "ESE - Numerical Methods I: Quadrature" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Performing numerical integration" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Quadrature* is the term used for numerical evaluation of a definite integral, or put more simply, finding the area under a curve. There are a number of methods of quadrature which we will learn today. If we have a function $f(x)$ defined between $a$ and $b$, the integral is defined as \n", "\n", "\\begin{equation}\n", "F\\left ( x \\right ) = \\int_{a}^{b} f\\left ( x \\right )dx\n", "\\end{equation}\n", "\n", "and its result is the area under the curve. As you know, this operation is an essential part of calculus, being the inverse of differentiation. Knowing the value of the area under a curve is imporant to all kinds of applications. Many expressions are difficult to integrate, or the function which governs their shape is unknown, and we only have data. Therefore, no course on numerical methods is complete without discussing the different methods of numerical integration. A fundamental property of a definite integral is that\n", "\n", "\\begin{equation}\n", "\\int_{a}^{b} f\\left ( x \\right )dx = \\int_{c}^{a} f\\left ( x \\right )dx + \\int_{b}^{c} f\\left ( x \\right )dx\n", "\\end{equation}\n", "\n", "where $c$ is a point between $a$ and $b$. Therefore, we can perform an integration by splitting the function up into sections and summing the result of each integration. If the function is complicated or unknown, we can approximate its distribution between subdivisions and choose the size of subdivisions to get an accurate result. This is known as *adaptive quadrature*. In this lecture, five different quadrature methods will be covered in the context of a simple function: the Midpoint rule, Trapezoid rule, Simpson's rule, Composite Simpson's rule and Weddle's rule. Then, there is an excersise to apply numerical integration to a geophysical data set. \n", "\n", "First, let's begin with a simple function to demonstrate the different methods of numerical integration. Let\n", "\n", "\\begin{equation}\n", "f\\left ( x \\right ) = \\sin \\left ( x \\right )\n", "\\end{equation}\n", "\n", "We want to know what the area under the sin function is between 0 and $pi$. The $\\sin \\left ( x \\right )$ function is known to integrate to $-\\cos \\left ( x \\right )$, so we can integrate it ourselves by hand to find the true area under the curve:\n", "\n", "\\begin{equation}\n", "F\\left ( x \\right ) = \\int_{0}^{\\pi} \\sin \\left ( x \\right ) = \\left [ -\\cos\\left ( x \\right )+ C \\right ]_{0}^{\\pi} =-\\cos\\left ( \\pi \\right ) + \\cos\\left ( 0 \\right ) = 2\n", "\\end{equation}\n", "\n", "Let's start by plotting the function between these points." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline\n", "import os\n", "import numpy as np\n", "from scipy import integrate # This gives us the fucntions for different numerical integration methods!\n", "import matplotlib.pyplot as plt\n", "\n", "x = linspace(0,np.pi,20) #Get the value of pi from numpy\n", "y = sin(x)\n", "\n", "plt.plot(x,y)\n", "plt.xlim([0,np.pi]) #Set x axis limits between 0 and pi" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] }, { "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "(0, 3.141592653589793)" ] }, { "metadata": {}, "output_type": "display_data", "png": "iVBORw0KGgoAAAANSUhEUgAAAW8AAAEACAYAAAB8nvebAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XecVNX5x/HPI0VjL0SSIIYYUVBREUWMQdYSQcSCP41i\nwVh+ooImVkQQFgiiqBRpFkSJBfyBCEgV0FVAuhSRIkXzQjBECAoRQj2/P86ubpZdZnZ3Zs7cme/7\n9drX7uycnftc7vLsM+eeYs45REQkWg4IHYCIiJSekreISAQpeYuIRJCSt4hIBCl5i4hEkJK3iEgE\nxUzeZjbYzDaY2Wf7afO8ma00s0VmVjexIYqISFHxVN6vAk1KetLMmgInOudqAncBAxMUm4iIlCBm\n8nbOTQM276fJlcCQ/LazgSPNrGpiwhMRkeIkos+7GrC20OOvgeMS8LoiIlKCRN2wtCKPNedeRCSJ\nKibgNdYB1Qs9Pi7/e//FzJTQRUTKwDlXtEBOS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O9wybTlnJsGbN5Pk6hdIyCu84IIXScA59w/nHML87/+N7AM+FWRZpG6XnGeE0TvWm3L\n/7Iyvuj7V5EmCb1OiU7eCZ3UkybiOScH/C7/rdB4MzslZdElR9SuUbwifZ3yR33VBWYXeSqy12s/\n5xS5a2VmB5jZQmAD8KFzbmmRJgm9Tone+jehk3rSRDyxfQpUd85tM7PLgFHASckNK+midI3iFdnr\nZGaHAiOAP+dXq/s0KfI47a9XjHOK3LVyzu0FzjSzI4BJZpbjnMsr0ixh1ynRlfc6oPDqJdXxf132\n1+a4/O+lq5jn5JzbWvCWyTk3AahkZkenLsSEi9o1iktUr5OZVQLeAd5wzo0qpknkrlesc4rqtQJw\nzn0PjAPOLvJUQq9TopP3j5N6zKwyflLPmCJtxgAt4ccZnMVO6kkjMc/JzKqa+X0vzKw+fghm0f6u\nKInaNYpLFK9TfryvAEudc71LaBap6xXPOUXtWplZFTM7Mv/rnwF/ABYUaZbQ65TQbhOXgZN64jkn\n4FrgHjPbDWwDbggWcBzMbCjQCKhiZmuBTviRNJG8RgVinRcRu075zgduBhabWUEyeBw4HiJ7vWKe\nE9G7Vr8EhpjZAfii+HXn3NRk5j5N0hERiaAgu8eLiEj5KHmLiESQkreISAQpeYuIRJCSt4hIBCl5\ni4hEkJK3iEgEKXmLiETQ/wNGr9IOEPAnJQAAAABJRU5ErkJggg==\n", "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's what our function looks like. Let's investigate the different quadrature rules with reference to it, finding the area under the curve and seeing how it differs from the true area which we know is 2." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Rule 1: Midpoint rule" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The midpoint rule is the simplest quadrature rule. Let $h = b - a$ be the length of the interval. The midpoint rule, $M$, approximates the integral by the area of a rectangle, with base of length $h$ and height of the value of $f(x)$ at the midpoint,\n", "\n", "\\begin{equation}\n", "M = hf \\left ( \\frac {a+b} {2} \\right )\n", "\\end{equation}\n", "\n", "The result of the integration is the combined area of all the rectanges. A complex example looks like this:\n", "\n", "\n", "\n", "The SciPy module features many different integration functions, and you can find thorough documentation for these functions (including methods not covered in this course) here. This library does not contain a function for the midpoint rule, so let's make our own. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def midpoint_rule(start_point, end_point, function, number_of_bins=10):\n", " #First, check how big each bin needs to be. Bin is just another word for rectangle.\n", " bin_size = float(end_point - start_point)/number_of_bins\n", " \n", " #To make sure the function works as expected and doesn't output garbage accidentally,\n", " #let's assert that there are more than 0 bins, and that the number of bins is a whole number\n", " assert bin_size > 0\n", " assert type(number_of_bins) == int\n", " #By doing this, the function will give an error if either of these are not true,\n", " #avoiding problems later. This is a neat alternative to a try-except loop.\n", " #In programming it is always good to know more than one way of doing things!\n", " \n", " #Create the variable to contain the sum of all the areas\n", " running_total = 0.0\n", " #Find the first midpoint\n", " mid = start_point + bin_size/2\n", " \n", " #Loop to create each rectangle \n", " while (mid < end_point):\n", " #Find the area of the next rectangle and add it to the running total\n", " running_total += bin_size * function(mid)\n", " #Move the midpoint up to the next centre of the bin\n", " mid += bin_size\n", " \n", " #Return our running total result\n", " return running_total" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's test the midpoint function. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The area found by direct integration = 2\"\n", "print \"The area found with the midpoint rule with 1 rectangle = \", midpoint_rule(0,np.pi,sin,1)\n", "print \"The area found with the midpoint rule with 2 rectangles = \", midpoint_rule(0,np.pi,sin,2)\n", "print \"The area found with the midpoint rule with 10 rectangles = \", midpoint_rule(0,np.pi,sin,10)\n", "print \"The area found with the midpoint rule with 100 rectangles = \", midpoint_rule(0,np.pi,sin,100)\n", "print \"The area found with the midpoint rule with 1000 rectangle = \", midpoint_rule(0,np.pi,sin,1000)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The area found by direct integration = 2\n", "The area found with the midpoint rule with 1 rectangle = 3.14159265359\n", "The area found with the midpoint rule with 2 rectangles = 2.22144146908\n", "The area found with the midpoint rule with 10 rectangles = 2.00824840791\n", "The area found with the midpoint rule with 100 rectangles = 2.00008224907\n", "The area found with the midpoint rule with 1000 rectangle = 2.00000082247\n" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "With one rectangle, we are simply finding the area of a box of shape $\\pi \\times$ 1, so of course the result is $\\pi$. As we increase the number of rectangles we split the function up by, we increase the accuracy of our area. The simplicity of this method is its weakness, as rectangles are rarely a good approximation for the shape of a smooth function. We should want to use as few shapes as possible to approximate our function, because each additional rectangle is one extra time round the loop, increasing processing time. Instead, let's try another shape..." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Rule 2: Trapezoid rule" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we swap the shape of the rectangle for a trapezoid, we arrive at the trapezoid rule. The trapezoid rule, $T$, approximates the integral by the area of a trapezoid with base $h$ and sides equal to the vaules of the integral at the two end points,\n", "\n", "\\begin{equation}\n", "T = h \\frac {f\\left ( a\\right ) + f \\left (b \\right )} {2}\n", "\\end{equation}\n", "\n", "An example looks more accurate than the midpoint rule:\n", "\n", "\n", "\n", "Numpy has a function for the trapezoid rule, numpy.trapz, but we'll make our own that works in a similar way to our midpoint rule function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def trapezoid_rule(start_point, end_point, function, number_of_bins=10):\n", " \n", " bin_size = float(end_point - start_point)/number_of_bins\n", " \n", " assert bin_size > 0\n", " assert type(number_of_bins) == int\n", " \n", " running_total = 0.0\n", " \n", " #Loop to create each trapezoid \n", " for i in range(number_of_bins):\n", " #Set the start of this bin\n", " this_bin_start = start_point + bin_size * (i)\n", " #Find the area of the next rectangle and add it to the running total\n", " running_total += bin_size*float(function(this_bin_start)+function(this_bin_start+bin_size))/2\n", " #Move the midpoint up to the start of the next trapezoid\n", " \n", " #Return our running total result\n", " return running_total" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Testing the function in a similar way:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The area found by direct integration = 2\"\n", "print \"The area found with the trapezoid rule with 1 trapezoid = \", trapezoid_rule(0,np.pi,sin,1)\n", "print \"The area found with the trapezoid rule with 2 trapezoids = \", trapezoid_rule(0,np.pi,sin,2)\n", "print \"The area found with the trapezoid rule with 10 trapezoids = \", trapezoid_rule(0,np.pi,sin,10)\n", "print \"The area found with the trapezoid rule with 100 trapezoids = \", trapezoid_rule(0,np.pi,sin,100)\n", "print \"The area found with the trapezoid rule with 1000 trapezoids = \", trapezoid_rule(0,np.pi,sin,1000)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The area found by direct integration = 2\n", "The area found with the trapezoid rule with 1 trapezoid = 1.92367069372e-16\n", "The area found with the trapezoid rule with 2 trapezoids = 1.57079632679\n", "The area found with the trapezoid rule with 10 trapezoids = 1.98352353751\n", "The area found with the trapezoid rule with 100 trapezoids = 1.99983550389\n", "The area found with the trapezoid rule with 1000 trapezoids = 1.99999835507\n" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see a few differences compared to the midpoint rule. First, the trapezoid rule always under-estimates the area, whereas the midpoint rule over-estimates. The result for finding the integral of $\\sin$ with only one triangle is very small, because that trapezoid has almost no height. Perhaps most suprisingly, the midpoint rule is more accurate than the trapezoid rule.\n", "\n", "The accuracy of a quadrature rule is predicted by examining its behaviour in practice with polynomials. The order of a rule is the regree of the lowest degree polynomial that the rule does not integrate exactly. If a rule of order $p$ is used to integrate a smooth function of length $h$, then Taylor series analysis shows the error is proportional to $h^p$. Both the midpoint and trapezoid rules are exact for constant and linear functions, but are not exact for quadratics. Therefore, they have order two. Let's use a different example (which integrates to whole numbers) to quantify how accurate the midpoint and trapezoid rules are in comparison to one another. If we are calculating\n", "\n", "\\begin{equation}\n", "\\int_{0}^{1} x^{2}dx = \\frac {1}{3}\n", "\\end{equation}\n", "\n", "the midpoint rule gives\n", "\n", "\\begin{equation}\n", "M = 1 \\frac {1}{3}^{2} = \\frac {1}{4}\n", "\\end{equation}\n", "\n", "and the trapezoid rule gives\n", "\n", "\\begin{equation}\n", "T = 1 \\frac {0+1^{2}}{2} = \\frac {1}{2}\n", "\\end{equation}\n", "\n", "The error for $M$ is 1/12, while the error for $T$ is -1/6. Therefore, the midpoint rule is twice as accurate as the trapezoid rule. This is true more generally for smooth functions over small intervals." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Rule 3: Simpson's rule" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Knowing the error estimates from the two rules explored so far allows us to combine them to create a new rule, more accurate than either one seperately, with a little help from the mathematician Thomas Simpson - luckily not related to Homer. \n", "\n", "\n", "\n", "If the error in $T$ is exactly -2 times the error in $M$, then solving\n", "\n", "\\begin{equation}\n", "S-T = -2 \\left ( S-M\\right )\n", "\\end{equation}\n", "\n", "for $S$ gives us the exact value of the integral. Therefore, the solution\n", "\n", "\\begin{equation}\n", "S = \\frac{2}{3}M + \\frac{1}{3}T\n", "\\end{equation}\n", "\n", "is usually a more accurate approximation that either $M$ or $T$ alone. This is known as Simpson's rule. It can also be found by integrating the quadratic function that interpolates the integral at two end points, $a$ and $b$, and the midpoint, $c = \\left ( a+b\\right )/2$:\n", "\n", "\\begin{equation}\n", "S = \\frac{h}{6}\\left ( f \\left ( a\\right ) + 4f \\left ( c\\right ) + f\\left ( b\\right )\\right )\n", "\\end{equation}\n", "\n", "This expression now integrates up to cubics exactly, so it is order 4. It looks like a much closer fit to the function:\n", "\n", "\n", "\n", "Let's make a function to test it out..." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def simpsons_rule(start_point, end_point, function, number_of_bins=10):\n", " \n", " bin_size = float(end_point - start_point)/number_of_bins\n", " \n", " assert bin_size > 0\n", " assert type(number_of_bins) == int\n", " \n", " running_total = 0.0\n", " \n", " #Loop to create each shape \n", " for i in range(number_of_bins):\n", " #Find a, c, and b\n", " this_bin_start = start_point + bin_size * (i)\n", " this_bin_mid = this_bin_start + bin_size/2\n", " this_bin_end = this_bin_start + bin_size\n", " #Calculate the rule and add to running total.\n", " running_total += (bin_size/6)*float(function(this_bin_start)+4*function(this_bin_mid)+function(this_bin_end))\n", " \n", " #Return our running total result\n", " return running_total" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The area found by direct integration = 2\"\n", "print \"The area found with Simpson's rule with 1 bin = \", simpsons_rule(0,np.pi,sin,1)\n", "print \"The area found with Simpson's rule with 2 bins = \", simpsons_rule(0,np.pi,sin,2)\n", "print \"The area found with Simpson's rule with 10 bins = \", simpsons_rule(0,np.pi,sin,10)\n", "print \"The area found with Simpson's rule with 100 bins = \", simpsons_rule(0,np.pi,sin,100)\n", "print \"The area found with Simpson's rule with 1000 bins = \", simpsons_rule(0,np.pi,sin,1000)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The area found by direct integration = 2\n", "The area found with Simpson's rule with 1 bin = 2.09439510239\n", "The area found with Simpson's rule with 2 bins = 2.00455975498\n", "The area found with Simpson's rule with 10 bins = 2.00000678444\n", "The area found with Simpson's rule with 100 bins = 2.00000000068\n", "The area found with Simpson's rule with 1000 bins = 2.0\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, Simpson's rule is very accurate. But we can get it even more accurate..." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Rule 4: Composite Simpson's Rule" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's take our calculations another step further. Previously, we have considered $a$ at the start of the interval, and $b$ at the end, with $c$ at the mid point. Instead, let's split the domain up into smaller subintervals, by making $d$ and $e$ the midpoints of these subintervals. $d = \\left ( a+c\\right )/2$, and $e = \\left( c+b\\right )/2$. If we apply Simpson's rule to each subinterval to obtain a rule over $\\left[a,b\\right]$:\n", "\n", "\\begin{equation}\n", "S_2 = \\frac{h}{12}\\left ( f \\left ( a\\right ) + 4f \\left ( d\\right ) + 2f\\left ( c\\right ) + 4f \\left ( e\\right ) + f \\left ( b\\right )\\right )\n", "\\end{equation}\n", "\n", "This is known as the Composite Simpson's rule, an example of a *composite* quadrature rule as it is a method that subdivides each interval. Composite methods produce much better results for oscillatory functions. Another composite rule we have used in previous lectures was cumtrapz, which uses the composite trapezoidal rule (you don't need to know any details about this rule).\n", "\n", "We finally don't need to write our own function, as scipy includes a function for the Composite Simpson's rule, simps. Since we have written functions above, let's make another one so we know it works in the same way as the others, so we can be sure our results are comparable." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def simpsons_composite_rule(start_point, end_point, function, number_of_bins=10):\n", " \n", " bin_size = float(end_point - start_point)/number_of_bins\n", " \n", " assert bin_size > 0\n", " assert type(number_of_bins) == int\n", " \n", " running_total = 0.0\n", " \n", " #Loop to create each shape \n", " for i in range(number_of_bins):\n", " #Find a, d, c, e, and b\n", " this_bin_start = start_point + bin_size * (i)\n", " this_bin_quarter = this_bin_start + bin_size*1./4\n", " this_bin_mid = this_bin_start + bin_size*2./4\n", " this_bin_three_quarter = this_bin_start + bin_size*3./4\n", " this_bin_end = this_bin_start + bin_size\n", " #Calculate the rule and add to running total. Sorry about the extremely long line...\n", " running_total += (bin_size/12)*float(function(this_bin_start)+4*function(this_bin_quarter)+2*function(this_bin_mid)+4*function(this_bin_three_quarter)+function(this_bin_end))\n", " \n", " #Return our running total result\n", " return running_total" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The area found by direct integration = 2\"\n", "print \"The area found with Composite Simpson's rule with 1 bin = \", simpsons_composite_rule(0,np.pi,sin,1)\n", "print \"The area found with Composite Simpson's rule with 2 bins = \", simpsons_composite_rule(0,np.pi,sin,2)\n", "print \"The area found with Composite Simpson's rule with 10 bins = \", simpsons_composite_rule(0,np.pi,sin,10)\n", "print \"The area found with Composite Simpson's rule with 100 bins = \", simpsons_composite_rule(0,np.pi,sin,100)\n", "print \"The area found with Composite Simpson's rule with 1000 bins = \", simpsons_composite_rule(0,np.pi,sin,1000)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The area found by direct integration = 2\n", "The area found with Composite Simpson's rule with 1 bin = 2.00455975498\n", "The area found with Composite Simpson's rule with 2 bins = 2.00026916995\n", "The area found with Composite Simpson's rule with 10 bins = 2.00000042309\n", "The area found with Composite Simpson's rule with 100 bins = 2.00000000004\n", "The area found with Composite Simpson's rule with 1000 bins = 2.0\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a slight improvement for a simple function like $\\sin$, but will be much more of an improvement for functions which oscillate more with respect to the size of our bins. " ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Rule 5: Weddle's rule" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are on our final rule... Given that $S$ and $S_2$ approximate the same integral, their difference can be used to estimate the error:\n", "\n", "\\begin{equation}\n", "E = \\left ( S_2 - S \\right)\n", "\\end{equation}\n", "\n", "Therefore, we can combine these rules to make an *even more* accurate approximation. Both of the rules are of order four, but the $S_2$ step size is half the S step size, so $S_2$ is roughly $2^4$ times as accurate. Let's call this super accurate rule $Q$, and we can find it by solving:\n", "\n", "\\begin{equation}\n", "Q - S = 16 \\left ( Q - S_2 \\right )\n", "\\end{equation}\n", "\n", "resulting in:\n", "\n", "\\begin{equation}\n", "Q = S_2 + \\frac {\\left (S_2 - S \\right )}{15} \n", "\\end{equation}\n", "\n", "This is known as Weddle's rule or the extrapolated Simposon's rule, because it uses two different values of $h$ and extrapolates towards $h = 0$. Making a function of this is easy as we just call our other two Simpson functions." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def weddles_rule(start_point, end_point, function, number_of_bins=10):\n", " result_simpson = simpsons_rule(start_point, end_point, function, number_of_bins)\n", " result_composite = simpsons_composite_rule(start_point, end_point, function, number_of_bins)\n", " \n", " return result_composite + float(result_composite - result_simpson)/15" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "print \"The area found by direct integration = 2\"\n", "print \"The area found with Weddle's rule with 1 bin = \", weddles_rule(0,np.pi,sin,1)\n", "print \"The area found with Weddle's rule with 2 bins = \", weddles_rule(0,np.pi,sin,2)\n", "print \"The area found with Weddle's rule with 10 bins = \", weddles_rule(0,np.pi,sin,10)\n", "print \"The area found with Weddle's rule with 100 bins = \", weddles_rule(0,np.pi,sin,100)\n", "print \"The area found with Weddle's rule with 1000 bins = \", weddles_rule(0,np.pi,sin,1000)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "The area found by direct integration = 2\n", "The area found with Weddle's rule with 1 bin = 1.99857073182\n", "The area found with Weddle's rule with 2 bins = 1.99998313095\n", "The area found with Weddle's rule with 10 bins = 1.999999999\n", "The area found with Weddle's rule with 100 bins = 2.0\n", "The area found with Weddle's rule with 1000 bins = 2.0\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can see our final rule is much more accurate for fewer required bins. This covers everything you need to know about quadrature. You will find the class problems in a different notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 } ], "metadata": {} } ] }