Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // equation derived from explanation on wikipedia.
- // by @sweetl80
- // 10.2
- $term = 35; // duration minus one.
- $monthly_payment = 474.19;
- $total_credit = 21820.50;
- $final_payment = 9873;
- $t_apr = 0;
- $npv = 0;
- // ultimately, these are used for attempting to speed up the calculation with a staged increment system.
- $limit[1] = $total_credit * 0.00078;
- $limit[2] = $total_credit * 0.00030;
- $limit[3] = $total_credit * 0.00010;
- while (!$found){
- $npv_perc = round(($npv / $total_credit) * 100,4);
- /*
- * attempting staged increment
- */
- if ($npv_perc >= $limit[1]) $t_apr += 0.01; // lop off two decimal places (fast increment of apr)
- if ($npv_perc < $limit[1] && $npv_perc >= $limit[2]) $t_apr += 0.001; // add a hundredth. (slower increment)
- if ($npv_perc < $limit[2] && $npv_perc >= $limit[3]) $t_apr += 0.0001; // slower still.
- if ($npv_perc < $limit[3]) $t_apr += 0.00001; // the slowest, for accuracy.
- /*
- * if you dont like the 'staged' setup above, you could just do this instead, but it can lead to hundreds of thousands of calculations
- // $t_apr += 0.00001; // slow, but accurate.
- */
- $npv = -$total_credit;
- for ($m = 1; $m <= $term; $m++){
- if ($m < $term){
- $val = $monthly_payment;
- } else {
- $val = $monthly_payment + $final_payment;
- }
- $npv += ($val / pow(1 + $t_apr,$m/12));
- }
- $loops++;
- if ($npv <= 0){
- $apr = round($t_apr * 100,1);
- break;
- }
- }
- ?>
- <h1><?=$apr?> found after <?=$loops?> loops</h1>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement