我也有这个问题 - 一个例子有效,下一个没有
这是我的代码
define('FINANCIAL_MAX_ITERATIONS', 128); define('FINANCIAL_PRECISION', 1.0e-08);
function RATE($nper, $pmt, $pv, $fv = 0.0, $type = 0, $guess = 0.1) {
$rate = $guess;
if (abs($rate) < FINANCIAL_PRECISION) {
$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 = $pv + $pmt * $nper + $fv;
$y1 = $pv * $f + $pmt * (1 / $rate + $type) * ($f - 1) + $fv;
// find root by secant method
$i = $x0 = 0.0;
$x1 = $rate;
while ((abs($y0 - $y1) > FINANCIAL_PRECISION) && ($i < FINANCIAL_MAX_ITERATIONS)) {
$rate = ($y1 * $x0 - $y0 * $x1) / ($y1 - $y0);
$x0 = $x1;
$x1 = $rate;
if (($nper * abs($pmt)) > ($pv - $fv))
$x1 = abs($x1);
if (abs($rate) < FINANCIAL_PRECISION) {
$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;
}
$rate = abs(round($rate*100,5));
return $rate;
} // function RATE()
和一些例子RATE(60,-1338.88,274775,0,0,0.03200); //作品 - 返回3.40321
RATE(60,-2415.44,448925,0,0,0.04150); //不起作用 - 当它应该是4.32时返回3.16288
有什么想法吗?我已经尝试了4-5段代码,它们看起来都很相似,这个代码似乎给了我最好的结果,即使它们不一致。我觉得代码有效,因为它怎么能给我一个正确的结果呢?其余的不是&#39;
p.s:我是张贴在这里的新手,我已经阅读了这个网站大约5年了 - 希望我足够彻底;)
类似的问题,有良好的信息,但仍然没有解决我的问题 Secant method of iteration into PHP
另一个好问题,但仍然没有做我需要的...... Calculating interest rate in PHP