BoolesRule.php 5.85 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
<?php
namespace MathPHP\NumericalAnalysis\NumericalIntegration;

use MathPHP\NumericalAnalysis\Interpolation\LagrangePolynomial;
use MathPHP\Functions\Polynomial;

/**
 * Boole's Rule
 *
 * In numerical analysis, Boole's rule is a technique for approximating
 * the definite integral of a function.
 *
 * Boole's rule belongs to the closed Newton-Cotes formulas, a group of methods
 * for numerical integration which approximate the integral of a function. We
 * can either directly supply a set of inputs and their corresponding outputs for
 * said function, or if we explicitly know the function, we can define it as a
 * callback function and then generate a set of points by evaluating that function
 * at n points between a start and end point. We then use these values to
 * interpolate a Lagrange polynomial. Finally, we integrate the Lagrange
 * polynomial to approximate the integral of our original function.
 *
 * Boole's rule is produced by integrating the fourth Lagrange polynomial.
 *
 * https://en.wikipedia.org/wiki/Boole%27s_rule
 * http://mathworld.wolfram.com/BoolesRule.html
 * http://www.efunda.com/math/num_integration/num_int_newton.cfm
 */
class BoolesRule extends NumericalIntegration
{
    /**
     * Use Boole's Rule to aproximate the definite integral of a
     * function f(x). Our input can support either a set of arrays, or a callback
     * function with arguments (to produce a set of arrays). Each array in our
     * input contains two numbers which correspond to coordinates (x, y) or
     * equivalently, (x, f(x)), of the function f(x) whose definite integral we
     * are approximating.
     *
     * Note: Boole's rule requires that our number of subintervals is a factor
     * of four (we must supply an n points such that n-1 is a multiple of four)
     * and also that the size of each subinterval is equal (spacing between each
     * point is equal).
     *
     * The bounds of the definite integral to which we are approximating is
     * determined by the our inputs.
     *
     * Example: approximate([0, 10], [2, 5], [4, 7], [6,3]) will approximate the
     * definite integral of the function that produces these coordinates with a
     * lower bound of 0, and an upper bound of 6.
     *
     * Example: approximate(function($x) {return $x**2;}, [0, 3 ,5]) will produce
     * a set of arrays by evaluating the callback at 5 evenly spaced points
     * between 0 and 3. Then, this array will be used in our approximation.
     *
     * Boole's Rule:
     *
     * xn        ⁿ⁻¹ xᵢ₊₁
     * ∫ f(x)dx = ∑   ∫ f(x)dx
     * x₁        ⁱ⁼¹  xᵢ
     *
     *         ⁽ⁿ⁻¹⁾/⁴ 2h
     *          = ∑    -- [7f⟮x₄ᵢ₋₃⟯ + 32f⟮x₄ᵢ₋₂⟯ + 12f⟮x₄ᵢ₋₁⟯ + 32f⟮x₄ᵢ⟯ + 7f⟮x₄ᵢ₊₁⟯] + O(h⁷f⁽⁶⁾(x))
     *           ⁱ⁼¹   45
     * where h = (xn - x₁) / (n - 1)
     *
     * @param callable|array $source The source of our approximation. Should be either
     *                           a callback function or a set of arrays. Each array
     *                           (point) contains precisely two numbers, an x and y.
     *                           Example array: [[1,2], [2,3], [3,4], [4,5], [5,6]].
     *                           Example callback: function($x) {return $x**2;}
     * @param number   ... $args The arguments of our callback function: start,
     *                           end, and n. Example: approximate($source, 0, 8, 4).
     *                           If $source is a set of points, do not input any
     *                           $args. Example: approximate($source).
     *
     * @return number            The approximation to the integral of f(x)
     */
    public static function approximate($source, ... $args)
    {
        // get an array of points from our $source argument
        $points = self::getPoints($source, $args);

        // Validate input and sort points
        self::validate($points, $degree = 5);
        Validation::isSubintervalsMultiple($points, $m = 4);
        $sorted = self::sort($points);
        Validation::isSpacingConstant($sorted);

        // Descriptive constants
        $x = self::X;
        $y = self::Y;

        // Initialize
        $n             = count($sorted);
        $subintervals  = $n - 1;
        $a             = $sorted[0][$x];
        $b             = $sorted[$n-1][$x];
        $h             = ($b - $a) / $subintervals;
        $approximation = 0;

        /*
        * ⁽ⁿ⁻¹⁾/⁴ 2h
        *  = ∑    -- [7f⟮x₄ᵢ₋₃⟯ + 32f⟮x₄ᵢ₋₂⟯ + 12f⟮x₄ᵢ₋₁⟯ + 32f⟮x₄ᵢ⟯ + 7f⟮x₄ᵢ₊₁⟯] + O(h⁷f⁽⁶⁾(x))
        *   ⁱ⁼¹   45
         */
        for ($i = 1; $i < ($subintervals/4) + 1; $i++) {
            $x₄ᵢ₋₃          = $sorted[(4*$i)-4][$x];
            $x₄ᵢ₋₂          = $sorted[(4*$i)-3][$x];
            $x₄ᵢ₋₁          = $sorted[(4*$i)-2][$x];
            $x₄ᵢ            = $sorted[(4*$i)-1][$x];
            $x₄ᵢ₊₁          = $sorted[(4*$i)][$x];
            $fx₄ᵢ₋₃⟯        = $sorted[(4*$i)-4][$y]; // y₄ᵢ₋₃
            $fx₄ᵢ₋₂⟯        = $sorted[(4*$i)-3][$y]; // y₄ᵢ₋₂
            $fx₄ᵢ₋₁⟯        = $sorted[(4*$i)-2][$y]; // y₄ᵢ₋₁
            $fx₄ᵢ          = $sorted[(4*$i)-1][$y]; // y₄ᵢ
            $fx₄ᵢ₊₁⟯        = $sorted[(4*$i)][$y];   // y₄ᵢ₊₁
            $lagrange       = LagrangePolynomial::interpolate([[$x₄ᵢ₋₃, $fx₄ᵢ₋₃⟯], [$x₄ᵢ₋₂, $fx₄ᵢ₋₂⟯], [$x₄ᵢ₋₁, $fx₄ᵢ₋₁⟯], [$x₄ᵢ, $fx₄ᵢ], [$x₄ᵢ₊₁, $fx₄ᵢ₊₁⟯]]);
            $integral       = $lagrange->integrate();
            $approximation += $integral($x₄ᵢ₊₁) - $integral($x₄ᵢ₋₃); // definite integral of lagrange polynomial
        }

        return $approximation;
    }
}