loop for these iterations

willy28

New Member
I am trying to loop the below iteration, the code is below and i will try and break it down aswell.\[code\]<?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 ; } ?>\[/code\]\[code\]$x0=0.008;\[/code\] this doesn't change, $x0 is the first guess\[code\]$x1=0.025;\[/code\] this doesn't change $x1 is the second guess\[code\]$LoanRequired=20000;$TermMonths=120;$MonthlyPayment=-271.09;\[/code\]All variables that will come from a form (all of these will change depending on what the user inputs)\[code\]function intr($LoanRequired, $TermMonths, $MonthlyPayment, $rate) { $intr= $LoanRequired * pow((1 + $rate), $TermMonths) + $MonthlyPayment * ((pow((1 + $rate), $TermMonths) - 1) / $rate); return $intr;\[/code\]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 >-1if the first two static guesses do not meet the criteria it then needs to do this\[code\]$x2=$x0-(((intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0))*($x0-$x1))/(intr($LoanRequired,$TermMonths,$MonthlyPayment,$x0)-intr($LoanRequired,$TermMonths,$MonthlyPayment,$x1)));\[/code\] in a loop.In this case $x2 will =0.0082002592938519 which will then be put into \[code\]$intr = $LoanRequired * pow((1 + $x0), $TermMonths) + $MonthlyPayment * ((pow((1 + $x0), $TermMonths) - 1) / $x0);\[/code\] again and if it does not meet the criteria it continues the loop until it does with $x3, $x4 etcafter that it needs to go into this formula \[code\]$apr=((pow(($x0+1),12))-1)*100;\[/code\] (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.
 
Back
Top