views:

45

answers:

1

I am trying to loop the below iteration, the code is below and i will try and break it down aswell.

<?php

    function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
     $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
     return $intr;
    }
    $x0=0.008;
    $x1=0.025;
    $LoanRequired=20000;
    $TermMonths=120;
    $MonthlyPayment=-271.09;
    $x2=$x0-(((intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0))*($x0-$x1))/(intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0)-intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1)));
    print_r($x2);
    $intr = $LoanRequired * pow((1 + $x2), $TermMonths) + $MonthlyPayment * ((pow((1 + $x2), $TermMonths) - 1) / $x2);
    while( $intr>=-1 || $intr<=1 ) {

    return ;
    }
    ?>

$x0=0.008; this doesn't change, $x0 is the first guess $x1=0.025; this doesn't change $x1 is the second guess

$LoanRequired=20000;
$TermMonths=120;
$MonthlyPayment=-271.09;

All variables that will come from a form (all of these will change depending on what the user inputs)

function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
         $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
         return $intr;

in this function $rate becomes $x0 at first, then $x1 and $x2 etc.

$intr = $LoanRequired * pow((1 + $x0), $TermMonths) + $MonthlyPayment * ((pow((1 + $x0), $TermMonths) - 1) / $x0);

first it needs to go into this formula above, ($x0 first and then $x1 as they are the two static ones) the whole process stops if the result turns out to be <1 or >-1

if the first two static guesses do not meet the criteria it then needs to do this

$x2=$x0-(((intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0))*($x0-$x1))/(intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0)-intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1))); in a loop. In this case $x2 will =0.0082002592938519 which will then be put into $intr = $LoanRequired * pow((1 + $x0), $TermMonths) + $MonthlyPayment * ((pow((1 + $x0), $TermMonths) - 1) / $x0); again and if it does not meet the criteria it continues the loop until it does with $x3, $x4 etc

after that it needs to go into this formula $apr=((pow(($x0+1),12))-1)*100; (the $x0 would obviously change to $x1, $x2, $x3 etc depending on which value was <1 and >-1.

I am trying to put this into a while loop,(or any loop for that matter) but having some difficulty.

any help would be appreciated.

+2  A: 

Let's start with setting

$i0 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0);
$i1 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1);

Then x2 becomes

$x2=$x0-($i0*($x0-$x1)/($i0-$i1));

and

$i2 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x2);

Now, if there is a next step you don't need the current $x0/$i0 any more. The current $x1/$i1 become $x0/$i0 and $x2/$i2 become $x1/$i1

$x0 = $x1; $i0=$i1;
$x1 = $x2; $i1=$i2;

And you do that while ($i2 <= -1 || $i2 &gt= 1), i.e. while ( abs($i2) >= 1 )

<?php
function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) {
  $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate);
  return $intr;
}

$LoanRequired=20000;
$TermMonths=120;
$MonthlyPayment=-271.09;
$x0=0.008;
$x1=0.025;
$i0 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0);
$i1 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1);

echo "f($x0)=", $i0, "\n";
echo "f($x1)=", $i1, "\n";

$dbgcnt = 0;
do {
  if ( 100 < ++$dbgcnt ) {
    die('bailout');
  }

  $x2=$x0-($i0*($x0-$x1)/($i0-$i1));
  $i2 = intr($LoanRequired,$TermMonths,$MonthlyPayment,$x2);
  echo "f($x2)=", $i2, "\n";

  $x0 = $x1; $i0=$i1;
  $x1 = $x2; $i1=$i2;
} while (abs($i2) > 1);

echo "----\nx=$x2";

prints

f(0.008)=-2242.1586767476
f(0.025)=188094.563138
f(0.0082002592938519)=-1736.2922063647
f(0.0083539185016984)=-1336.4282682791
f(0.0088674794645362)=76.76523796426
f(0.0088395826270916)=-3.1241157534314
f(0.0088406735477872)=-0.0068921130805393
----
x=0.0088406735477872

But if this is of any importance you should ask over at http://mathoverflow.net/ whether this method is safe to use with IEEE floats (my guess is it's not).

VolkerK
Thanks! works perfect
James