How much should the mortgage be

You're going to be paying down a mortgage.

Question, how much does \( M \) have to be for a given \( P \) and \( r \) as a function of \( D \)?

This could be a good topic for a dynamics class, since it involves discrete time. But let's model it continuously, as a differential equation in the amount remaining, \( P \). If we know the interest rate, monthly payment, and principal, we can find the amount owing at any time. For example:

p = integrateODE(dP ~ r * P - M, r = 0.05/12, M = 100, P = 10000, 
    tdur = 500)

Now there is a function. We can find how long we need to pay to bring the function to zero:

findZeros(p$P(t) ~ t, t.lim = c(0, 500))
## [1] 129.4

This tells us the number of months that the loan would have to be paid to reach zero.

The question, however, was how much the monthly payment needs to be to bring the amount to zero at time \( D \). To set this up, let's integrate to time \( D \) and see how much money is owing.

left.at.D = function(M, D = 30 * 12) {
    p = integrateODE(dP ~ r * P - M, r = 0.05/12, M = M, P = 10000, tdur = D)
    return(p$P(D))
}

We can solve this by trial and error, say for a 15-year mortage:

left.at.D(M = 100, D = 15 * 12)  # paid too much
## [1] -5638
left.at.D(M = 50, D = 15 * 12)  # not enough!
## [1] 7766
left.at.D(M = 75, D = 15 * 12)
## [1] 1064
left.at.D(M = 85, D = 15 * 12)
## [1] -1617
left.at.D(M = 80, D = 15 * 12)
## [1] -276.4

Or, automatically:

res = uniroot(left.at.D, lower = 50, upper = 100, D = 15 * 12)
res
## $root
## [1] 78.97
## 
## $f.root
## [1] 1.249e-10
## 
## $iter
## [1] 2
## 
## $estim.prec
## [1] 6.104e-05
## 

If you pay 78.969 per month for 15 years, you'll pay off the $10,000 principle, given an interest rate of 5 percent per year.

WARNING: The left.at.D function isn't working within findZeros. Figure out why.