public static solve ( callable $function, number $a, number $b, number $tol ) : number | ||
$function | callable | f(x) callback function |
$a | number | The start of the interval which contains a root |
$b | number | The end of the interval which contains a root |
$tol | number | Tolerance; How close to the actual solution we would like. |
리턴 | number |
public static function solve(callable $function, $a, $b, $tol)
{
// Validate input arguments
self::validate($function, $a, $b, $tol);
do {
$f⟮a⟯ = $function($a);
$p = ($a + $b) / 2;
// construct the midpoint
$f⟮p⟯ = $function($p);
$dif = abs($f⟮p⟯);
// the magnitude of our function at the midpoint
if (Special::sgn($f⟮p⟯) !== Special::sgn($f⟮a⟯)) {
$b = $p;
// the new endpoint is our original midpoint
} else {
$a = $p;
// the new startpoint is our original endpoint
}
} while ($dif > $tol);
return $p;
}