《PHP與MySQL程序設計(第四版)》中第73頁4.2.6節分期還貸計算器數學原理分析

-----------------------------《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
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章