Find roots of a function a x^n + bx - c = 0 where n isn't an integer with Numpy?
I think your beast choice is scipy.optimize.brentq()
:
def f(x, n, a, b, c): return a * x**n + b * x - cprint scipy.optimize.brentq( f, 0.0, 100.0, args=(77.0/27.0, 1.0, 1.0, 10.0))
prints
2.0672035922580592
I would use fsolve
from scipy,
from scipy.optimize import fsolvedef func(x,a,b,c,n): return a*x**n + b*x - ca,b,c = 11.,23.,31.n = 77./27.guess = [4.0,] print fsolve(func,guess,args=(a,b,c,n)) # 0.94312258329
This of course gives you a root, not necessarily all roots.
Edit: Use brentq
, it's much faster
from timeit import timeitsp = """from scipy.optimize import fsolvefrom scipy.optimize import brentqfrom numpy.random import uniformfrom numpy import zerosm = 10**3z = zeros((m,4))z[:,:3] = uniform(1,50,size=(m,3))z[:,3] = uniform(1,10,m)def func(x,a,b,c,n): return a*x**n + b*x - c"""s = "[fsolve(func,1.0,args=tuple(i)) for i in z]"t = "[brentq(func,0.,10.,args=tuple(i)) for i in z]"runs = 10**2print 'fsolve\t', timeit(s,sp,number=runs)print 'brentq\t', timeit(t,sp,number=runs)
gives me,
fsolve 15.5562820435brentq 3.84963393211