x^(n+1) + a * x^n + b*x + c = 0
' Equivalent to the worksheet function RATE(nper,pmt,pv,fv,type,guess)
Function MyRATE(nper As Integer, pmt As Double, pv As Double, Optional fv As Double = 0, _
Optional PaymentEnd As Integer = 0, Optional guess As Double = 0.1)
Dim a As Double, b As Double, c As Double ' coefficients of the equation
Dim R As Double, RTmp As Double, i As Integer
' Initialize coefficients and R
R = 1 + guess
a = (pmt * (1 - PaymentEnd) - pv) / (pv + pmt * PaymentEnd)
b = (fv - pmt * PaymentEnd) / (pv + pmt * PaymentEnd)
c = (-pmt * (1 - PaymentEnd) - fv) / (pv + pmt * PaymentEnd)
' Iterate
For i = 1 To 20
RTmp = R - (R ^ (nper + 1) + a * R ^ nper + b * R + c) / ((nper + 1) * R ^ nper + a * nper * R ^ (nper - 1) + b)
If Abs(RTmp - R) < 0.0000001 Then Exit For
R = RTmp
Next i
If i <= 20 Then
MyRATE = RTmp - 1
Else
MyRATE = "N/A" ' Must try another guess
End If
End Function
Book1 | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Function:RATE(nper,pmt,pv,fv,type,guess) | ||||||
2 | |||||||
3 | |||||||
4 | |||||||
5 | nper | 7 | 7 | 7 | |||
6 | pmt | -2000 | -550 | -600 | |||
7 | pv | 3000 | 3000 | 3000 | |||
8 | fv | 0 | 0 | 0 | |||
9 | Type | ||||||
10 | Guess | 50% | 10% | 10% | |||
11 | |||||||
12 | Rate | 64.63% | 6.66% | 9.20% | |||
13 | Myrate | 64.63% | 6.66% | 9.20% | |||
14 | |||||||
15 | |||||||
16 | Totalpayment | -14000 | -3850 | -4200 | |||
17 | |||||||
Sheet2 |