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 Nevilles Method (Lagrange 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;
$roundoff = 1.0E-7;
// round off error
// Check that p(x) agrees with f(x) at x = 0
$target = 0;
$expected = $f($target);
$actual = NevillesMethod::interpolate($target, $f, $a, $b, $n);
$this->assertEquals($expected, $actual, '', +$roundoff);
// Check that p(x) agrees with f(x) at x = 2
$target = 2;
$expected = $f($target);
$actual = NevillesMethod::interpolate($target, $f, $a, $b, $n);
$this->assertEquals($expected, $actual, '', +$roundoff);
// Check that p(x) agrees with f(x) at x = 4
$target = 4;
$expected = $f($target);
$actual = NevillesMethod::interpolate($target, $f, $a, $b, $n);
$this->assertEquals($expected, $actual, '', +$roundoff);
// Check that p(x) agrees with f(x) at x = 6
$target = 6;
$expected = $f($target);
$actual = NevillesMethod::interpolate($target, $f, $a, $b, $n);
$this->assertEquals($expected, $actual, '', +$roundoff);
// Check that p(x) agrees with f(x) at x = 8
$target = 8;
$expected = $f($target);
$actual = NevillesMethod::interpolate($target, $f, $a, $b, $n);
$this->assertEquals($expected, $actual, '', +$roundoff);
// Check that p(x) agrees with f(x) at x = 10
$target = 10;
$expected = $f($target);
$actual = NevillesMethod::interpolate($target, $f, $a, $b, $n);
$this->assertEquals($expected, $actual, '', +$roundoff);
// Check that p(x) agrees with f(x) at x = -90
$target = -90;
$expected = $f($target);
$actual = NevillesMethod::interpolate($target, $f, $a, $b, $n);
$this->assertEquals($expected, $actual, '', +$roundoff);
}