why does this loan calculation have no direct equation? The problem
Find the monthly payment at which a given "simple" loan would be paid off in $n$ months.  I was told there is no direct equation for this.  I would like to understand why.
Inputs

*

*$P$: the loan's starting principal balance

*$I$: the loan's starting interest balance

*$r$: the monthly interest rate

*$n$: the target number of months by which to pay off the loan (i.e. both the principal balance and the interest balance are reduced to zero)

Output

*

*$p$: the fixed monthly payment made on the loan

Rules

*

*Interest is calculated at the beginning of the month, on the remaining principal balance only.  This newly accrued interest is immediately added to the running interest balance, $I$.

*The fixed monthly payment is applied at the end of the month, always first towards the remaining interest balance, then towards the remaining principal balance.

Reduced case, for study
Consider a loan that has no starting interest balance.  I found this known equation.  I then solved for $p$.
$$p = \frac{P \cdot r}{1 - 10^{-N \cdot log (1 + r)}}$$
Some test values, everything checks out.
P=1000, I=0, r=0.01, n=1 --> p = 1010
P=1000, I=0, r=0.01, n=2 --> p = 507.51
P=1000, I=0, r=0.01, n=3 --> p = 340.02
P=1000, I=0, r=0.01, n=4 --> p = 256.28

Common case
Now consider a loan with both starting principal and interest balances.
Let $I_i$ be the interest balance at the start of month $i$.  I worked out this equation:
$$I_i = I + (i - 1)(P \cdot r - p)$$
My stubborn intuition is that you should be able join the above two formulas together somehow?
I think of the life of the loan as having three phases.

*

*Phase one - the monthly payment only goes towards interest.  Here, the phase begins with a principal balance of $P$ and an interest balance of $I$.  This phase will last $n_0$ months, which I worked out as being

$$n_0 = \lfloor{\frac{I_1}{p - P \cdot r} + 1}\rfloor$$


