The Easy Way To Solve Equations In Python
thelivingpearl.com
thelivingpearl.com
Except for the bisection method, all of these implementations take an argument specifying the number of iterations to run. In most cases, the only way to terminate in fewer iterations is by hitting an "exact" root, i.e., calculating the residual as exactly zero. This is poor practice for a number of reasons. First, in practice it's pretty rare for a method to find an exact zero. Second, once a method has converged to the numerical precision of the machine, making more iterations just wastes flops. So a much better approach is to specify a solution tolerance (as shown with the bisection method). Even better is to provide absolute and relative tolerances, and to choose those values based on either the domain requirements, or on the machine characteristics. Dennis & Schnabel's excellent "Numerical Methods for Unconstrained Optimization and Nonlinear Equations" has a good discussion on choosing convergence tolerances.
This dependence on iteration counts to terminate, by the way, is probably why the author equates low iteration counts with greater accuracy. But in fact these methods don't vary in their intrinsic accuracy, rather, they vary in their order of convergence.
Another example of poor practice is in the bisection method implementation. One generally should not bisect an interval using c = (a+b)/2, because the nature of finite-precision arithmetic means there is no guarantee that c will lie between a and b, even if the machine can represent numbers between a and b. A better approach is to ensure a < b, then to set c = a + (b-a)/2. This expression is much less subject to roundoff errors.
Sure. I've experienced this myself, for example, when doing the research cited here: http://pubs.acs.org/doi/abs/10.1021/es202359j Unfortunately, my Lab doesn't have our report version of the work up on the web yet (I believe it should be freely available at some point, but they're re-doing the library).
In addition, I knew about this particular effect before I started that work, so I must have run into it earlier.
In most cases, naive bisection works fine. That's especially true if you use pretty loose tolerances-- for example, you if only want to identify the zero to a relative tolerance of 1e-6 or something coarse like that. But this work demanded very tight tolerances (because the output gets used in a time-marching numerical simulation that may iterate on time steps, and hence has to be very repeatable no matter what the initial condition). In this work, the relative tolerance was about 2e-15, and the absolute tolerance about 1e-18 (on a machine with double-precision epsilon about 2e-16).
By the way, if you're testing for convergence according to a bracket on the value, then you want to know (b-a) anyhow. So there's no reason not to go ahead and use it to bisect the interval as well.
Another interesting implementation detail-- at very tight relative tolerances, you also need to calculate the terminating bracket width based on the larger of |b| and |a|-- due to floating point effects, if you find the terminating bracket width by applying the relative tolerance to the smaller value, you can occasionally run into a problem where adding that width to one of a or b fails to change the estimated value.
f = 'x**3+x-1'
x = 4
eval(f)
It should work fine.Then I noticed that there was no mention at all of the various ways of using the R language with Python (RPy, etc.).
The easy way to solve equations in Python (or any other language for that matter) would be to take full advantage of the work that other bright people have done.
Not necessarily.
> Isn't Python 2.7 the version usually used
Yes. But because the 2.x series is backward compatible, you will be able to run 2.6 code on 2.7. Honestly, if he was writing on 2.7 the code probably would look identical. For that matter, it would probably look identical on Python 3, except for print statements.
> using the R language
I'm not familiar with R, I always thought it was more for statistics like curve fitting or ANOVA. (I actually am not really sure what ANOVA is, but stats people seem to like that word, so I'll toss it out there.) Sage [1] would be the tool I'd turn to for symbolic equation solving.
http://www.overclockers.com.au/wiki/Numerical_Analysis
I offer no Good Science or Good Writing warranty on it ;)