PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
inosent1
Forum Commoner
Posts: 97 Joined: Wed Jan 28, 2009 12:18 pm
Post
by inosent1 » Sun Apr 22, 2012 2:22 pm
hi
with this code i get an amortized, principal and interest payment calculation:
amount: $100,000
term: 30 (years)
rate: 5%
payment: UNKNOWN
solve for payment ...
Code: Select all
// initialize variables
$sale_price = 0;
$annual_interest_percent = 0;
$year_term = 0;
//This function does the actual mortgage calculations by plotting a PVIFA (Present Value Interest Factor of Annuity) table...
function get_interest_factor($year_term, $monthly_interest_rate) {
global $base_rate;
$factor = 0;
$base_rate = 1 + $monthly_interest_rate;
$denominator = $base_rate;
for ($i=0; $i < ($year_term * 12); $i++) {
$factor += (1 / $denominator);
$denominator *= $base_rate;
}
return $factor;
}
// crunch the numbers
$month_term = $year_term * 12;
$annual_interest_rate = $annual_interest_percent / 100;
$monthly_interest_rate = $annual_interest_rate / 12;
$financing_price = $sale_price;
$monthly_factor = get_interest_factor($year_term, $monthly_interest_rate);
$monthly_payment = $financing_price / $monthly_factor;
payment: $536.82 PI
but if i have this:
amount: $100,000
term: 30 (years)
rate: UNKNOWN
payment: $536.82 PI
how do i write the code to find the RATE?
TIA
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Mon Apr 23, 2012 3:54 am
how is the rate calculated?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
litebearer
Forum Contributor
Posts: 194 Joined: Sat Mar 27, 2004 5:54 am
Post
by litebearer » Mon Apr 23, 2012 7:57 am
try this (you will need to create the applicable form)...
Code: Select all
<?PHP
session_start();
/*
script to calculate annual percentage rate
of a loan using monthly compounding
fixed term
fixed rate
fixed payments
*/
/* gather data */
$payment = ((int) ($_POST['payment']*100))/100;
$term = (int) $_POST['term'];
$loan = ((int) ($_POST['loan']*100))/100;
/* validate data */
if($payment<=0 || $term<=0 || $loan<=0) {
$my_result = "You MUST enter all three values - try again";
$_SESSION['myresult'] = $my_result;
header ("Location: roi00.php");
exit();
}
if($payment >= $loan) {
$my_result = "Payment must be less than Loan Amount - try again";
$_SESSION['myresult'] = $my_result;
header ("Location: roi00.php");
exit();
}
if($payment * $term <= $loan) {
$my_result = "Total Payments are less than Loan Amount - try again";
$_SESSION['myresult'] = $my_result;
header ("Location: roi00.php");
exit();
}
/* calculate addon rate, low rate, high rate */
/* DO NOT EDIT THIS SECTION */
$fv = $term * $payment;
$interest = $fv - $loan;
$years = $term/12;
$addon = $interest / $loan / $years;
$lo_i = $addon;
$hi_i = $addon *100;
/* create function DO NOT EDIT */
/* function returns the annual APR as a decimal */
function WhatRate($lo_i, $hi_i, $payment, $term, $loan) {
$done = 0;
while($done != 1) {
$i = ($lo_i + $hi_i)/2/12;
$x =1 + $i;
$y =pow($x, $term);
$z = 1/$y;
$w = 1-$z;
$u = $w/$i;
$pv = $payment * $u;
if($pv >$loan+.01) {
$lo_i = ($lo_i + $hi_i) /2;
}elseif($pv <$loan - .01) {
$hi_i = ($lo_i + $hi_i) /2;
}else{
$done=1;
}
}
return $i *12;
}
$apr = WhatRate($lo_i, $hi_i, $payment, $term, $loan) * 100;
setlocale(LC_MONETARY, 'en_US');
$my_result = "
<table>
<tr><td>Loan Amount: </td><td align=right>" . money_format('%i', $loan) . "</td></tr>
<tr><td>Monthly Payment: </td><td align=right>" . money_format('%i', $payment) . "</td></tr>
<tr><td>Number of Payments: </td><td align=right>" . $term . "</td></tr>
<tr><td>Total Payback: </td><td align=right>" . money_format('%i', $fv) . "</td></tr>
<tr><td>Total Interest Paid: </td><td align=right>" . money_format('%i', $interest) . "</td></tr>
<tr><td>Addon Rate: </td><td align=right>" . number_format($addon*100, 4) . "%</td></tr>
<tr><td>APY (simple interest rate): </td><td align=right>" . number_format($apr, 4) . "%</td></tr>
</table>";
$_SESSION['myresult'] = $my_result;
header ("Location: roi00.php");
?>
x_mutatis_mutandis_x
Forum Contributor
Posts: 160 Joined: Tue Apr 17, 2012 12:57 pm
Post
by x_mutatis_mutandis_x » Mon Apr 23, 2012 12:27 pm
This is more of a logic question. Use your mathematical (algebra) skills to solve this on paper first, and then worry about the code.