MathPHP\NumericalAnalysis\NumericalDifferentiation\FivePointFormulaTest::testNonzeroError PHP Method

testNonzeroError() public method

public testNonzeroError ( )
    public function testNonzeroError()
    {
        // f(x) = x⁵ - 13x² -92x + 96
        $f = function ($x) {
            return $x ** 5 - 13 * $x ** 2 - 92 * $x + 96;
        };
        /*
         *                                      h⁴
         * Error term for the Midpoint Formula: - f⁽⁵⁾(ζ₁)
         *                                      30
         *
         *     where ζ₁ lies between x₀ - 2h and x₀ + 2h
         *
         *                                      h⁴
         * Error term for the Endpoint Formula: - f⁽⁵⁾(ζ₀)
         *                                      5
         *
         *     where ζ₀ lies between x₀ and x₀ + 4h
         */
        // f'(x)   = 5x⁴ - 26x - 92
        // f''(x)  = 20x³ - 26
        // f⁽³⁾(x) = 60x²
        // f⁽⁴⁾(x) = 120x
        // f⁽⁵⁾(x) = 120
        // Error in Midpoint Formula on [0,4] (where h=1) < 4
        // Error in Endpoint Formula on [0,4] (where h=1) < 24
        $f’ = function ($x) {
            return 5 * $x ** 4 - 26 * $x - 92;
        };
        $n = 5;
        $a = 0;
        $b = 4;
        // Check that the endpoint formula agrees with f'(x) at x = 0
        $target = 0;
        $tol = 24;
        $expected = $f’($target);
        $actual = FivePointFormula::differentiate($target, $f, $a, $b, $n);
        $this->assertEquals($expected, $actual, '', $tol);
        // Check that the midpoint formula agrees with f'(x) at x = 2
        $target = 2;
        $tol = 4;
        $expected = $f’($target);
        $actual = FivePointFormula::differentiate($target, $f, $a, $b, $n);
        $this->assertEquals($expected, $actual, '', $tol);
        // Check that the (backward) endpoint formula agrees with f'(x) at x = 4
        $target = 4;
        $tol = 24;
        $expected = $f’($target);
        $actual = FivePointFormula::differentiate($target, $f, $a, $b, $n);
        $this->assertEquals($expected, $actual, '', $tol);
    }