-- This calculates the MonthlyPayment required to pay off an
-- AmountBorrowed at an AnnualPercentageRate in equal monthly payments
-- for a number of Years.

MonthlyPayment(AmountBorrowed,AnnualPercentageRate,Years):805.59
AmountBorrowed=100000; AnnualPercentageRate=7.5; Years=20

MonthlyPayment(Amount,Rate,Years)=Amount*installment(Rate/1200,Years*12)

-- Show how the MonthlyPayment changes with the term.
plot MonthlyPayment(AmountBorrowed,AnnualPercentageRate,X)
Xmin=5; Xmax=30; Ymin=0;  Ymax=AmountBorrowed/50

-- Baseline is interest payment only (no reduction of principal).
plot AmountBorrowed*AnnualPercentageRate/1200

-- installment required to amortize 1 in n periods
-- at interest rate of i per period
installment(i,n)=s(i,n)*i/(s(i,n)-1)

-- amount of 1 compounded at interest rate of i per period for n periods
s(i,n)=(1+i)^n
