-----------------------------《PHP与MySQL程序设计(第四版)》中第73页4.2.6节分期还贷计算器数学原理分析。 《PHP与MySQL程序设计(第四版)》中73页4.2.6节,代码清单4-1是还贷计算器函数amortizationTable,代码如下。 其使用的是等额本息分期还款模型。要理解这段代码,关键是理解月还款额b的计算公式背后的数学原理。 设本金a,还款月数n,月利率r,每月还款b。还款计算如下: 首月利息a*r,还款后,余额a1=a(1+r)-b;次月利息a1*r,次月还款后余额a2=a1(1+r)-b=(a(1+r)-b)(1+r)-b=a(1+r)^2-b[(1+r)+1], 如此类推,最后一个月还清,有余额an=a(1+r)^n-b[(1+r)^(n-1)+(1+r)^(n-2)+……1]=0。 可见中括号内为等比数列,根据等比数列计算公式可得an=a(1+r)^n-b((1+r)^n-1)/r = 0,所以有b=ar(1+r)^n/((1+r)^n-1)。 代码中,$periodicPayment即b,$balance即a,$intCalc即1+r,$totalPayments即n,所以有 $periodicPayment = $balance * pow($intCalc,$totalPayments) * ($intCalc - 1) / (pow($intCalc,$totalPayments) - 1); 回过头再看计算器函数,根据以上原理分析,每月的计算如下: 设上期贷款余额a1,本期余额a2,则有:a2=a1(1+r)-b=a1-(b-a1×r),也就是本期还款剔除利息,剩余的就是上期余额扣除部分, 得到本期余额,如此迭代。体现在代码上就是: $paymentInterest = round($balance * $monthlyInterest, 2); $paymentPrincipal = round($periodicPayment - $paymentInterest, 2); $newBalance = round($balance - $paymentPrincipal, 2);
分期还贷计算器完整代码 <?php
function amortizationTable($pNum, $periodicPayment, $balance, $monthlyInterest) { // Calculate payment interest $paymentInterest = round($balance * $monthlyInterest, 2);
// Calculate payment principal $paymentPrincipal = round($periodicPayment - $paymentInterest, 2);
// Deduct principal from remaining balance $newBalance = round($balance - $paymentPrincipal, 2);
// If new balance < monthly payment, set to zero if ($newBalance < $paymentPrincipal) { $newBalance = 0; }
printf("<tr><td>%d</td>", $pNum); printf("<td>$%s</td>", number_format($newBalance, 2)); printf("<td>$%s</td>", number_format($periodicPayment, 2)); printf("<td>$%s</td>", number_format($paymentPrincipal, 2)); printf("<td>$%s</td></tr>", number_format($paymentInterest, 2));
# If balance not yet zero, recursively call amortizationTable() if ($newBalance > 0) { $pNum++; amortizationTable($pNum, $periodicPayment, $newBalance, $monthlyInterest); } else { return 0; }
}
// Loan balance $balance = 10000.00;
// Loan interest rate $interestRate = .0575;
// Monthly interest rate $monthlyInterest = $interestRate / 12;
// Term length of the loan, in years. $termLength = 5;
// Number of payments per year. $paymentsPerYear = 12;
// Payment iteration $paymentNumber = 1;
//还款月数m // Determine total number payments $totalPayments = $termLength * $paymentsPerYear;
//复利1+r // Determine interest component of periodic payment $intCalc = 1 + $interestRate / $paymentsPerYear;
//计算月还款额b // Determine periodic payment $periodicPayment = $balance * pow($intCalc,$totalPayments) * ($intCalc - 1) / (pow($intCalc,$totalPayments) - 1);
// Round periodic payment to two decimals $periodicPayment = round($periodicPayment,2);
// Create table echo "<table width='50%' align='center' border='1'>"; echo "<tr> <th>Payment Number</th><th>Balance</th> <th>Payment</th><th>Principal</th><th>Interest</th> </tr>";
// Call recursive function amortizationTable($paymentNumber, $periodicPayment, $balance, $monthlyInterest);
// Close table echo "</table>";
?>
//以上代码贷款1万元5年还清,年息(5.75%),代码运行结果得出的还款逐月情况如下: Payment Number Balance Payment Principal Interest 1 $9,855.75 $192.17 $144.25 $47.92 2 $9,710.81 $192.17 $144.94 $47.23 3 $9,565.17 $192.17 $145.64 $46.53 4 $9,418.83 $192.17 $146.34 $45.83 5 $9,271.79 $192.17 $147.04 $45.13 6 $9,124.05 $192.17 $147.74 $44.43 7 $8,975.60 $192.17 $148.45 $43.72 8 $8,826.44 $192.17 $149.16 $43.01 9 $8,676.56 $192.17 $149.88 $42.29 10 $8,525.97 $192.17 $150.59 $41.58 11 $8,374.65 $192.17 $151.32 $40.85 12 $8,222.61 $192.17 $152.04 $40.13 13 $8,069.84 $192.17 $152.77 $39.40 14 $7,916.34 $192.17 $153.50 $38.67 15 $7,762.10 $192.17 $154.24 $37.93 16 $7,607.12 $192.17 $154.98 $37.19 17 $7,451.40 $192.17 $155.72 $36.45 18 $7,294.93 $192.17 $156.47 $35.70 19 $7,137.71 $192.17 $157.22 $34.95 20 $6,979.74 $192.17 $157.97 $34.20 21 $6,821.01 $192.17 $158.73 $33.44 22 $6,661.52 $192.17 $159.49 $32.68 23 $6,501.27 $192.17 $160.25 $31.92 24 $6,340.25 $192.17 $161.02 $31.15 25 $6,178.46 $192.17 $161.79 $30.38 26 $6,015.90 $192.17 $162.56 $29.61 27 $5,852.56 $192.17 $163.34 $28.83 28 $5,688.43 $192.17 $164.13 $28.04 29 $5,523.52 $192.17 $164.91 $27.26 30 $5,357.82 $192.17 $165.70 $26.47 31 $5,191.32 $192.17 $166.50 $25.67 32 $5,024.03 $192.17 $167.29 $24.88 33 $4,855.93 $192.17 $168.10 $24.07 34 $4,687.03 $192.17 $168.90 $23.27 35 $4,517.32 $192.17 $169.71 $22.46 36 $4,346.80 $192.17 $170.52 $21.65 37 $4,175.46 $192.17 $171.34 $20.83 38 $4,003.30 $192.17 $172.16 $20.01 39 $3,830.31 $192.17 $172.99 $19.18 40 $3,656.49 $192.17 $173.82 $18.35 41 $3,481.84 $192.17 $174.65 $17.52 42 $3,306.35 $192.17 $175.49 $16.68 43 $3,130.02 $192.17 $176.33 $15.84 44 $2,952.85 $192.17 $177.17 $15.00 45 $2,774.83 $192.17 $178.02 $14.15 46 $2,595.96 $192.17 $178.87 $13.30 47 $2,416.23 $192.17 $179.73 $12.44 48 $2,235.64 $192.17 $180.59 $11.58 49 $2,054.18 $192.17 $181.46 $10.71 50 $1,871.85 $192.17 $182.33 $9.84 51 $1,688.65 $192.17 $183.20 $8.97 52 $1,504.57 $192.17 $184.08 $8.09 53 $1,319.61 $192.17 $184.96 $7.21 54 $1,133.76 $192.17 $185.85 $6.32 55 $947.02 $192.17 $186.74 $5.43 56 $759.39 $192.17 $187.63 $4.54 57 $570.86 $192.17 $188.53 $3.64 58 $381.43 $192.17 $189.43 $2.74 59 $191.09 $192.17 $190.34 $1.83 60 $0.00 $192.17 $191.25 $0.92