Here's a function that calculates the periodic payment for a loan, given the loan amount, interest rate, the number of payments, and the loan future value:
def pmt(rate, nper, pv, fv=0)
pmt = rate * (fv + pv * (1 + rate)**nper) / ((1 + rate)**nper - 1)
return -pmt
end
The rate parameter represents the interest rate per period, the nper parameter represents the total number of payment periods, the pv parameter represents the present value of the loan, and the fv parameter represents the future value of the loan at the end of the payment period.
For example, if you wanted to calculate the monthly payment for a $10,000 loan with a 5% interest rate over 3 years (36 months) with a future value of $5,000, you could call the function like this:
pmt(0.05/12, 36, 10000, 5000)
This would return the value of the monthly payment that you would need to make on the loan to pay off both the present value and the future value of the loan.