How would i turn the secant method of iteration into php?
The formula is xi+1= xi - (f(xi) (xi-xi+1))/f(xi)-f(xi-1)
where f = function and i is an iteration.
so xi+1 is the next iteration after xi
So far I have seen this, but there is somethning wrong somewhere in it so i want to make one from scratch, unless someone here can see what is wrong with it?
while ((abs($y0 - $y1) > FINANCIAL_ACCURACY) && ($i < FINANCIAL_MAX_ITERATIONS))
{
$rate = ($y1 * $x0 - $y0 * $x1) / ($y1 - $y0);
$x0 = $x1;
$x1 = $rate;
if (abs($rate) < FINANCIAL_ACCURACY) {
$y = $pv * (1 + $nper * $rate) + $pmt * (1 + $rate * $type) * $nper + $fv;
} else {
$f = exp($nper * log(1 + $rate));
$y = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
}
$y0 = $y1;
$y1 = $y;
$i++;
}
Thanks
forgot to add, FINANCIAL_ACCURACY is defined as
define('FINANCIAL_ACCURACY', 1.0e-6);