math - Calculating interest rate in PHP with converted Excel RATE Function -


i'm having issue - 1 example works , next doesn't

here's code

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 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() 

and examples rate(60,-1338.88,274775,0,0,0.03200); // works - returns 3.40321

rate(60,-2415.44,448925,0,0,0.04150); // doesn't work - returns 3.16288 when should 4.32

any ideas? i've tried 4-5 pieces of code similar , 1 seems give me best results, though aren't consistent. feel code works, because how give me 1 correct result , rest aren't??

p.s: i'm newb @ posting here, i've been reading site 5 years though - hope thorough enough ;)

similar question, info, still doesn't solve problem secant method of iteration php

another question, still doesn't need... calculating interest rate in php


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

android - MPAndroidChart - How to add Annotations or images to the chart -