I am developing a UDF which requires some looping to arrive at the result. To keep it simple, part of the routine is akin to solving the following Amount equation for i.
Find i such that A=1.72846 given A=(1+i)^10
The following code resolves to the correct answer of i = 5.6248%
Sub nn()
Time1 = Timer
i = 0.01
k = 1
Do
A = (1 + i) ^ 10
i = i + 0.0000001
k = k + 1
'Debug.Print k & "........" & i
Loop Until Application.Round(A, 4) = 1.7285
MsgBox i & " ... " & Timer - Time1
End Sub
But it does so using 462,477 iterations and taking a whooping 90 seconds! (One would expect under 100 iterations to be ideal).
While Solver can easily tackle this problem, the Solver tool cannot be deployed in my exercise. Solver requires a WorkSheet interface and is hence inappropriate for incorporation in the UDF.
Can someone kindly come up with a non-linear algorithm that will speed up the rate of convergence? I would be happy with a solution that takes under 5 seconds.
Bookmarks