Debian Codegraph @

monthlypmt.sh

raw symbols SCIP
1 #!/bin/bash
2 # monthlypmt.sh: Calculates monthly payment on a mortgage.
3
4
5 # This is a modification of code in the
6 #+ "mcalc" (mortgage calculator) package,
7 #+ by Jeff Schmidt
8 #+ and
9 #+ Mendel Cooper (yours truly, the ABS Guide author).
10 # http://www.ibiblio.org/pub/Linux/apps/financial/mcalc-1.6.tar.gz
11
12 echo
13 echo "Given the principal, interest rate, and term of a mortgage,"
14 echo "calculate the monthly payment."
15
16 bottom=1.0
17
18 echo
19 echo -n "Enter principal (no commas) "
20 read principal
21 echo -n "Enter interest rate (percent) " # If 12%, enter "12", not ".12".
22 read interest_r
23 echo -n "Enter term (months) "
24 read term
25
26
27 interest_r=$(echo "scale=9; $interest_r/100.0" | bc) # Convert to decimal.
28 # ^^^^^^^^^^^^^^^^^ Divide by 100.
29 # "scale" determines how many decimal places.
30
31 interest_rate=$(echo "scale=9; $interest_r/12 + 1.0" | bc)
32
33
34 top=$(echo "scale=9; $principal*$interest_rate^$term" | bc)
35 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36 # Standard formula for figuring interest.
37
38 echo; echo "Please be patient. This may take a while."
39
40 let "months = $term - 1"
41 # ====================================================================
42 for ((x=$months; x > 0; x--))
43 do
44 bot=$(echo "scale=9; $interest_rate^$x" | bc)
45 bottom=$(echo "scale=9; $bottom+$bot" | bc)
46 # bottom = $(($bottom + $bot"))
47 done
48 # ====================================================================
49
50 # --------------------------------------------------------------------
51 # Rick Boivie pointed out a more efficient implementation
52 #+ of the above loop, which decreases computation time by 2/3.
53
54 # for ((x=1; x <= $months; x++))
55 # do
56 # bottom=$(echo "scale=9; $bottom * $interest_rate + 1" | bc)
57 # done
58
59
60 # And then he came up with an even more efficient alternative,
61 #+ one that cuts down the run time by about 95%!
62
63 # bottom=`{
64 # echo "scale=9; bottom=$bottom; interest_rate=$interest_rate"
65 # for ((x=1; x <= $months; x++))
66 # do
67 # echo 'bottom = bottom * interest_rate + 1'
68 # done
69 # echo 'bottom'
70 # } | bc` # Embeds a 'for loop' within command substitution.
71 # --------------------------------------------------------------------------
72 # On the other hand, Frank Wang suggests:
73 # bottom=$(echo "scale=9; ($interest_rate^$term-1)/($interest_rate-1)" | bc)
74
75 # Because . . .
76 # The algorithm behind the loop
77 #+ is actually a sum of geometric proportion series.
78 # The sum formula is e0(1-q^n)/(1-q),
79 #+ where e0 is the first element and q=e(n+1)/e(n)
80 #+ and n is the number of elements.
81 # --------------------------------------------------------------------------
82
83
84 # let "payment = $top/$bottom"
85 payment=$(echo "scale=2; $top/$bottom" | bc)
86 # Use two decimal places for dollars and cents.
87
88 echo
89 echo "monthly payment = \$$payment" # Echo a dollar sign in front of amount.
90 echo
91
92
93 exit 0
94
95
96 # Exercises:
97 # 1) Filter input to permit commas in principal amount.
98 # 2) Filter input to permit interest to be entered as percent or decimal.
99 # 3) If you are really ambitious,
100 #+ expand this script to print complete amortization tables.