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.