Browsing links on Brent’s Method I found:
Daft Engineer – Numerical Methods and Excel*
which is well worth a look (plenty of stuff on statistics and Python, with the odd dash of Excel). The link has VBA code for a class based Excel user defined function implementing Brent’s Method to find the roots of non-linear equations. Time permitting, I’ll have a look and compare with my effort
* From a quick survey of the blog, I ‘m not convinced of the daftness of the author, but he should know I suppose.
Not sure if you ever got round to comparing the algorithms, but the code you linked to has an error which I’ve discovered when comparing the answers and rate of convergence for the two modules.
I was expecting them to be similar, but your implementation converges really quickly, whereas the daft engineers code took an order of magnitude more iterations to solve for the same level of accuracy.
Looking into it further the error lies in the checking of the ‘condition 1’ from the Wikipedia algorithm.
they had for reference for checking ‘Condition 1’:-
f ((s >= (3 * a + b) / 4 And s <= b) Or (s = b)) Or _
versus what I believe it should be for checking ‘Condition 1’:-
f ((s >= (3 * a + b) / 4 And s >= b) Or (s <= (3 * a + b) / 4 And s <= b)) Or _
Note the sign change on the s & b inequalities after the 'And'.
Make this change and the code is then essentially the same with a slightly different structure in a few locations (didn't check if any of that impacts on speed, but I doubt it).
Hopefully this is of use to anyone that follows and is looking to find out more about correctly implementing Brents Method.
Thanks Craig. I’ll have a closer look when I have time.
You might also like a look at:
comment system ate some of the first condition! hopefully you can edit it!