public static solve ( callable $function, array $args, number $target, number $tol, number $position ) : number | ||
$function | callable | f(x) callback function |
$args | array | Parameters to pass to callback function. The initial value for the parameter of interest must be in this array. |
$target | number | Value of f(x) we a trying to solve for |
$tol | number | Tolerance; How close to the actual solution we would like. |
$position | number | Which element in the $args array will be changed; also serves as initial guess |
return | number |
public static function solve(callable $function, array $args, $target, $tol, $position = 0)
{
Validation::tolerance($tol);
// Initialize
$args1 = $args;
$guess = $args[$position];
do {
$args1[$position] = $guess + $tol;
// load the initial guess into the arguments
$args[$position] = $guess;
// load the initial guess into the arguments
$y = call_user_func_array($function, $args);
$y_at_xplusdelx = call_user_func_array($function, $args1);
$slope = ($y_at_xplusdelx - $y) / $tol;
$del_y = $target - $y;
$guess = $del_y / $slope + $guess;
$dif = abs($del_y);
} while ($dif > $tol);
return $guess;
}