= 0) { throw new \Exception("Function values at endpoints must have opposite signs."); } $c = $b; $iteration = 0; $sign = 1; while (($y = \abs($func($c))) > self::EPSILON && $iteration < $maxIterations) { $fa = $func($a); $fb = $func($b); if ($y === 0.0) { return $c; } // @todo: c might be wrong, could be that if and else must be switched // @see https://en.wikipedia.org/wiki/Regula_falsi#The_Illinois_algorithm if ($y * $fa < 0) { $c = $sign === (int) ($y >= 0) ? (0.5 * $a * $fb - $b * $fa) / (0.5 * $fb - $fa) : ($a * $fb - $b * $fa) / ($fb - $fa); $b = $c; } else { $c = $sign === (int) ($y >= 0) ? ($a * $fb - 0.5 * $b * $fa) / ($fb - 0.5 * $fa) : ($a * $fb - $b * $fa) / ($fb - $fa); $a = $c; } $sign = (int) ($y > 0); ++$iteration; } return ($a + $b) / 2; } }