public function testSolve()
{
// f(x) = x⁴ + 8x³ -13x² -92x + 96
$f = function ($x) {
return $x ** 4 + 8 * $x ** 3 - 13 * $x ** 2 - 92 * $x + 96;
};
// Given n points, the error in the Newton Polynomials is proportional
// to the max value of the nth derivative. Thus, if we if interpolate n at
// 6 points, the 5th derivative of our original function f(x) = 0, and so
// our resulting polynomial will have no error.
$a = 0;
$b = 10;
$n = 5;
$p = NewtonPolynomialForward::interpolate($f, $a, $b, $n);
// Check that p(x) agrees with f(x) at x = 0
$expected = $f(0);
$actual = $p(0);
$this->assertEquals($expected, $actual);
// Check that p(x) agrees with f(x) at x = 2
$expected = $f(2);
$actual = $p(2);
$this->assertEquals($expected, $actual);
// Check that p(x) agrees with f(x) at x = 4
$expected = $f(4);
$actual = $p(4);
$this->assertEquals($expected, $actual);
// Check that p(x) agrees with f(x) at x = 6
$expected = $f(6);
$actual = $p(6);
$this->assertEquals($expected, $actual);
// Check that p(x) agrees with f(x) at x = 8
$expected = $f(8);
$actual = $p(8);
$this->assertEquals($expected, $actual);
// Check that p(x) agrees with f(x) at x = 10
$expected = $f(10);
$actual = $p(10);
$this->assertEquals($expected, $actual);
// Check that p(x) agrees with f(x) at x = -99
$expected = $f(-99);
$actual = $p(-99);
$this->assertEquals($expected, $actual);
}