The formula for an annuity of periodic payments of equal amount is:
A = P * r * c / (c - 1)
where:
P = Principal
R = Annual interest rate (%)
r = Rate per payment period (decimal form) = R / number of payments per year
n = Total number of payments in loan
c = (1 + r) ^ n
This gets you the payment amount when all payment periods, including the first, are equal in length.
For good business reasons, the first payment period is often set for a longer or shorter period than the standard payment period. American lenders commonly use the term "odd days" to describe this practice. Longer payment periods are said to have "long odd days", while periods that fall short of the standard are called "short odd days." When a loan has odd days, an interest adjustment is in order. This adjustment can be handled at the loan's inception, or spread across all the payments.
Based on your posts, it appears your system is trying to spread the additional interest created by long odd days. If I were designing such a system, I would perform the following steps before solving the formula above:
1. Compute a daily interest rate = annual interest rate (%) / 36500
2. Calculate the number of long odd days. Lenders have various ways to do this, but my preference is to first calculate the date that is one regular period earlier than the date of the first scheduled payment. The number of long odd days will then equal the elapsed time from the note date until the date just calculated.
3. Calculate odd-day interest = number of long odd days * daily interest rate * principal
4. Adjust the "Principal" amount (for calculation purposes, only) by adding the odd-day interest to the actual "Principal" amount.
5. Solve the formula above.
_________________________
...gone fishing.