Find out if there is a zero on an interval...and then use that knowledge
So, I'm using find_root to figure out if there is a zero on a particular interval for a function. But this is part of a larger program and I want to be able to do something different if there is a root versus if there isn't. The idea is that this has to automate for all manner of functions!
For instance:
f = sin(x)
if find_root(sin(x),1,2) not in RR:
print(no root)
else:
print(root)
Now, of course, this doesn't work because find_root just kicks out an error when there isn't a root. Any ideas what I can do instead?
You may catch this exception and treat it accordingly. see the relevant chapter of the Python tutorial.
But beware :
find_root
will return one root even if many exist in your interval. Consider :or, worse :
(the point of the latter example being that there is an infinity of solutions in this interval...).
Furthermore,
find_root
is a numerical solver, whose answers may not learn you puch about the structure of the solution, which is often the point...HTH,
Nice - this is exactly what I need. Fortunately, my whole program is built on approximation by it's very nature, so the rounding is not an issue.
I'm not so sure : meditate :