Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Root-finding (of a function) algorithms in Python

I would like to compare different methods of finding roots of functions in python (like Newton's methods or other simple calc based methods). I don't think I will have too much trouble writing the algorithms

What would be a good way to make the actual comparison? I read up a little bit about Big-O. Would this be the way to go?

like image 517
Meo Avatar asked Dec 13 '11 02:12

Meo


People also ask

What is the best root-finding algorithm?

on the value of the root may produce a value of the polynomial at the approximate root that is of the order of. For avoiding these problems, methods have been elaborated, which compute all roots simultaneously, to any desired accuracy. Presently the most efficient method is Aberth method.

What is the fastest root-finding method?

The fastest root-finding method we have included is Newton's method, which uses the derivative at a point on the curve to calculate the next point on the way to the root. Accuracy with this method increases as the square of the number of iterations.

How do I find roots in Python?

sqrt() function is an inbuilt function in Python programming language that returns the square root of any number. Syntax: math.sqrt(x) Parameter: x is any number such that x>=0 Returns: It returns the square root of the number passed in the parameter.

How do you find the root using the bisection method in Python?

The bisection method uses the intermediate value theorem iteratively to find roots. Let f(x) be a continuous function, and a and b be real scalar values such that a<b. Assume, without loss of generality, that f(a)>0 and f(b)<0. Then by the intermediate value theorem, there must be a root on the open interval (a,b).


4 Answers

The answer from @sarnold is right -- it doesn't make sense to do a Big-Oh analysis.

The principal differences between root finding algorithms are:

  • rate of convergence (number of iterations)
  • computational effort per iteration
  • what is required as input (i.e. do you need to know the first derivative, do you need to set lo/hi limits for bisection, etc.)
  • what functions it works well on (i.e. works fine on polynomials but fails on functions with poles)
  • what assumptions does it make about the function (i.e. a continuous first derivative or being analytic, etc)
  • how simple the method is to implement

I think you will find that each of the methods has some good qualities, some bad qualities, and a set of situations where it is the most appropriate choice.

like image 95
Raymond Hettinger Avatar answered Oct 21 '22 19:10

Raymond Hettinger


Big O notation is ideal for expressing the asymptotic behavior of algorithms as the inputs to the algorithms "increase". This is probably not a great measure for root finding algorithms.

Instead, I would think the number of iterations required to bring the actual error below some epsilon ε would be a better measure. Another measure would be the number of iterations required to bring the difference between successive iterations below some epsilon ε. (The difference between successive iterations is probably a better choice if you don't have exact root values at hand for your inputs. You would use a criteria such as successive differences to know when to terminate your root finders in practice, so you could or should use them here, too.)

While you can characterize the number of iterations required for different algorithms by the ratios between them (one algorithm may take roughly ten times more iterations to reach the same precision as another), there often isn't "growth" in the iterations as inputs change.

Of course, if your algorithms take more iterations with "larger" inputs, then Big O notation makes sense.

like image 31
sarnold Avatar answered Oct 21 '22 18:10

sarnold


Big-O notation is designed to describe how an alogorithm behaves in the limit, as n goes to infinity. This is a much easier thing to work with in a theoretical study than in a practical experiment. I would pick things to study that you can easily measure that and that people care about, such as accuracy and computer resources (time/memory) consumed.

When you write and run a computer program to compare two algorithms, you are performing a scientific experiment, just like somebody who measures the speed of light, or somebody who compares the death rates of smokers and non-smokers, and many of the same factors apply.

Try and choose an example problem or problems to solve that is representative, or at least interesting to you, because your results may not generalise to sitations you have not actually tested. You may be able to increase the range of situations to which your results reply if you sample at random from a large set of possible problems and find that all your random samples behave in much the same way, or at least follow much the same trend. You can have unexpected results even when the theoretical studies show that there should be a nice n log n trend, because theoretical studies rarely account for suddenly running out of cache, or out of memory, or usually even for things like integer overflow.

Be alert for sources of error, and try to minimise them, or have them apply to the same extent to all the things you are comparing. Of course you want to use exactly the same input data for all of the algorithms you are testing. Make multiple runs of each algorithm, and check to see how variable things are - perhaps a few runs are slower because the computer was doing something else at a time. Be aware that caching may make later runs of an algorithm faster, especially if you run them immediately after each other. Which time you want depends on what you decide you are measuring. If you have a lot of I/O to do remember that modern operating systems and computer cache huge amounts of disk I/O in memory. I once ended up powering the computer off and on again after every run, as the only way I could find to be sure that the device I/O cache was flushed.

like image 40
mcdowella Avatar answered Oct 21 '22 18:10

mcdowella


You can get wildly different answers for the same problem just by changing starting points. Pick an initial guess that's close to the root and Newton's method will give you a result that converges quadratically. Choose another in a different part of the problem space and the root finder will diverge wildly.

What does this say about the algorithm? Good or bad?

like image 27
duffymo Avatar answered Oct 21 '22 19:10

duffymo