I want to implement the secant method in order to find the internal rate of return of an investment.
If I run this code it always returns the initial value of x1, in this case 10%. But if I run this code in any other programming language such as R or Python, it works and returns the correct internal rate of return.
The function f is derived from the geometric sum with monthly investments and implemented as follows:
VBA Code:
Public Function irr(payment As Double, endValue As Double, T As Integer) As Double
Dim x0 As Double
Dim x1 As Double
Dim f0 As Double
Dim f1 As Double
Dim temp As Double
Dim i As Integer
'We will use the Secant method'
x0 = -0.03
x1 = 0.1
f0 = f(x0, endValue, payment, T)
f1 = f(x1, endValue, payment, T)
For i = 1 To 100
If f1 = f0 Then
Exit For
End If
temp = x1
x1 = x1 - f1 * (x1 - x0) / (f1 - f0)
x0 = temp
f0 = f1
f1 = f(x1, endValue, payment, T)
If Abs(f1)<0.00001 Then
Exit For
End If
Next i
irr = r1
End Function
If I run this code it always returns the initial value of x1, in this case 10%. But if I run this code in any other programming language such as R or Python, it works and returns the correct internal rate of return.
The function f is derived from the geometric sum with monthly investments and implemented as follows:
VBA Code:
Private Function f(r, value, prem, T) As Double
Dim y As Double
y = prem * ((1 + r) ^ (T + 1 / 12) - 1) / ((1 + r) ^ (1 / 12) - 1) - value
End Function