Suppose you purchase a house for $500,000 with a loan that has a nominal interest rate of 6.5% for 30 years.

- Find your yearly mortgage payment amount.
- If you can increase each yearly payment by 5%, how long will it take to pay off the loan?

First load sympy and create the variables $t$, $P$, and the function $Q(t)$.

In [66]:

```
from sympy import *
t, P = symbols('t P')
Q = Function('Q')
```

The DE to find the yearly payment is
$$\frac{dQ}{dt}=0.065Q-P$$

where $P$ is the yearly payment. The initial value is $Q(0)=\$500000$ and we also have $Q(30)=0$.

Solve for $Q(t)$ in terms of $t$ and $P$.

In [55]:

```
sol1 = dsolve(diff(Q(t),t)-0.065*Q(t)+P, ics = {Q(0): 500000});sol1
```

Out[55]:

$\displaystyle Q{\left(t \right)} = 15.3846153846154 P + \left(500000.0 - 15.3846153846154 P\right) e^{0.065 t}$

We can solve for the yearly payment by substituting $t=30$ and $Q=0$, or $Q(30)=0$:

In [37]:

```
Pmt=solve((sol1.rhs).subs(t,30),P)[0];Pmt
```

Out[37]:

$\displaystyle 37890.8913947774$

Suppose we can increase the yearly payment by 5% each year.

Solve for the yearly payment with increases of 5% each year. (Pv is the varying payment.) That is solve:

$$\frac{dP}{dt}=0.05\cdot P$$where $P_0 = \text{Pmt}$.

In [69]:

```
Pv = Function('Pv')
Pmt2 = dsolve(diff(Pv(t),t)-0.05*Pv(t), ics = {Pv(0):Pmt}); Pmt2
```

Out[69]:

$\displaystyle \operatorname{Pv}{\left(t \right)} = 37890.8913947774 e^{0.05 t}$

Solve the original DE using this varying payment in place of the fixed yearly payment.

In [70]:

```
sol2 = dsolve(diff(Q(t),t)-0.065*Q(t)+Pmt2.rhs, ics = {Q(0): 500000});sol2
```

Out[70]:

$\displaystyle Q{\left(t \right)} = 2526059.42631849 e^{0.05 t} - 2026059.42631849 e^{0.065 t}$

Finally, solve this equation for $t$ to find how long it will take to pay off the mortgage.

In [71]:

```
time = solve(sol2.rhs,t)[0];time
```

Out[71]:

$\displaystyle 14.7045208178974$

In [64]:

```
plot((sol1.rhs).subs(P,Pmt),sol2.rhs,(t,0,30),ylim=(0,500000))
```

Out[64]:

<sympy.plotting.plot.Plot at 0x7fa6a23699a0>