Derive Amortization formula

Let the principal be $p$, the monthly interest rate be $r$, and the number of payments be $n$. Payments are assumed monthly. Hence, for a 30-year mortgage, the number of periods is 360. Note that if the yearly interest rate is $r_y$, then the monthly interest rate is $r = e^{{1\over12}log\left(1+r_y\right)}$.

Amortization

We first want to determine how much the monthly payment, $A$, is going to be. Let $p_m$ be the principal amount remaining after $m$ payments (i.e., $m$ periods). $$ \eqalign{ p_0 &= P\cr p_1 &= p_0\left(1+r\right) - A = P\left(1+r\right) - A \cr p_2 &= p_1\left(1+r\right) - A = P\left(1+r\right)^2 - A\left(1+r\right) - A \cr p_3 &= p_2\left(1+r\right) - A = P\left(1+r\right)^3 - A\left(1+r\right)^2 - A\left(1+r\right) - A \cr }$$ and generally, $$p_m = P\left(1+r\right)^m - A\sum_{k=0}^{m-1}\left(1+r\right)^k = P\left(1+r\right)^m - A{\left(1+r\right)^m-1\over r}\tag{1}$$ To find $A$, we use the fact that since we have $n$ payments, the $\left(n+1\right)^{th}$ payment is 0, or $$p_n = P(1+r)^n - A\left({\left(1+r\right)^n-1\over r}\right) = 0,$$ and therefore, $$A = P\left[{r\left(1+r\right)^n\over \left(1+r\right)^n-1}\right]\tag{2}$$ Now, substituting equation (2) in equation (1) yields, $$ p_m = P\left(1+r\right)^m - P\left[{r\left(1+r\right)^n\over \left(1+r\right)^n-1}\right] \left[ {\left(1+r\right)^m-1\over r}\right] = P\left[ { \left(1+r\right)^n- \left(1+r\right)^m \over\left(1+r\right)^n- 1 } \right],$$ and simplifying we get $$p_m = P\left[ 1- {\left(1+r\right)^m- 1 \over\left(1+r\right)^n- 1 } \right].\tag{3}$$ Note that the interest accumulated in the $m^{th}$ period (and, consequently, the interest portion of the $m^{th}$ payment) is $$I_m = r p_{m-1}, \tag{4}$$ which is the tax-deductible portion.