python - Find a root of a function in a given range -
i have set of functions f_t
several roots (two actually). want find "first" root , doing fsolve
works fine of time. problem is, 2 roots converge, t goes infinity. (a simple exmple of functions f_t(x) = x^2 - 1/t
). larger t
gets, more mistakes fsolve
makes. there predefined function, similar fsolve
can tell should in given range (e.g. find root in [0, inf
)).
the question same https://mathematica.stackexchange.com/questions/91784/how-to-find-numerically-all-roots-of-a-function-in-a-given-range?noredirect=1&lq=1, answers there mathematica, want them in python.
ps: how can write own algorithm, these tend slower builtins hoping find builtin same. i've read post find root of function in given interval
it accepted smooth, well-behaved functions, brent method fastest method guaranteed give root. other 2 methods listed, must provide interval [a,b] across function continuous , changes sign.
the scipy implementation documented here. example use case function mentioned this:
from __future__ import division import scipy def func(x,t): return(x**2 - 1/t) t0 = 1 min = 0 max = 100000 # set max sufficiently large value root = scipy.optimize.brentq(func, min, max, args = (t0)) # args supplies # argument function isn't varied parameter
Comments
Post a Comment