nitro29 Posted April 15, 2014 Share Posted April 15, 2014 Hi, Who can make this work (return $rate), and make me very happy!? 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 (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; } return $rate; } // function RATE() Could be with following values: $nper=120 $pmt=1009.06 $pv=100,000 This is originally from StackOverflow . Thanks a million. Link to comment https://forums.phpfreaks.com/topic/287781-recreate-excel-rate-function-who-can-make-it-work/ Share on other sites More sharing options...
Ch0cu3r Posted April 15, 2014 Share Posted April 15, 2014 Umm.. You call that function with those values... // constants required by the function define('FINANCIAL_MAX_ITERATIONS', 128); define('FINANCIAL_PRECISION', 1.0e-08); $nper=120; $pmt=1009.06; $pv=100000; // pass the above values as arguments echo RATE($nper, $pmt, $pv); // output the returned rate Dont forget to define the constants Link to comment https://forums.phpfreaks.com/topic/287781-recreate-excel-rate-function-who-can-make-it-work/#findComment-1476197 Share on other sites More sharing options...
nitro29 Posted April 15, 2014 Author Share Posted April 15, 2014 Uhm. Ok. I can see that it is a very silly question if the answer is that easy. I will try this again. Thanks EDIT: This was the worst question ever. Sorry. Link to comment https://forums.phpfreaks.com/topic/287781-recreate-excel-rate-function-who-can-make-it-work/#findComment-1476200 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.