MathPHP\NumericalAnalysis\RootFinding\SecantMethodTest::testSolve PHP Метод

testSolve() публичный Метод

public testSolve ( )
    public function testSolve()
    {
        // f(x) = x⁴ + 8x³ -13x² -92x + 96
        // This polynomial has 4 roots: 3,1,-8 and -4
        $func = function ($x) {
            return $x ** 4 + 8 * $x ** 3 - 13 * $x ** 2 - 92 * $x + 96;
        };
        $tol = 1.0E-5;
        // Solve for f(x) = 0 where x is -4
        $expected = -4;
        $p₀ = -5;
        $p₁ = -2;
        $x = SecantMethod::solve($func, $p₀, $p₁, $tol);
        $this->assertEquals($expected, $x, '', $tol);
        // Solve for f(x) = 0 where x is -8
        $expected = -8;
        $p₀ = -10;
        $p₁ = -7;
        $x = SecantMethod::solve($func, $p₀, $p₁, $tol);
        $this->assertEquals($expected, $x, '', $tol);
        // Solve for f(x) = 0 where x is 3
        $expected = 3;
        $p₀ = 2;
        $p₁ = 5;
        $x = SecantMethod::solve($func, $p₀, $p₁, $tol);
        $this->assertEquals($expected, $x, '', $tol);
        // Solve for f(x) = 0 where x is 1
        $expected = 1;
        $p₀ = -1;
        $p₁ = 2;
        $x = SecantMethod::solve($func, $p₀, $p₁, $tol);
        $this->assertEquals($expected, $x, '', $tol);
        // Switch p₀ and p₁ and test that they get reversed properly
        // Solve for f(x) = 0 where x is 1
        $expected = 1;
        $p₁ = -1;
        $p₀ = 2;
        $x = SecantMethod::solve($func, $p₀, $p₁, $tol);
        $this->assertEquals($expected, $x, '', $tol);
    }