Numerical Analysis Problem (QBasic program from Feb. 1993)
'Numerical Analysis - Assignment #1 Problem #1
' The annuity equation, P = (A/i) * ( 1 - (1+i)^-n ) , is to be used
' to determine the maximum annual interest rate that a borrower can
' pay, given that the a 30-year mortgage in the amount of $75,000
' is to be paid off using payments of at most $625 per month.
' The function P(i) is to be defined, where i is the interest rate.
' The first derivative is determined analytically.
' Where A is the monthly payment, i is the interest rate (the independent
' variable), and n is the number of payments made.
Total = 75000 ' The amount to be paid off
A = 625 ' Amount of each payment
n = 30 * 12 ' The total number of payments to be made
Tolerance1 = .0000001
Tolerance2 = .0000001
DEF FNF (i) = (A / i) * (1 - (1 + i) ^ -n) - Total
'Function definition of F(i)
DEF FNFP (i) = (A / (i * i)) * ((1 + i) ^ (-n - 1) * (n * i + i + 1) - 1)
'Function definition of F'(i)
X1 = .02 'The initial "Guess" of monthly interest rate
X2 = X1
IF (FNF(X1) <> 0) AND (FNFP(X1) <> 0) THEN
PRINT
PRINT "Annual Rate"; TAB(30); "Amount Borrowed" 'Print Headings
DO
X1 = X2
X2 = X1 - FNF(X1) / FNFP(X1) 'Newton's Method
PRINT USING "#.####### "; X1 * 12; TAB(30); 'Print Annual Rate
PRINT USING "#######.##"; FNF(X1) + Total 'Print amt.borrowed
LOOP UNTIL (ABS(X1 - X2) < Tolerance1) OR (ABS(FNF(X2)) < Tolerance2)
PRINT USING "#.####### "; X2 * 12; TAB(30); 'Print Annual Rate
PRINT USING "#######.##"; FNF(X2) + Total 'Print amt borrowed
PRINT
PRINT "This indicates that the max. annual interest rate is approx. ";
PRINT USING " #.##### "; X2 * 1200;
PRINT " % ."
END IF
END
Program Output:
Annual Rate Amount Borrowed
0.2400000 31224.95
-.0983782 1400143.63
-.0548241 574445.75
-.0092432 259405.28
0.0359021 137636.23
0.0727020 91434.83
0.0908020 77122.45
0.0938944 75048.07
0.0939677 75000.02
0.0939678 75000.00
This indicates that the max. annual interest rate is approx. 9.39678 % .