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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With