Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find roots of a function a x^n + bx - c = 0 where n isn't an integer with Numpy?

I'm writing a program in python and in it I need to find the roots of a function that is:

a*x^n + b*x -c = 0 

where a and b are constants that are calculated earlier in the program but there are several thousand of them. I need to repeat this equation twice for all values of a and b once with n = 77/27 and once with n = 3.

How can i do this in python? I checked numpy.roots(p) and that would work for when n = 3 I think. But for n = 77/27 how would I be able to do that?

like image 531
sbeleidy Avatar asked Jun 29 '11 10:06

sbeleidy


1 Answers

I think your beast choice is scipy.optimize.brentq():

def f(x, n, a, b, c):
    return a * x**n + b * x - c

print scipy.optimize.brentq(
    f, 0.0, 100.0, args=(77.0/27.0, 1.0, 1.0, 10.0))

prints

2.0672035922580592
like image 143
Sven Marnach Avatar answered Oct 06 '22 00:10

Sven Marnach