*Phase two - the crossover or transition month.  This phase is exactly one month long (I'm ignoring the edge case where this month may not even happen).  For this month, part of the monthly payment is used to zero out the interest balance.  The remainder of the payment is used to start whittling down the principal balance.  Here, this phase begins with a principal balance of $P$ and an interest balance of

$$I_{n_o} = I_1 + (n_0 - 1)(P \cdot r - p)$$


*Phase three - the monthly payment only goes towards principal.  This is the same as our above reduced case, for which there is a known formula for $p$.  Here, this phase begins with an interest balance of zero, and a principal balance I'm calling $P'$.  This phase will last $n_1$ months.  I worked out these equations:

$$P' = P(1 + r) - p + I_{n_o}$$
$$n_1 = N - n_0 - 1$$
The impasse
Given the above formulas, I tried combining the three phases together to solve for $p$.  But I soon became stuck, and ended up with $p$ inside a floor function.  Is this the specific mathematical reason why there's no direct equation here?  Because it's impossible to isolate a variable that's inside a floor function?
Again, my only goal below is to get $p$ all alone on the left side of the equation.  I defined several constants below, none of which depend on $p$.
$$p = c_3 + c_4 \cdot \frac{\lfloor{\frac{I_1}{p - P \cdot r} + 1}\rfloor}{1 + c_1} - c_5 \cdot \frac{\lfloor{\frac{I_1}{p - P \cdot r} + 1}\rfloor \cdot P}{1 + c_1} + c_5 \cdot \frac{P}{1 + c_1}$$
where
$$c_0 = 1 - 10^{-N \cdot log (1 + r)}$$
$$c_1 = \frac{r}{c_o}$$
$$c_2 = \frac{P \cdot r \cdot (1 + r)}{c_0}$$
$$c_3 = \frac{c_2 + c_1 \cdot I_1 - c_1 \cdot P \cdot r}{1 + c_1}$$
$$c_4 = \frac{c_1 \cdot P \cdot r}{1 + c_1}$$
$$c_5 = \frac{c_1}{1 + c_1}$$
 A: First of all, it is important to understand exactly where the initial formula (based on a zero interest balance) came from.
$$p = \frac{P \cdot r}{1 - 10^{-N \cdot log (1 + r)}}$$
Let $s = (1 + r),~$  and assume that $log$ is referring to logarithms base 10.  Then:
$$10^{-n \cdot log (s)} ~=~ \frac{1}{(s)^n}.$$
where it is assumed that $n$ payments will be made.
Therefore, the formula is asserting that
$$p \left[1 - \frac{1}{(s)^n}\right] ~=~ Pr.$$

You can reasonably assume that since the payment is always greater than the running interest balance, that (for simplicity) the running interest balance can be lumped in with the loan balance.

I highlighted the above sentence to emphasize that in your first scenario, it is true.  However, in your second scenario, where the beginning interest balance is not zero, things will get more complicated.
Anyway, staying within the first scenario, let $P_k$ represent what is happening at the end of the $k$-th month, under the assumption that the payment is always received before the end of the month, and then applied at the end of the month.
$P_1 ~: ~Ps - p.$ 
$P_2 ~: ~(Ps - p)s - p = Ps^2 - ps - p.$ 
$P_3 ~: ~(Ps^2 - ps - p)s - p = Ps^3 - ps^2 - ps - p.$ 
$\cdots$ 
$P_n ~: ~Ps^n - p\left[s^{(n-1)} + s^{(n-2)} + \cdots s^2 - s - 1\right].$
Note that:
$$\left[s^{(n-1)} + s^{(n-2)} + \cdots s^2 - s - 1\right]  ~=~ \frac{s^n - 1}{s - 1}.$$
This means that $$Ps^n ~=~ p\left[\frac{s^n - 1}{s - 1}\right].$$
Noting that $~r = (s-1),~$ this means that
$$Pr ~=~ p\left[\frac{s^n - 1}{s^n}\right] ~=~ p \left[1 - \frac{1}{(s)^n}\right].$$

Assuming that you understand the source of the formula, a key point must now be addressed.  Consider the following rule, which is (presumably) intended to apply to loans with a beginning interest balance of zero, where the fixed monthly payment is greater than the incurred monthly interest charge:

Interest is calculated at the beginning of the month, on the remaining principal balance only.

Banks are in business to make money.  The bank accepts the above rule because (in effect) the running interest balance is always zero.
This means that to consider what happens when the initial interest balance is not zero, you have to consider two separate cases:

*

*The bank will allow the interest rate to only be applied to the loan balance, regardless of whether the interest balance is itself significant.


*The bank will insist that the interest rate also be applied to the
(significant) interest balance.
Although I consider the second case above more realistic, I will go ahead and attack each case separately, starting with the first case.
Note that your work assumes that the second case pertains.  The problem with your work in the second case is that you made things overly complicated.  Anyway, I am going to attack each case from scratch, starting with the first case:
$\underline{\text{Case 1: the interest rate is applied only to the loan balance}}$
First of all - pretend that you have a zero interest balance, and use that to calculate $p$ under the assumption that $n_1$ monthly payments will be made (after the interest balance is reduced to zero).
Let $$n_0 = \left\lfloor \frac{I}{p} \right\rfloor$$
This means that
$$n_0~\leq ~\left(\frac{I}{p}\right) ~< (n_0 + 1).$$
Under this scenario, after the payment is received at the end of  month $n_0$, the interest balance will have been reduced so that it is now $~< p$.  The easiest way to handle this scenario is to include a second payment (in this month only) that is sufficient to bring the interest balance to zero.
This will keep the math simple and imply that your payments are all constant except in month $~n_0,~$ where you are making a second (interest) payment.  This means that except for this single occurrence of a second payment, you will end up making $(n_0 + n_1)$ payments of $p$.
If you are lucky enough to find a mentally retarded bank loan officer, then you can pursue Case 1.
$\underline{\text{Case 2: the interest rate is also applied to the interest balance}}$
I studied your work.  As you discovered, you are chasing your tail as follows:
If you calculate $n_0$ and $n_1$ via the algorithm in Case 1, you will find that at the end of $(n_0 + n_1)$ payments, you haven't paid off the loan because the beginning interest balance was itself accruing interest.
You can't perform the computation of $n_0 = \left\lfloor\frac{I}{p}\right\rfloor$ without knowing what $p$ is.  And you can't determine what $p$ is without knowing what $(n_0 + n_1)$ is.
You need a way out of this mess that the bank will accept.  All the bank cares about is money.
Simply construe $L_1 = L + I,$ where $L$ is the initial (or outstanding) loan balance, $I$ is the current interest balance (if any), and $L_1$ is the new loan balance.  Then calculate the payment $p$ assuming that the loan is $L_1$ and the number of months of the loan, from this point forward, is to be $n$.
From the bank's perspective this is a realistic and acceptable construance because:

*

*Interest will be charged against $L_1$.


*Assuming that $p$ is caculated on the basis of $n$ months, $p$ will (as in your initial scenario) be greater than the monthly interest charges.
This is because with interest being charged against $L_1,$ the new interest balance $I_1$ will start at zero.  Therefore, as before, the running interest balance will always be zero.
