查找函数的根a x ^ n + bx-c = 0,其中n不是Numpy的整数吗? [英] Find roots of a function a x^n + bx - c = 0 where n isn't an integer with Numpy?
问题描述
我正在用python编写程序,在其中我需要找到一个函数的根:
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
其中,a
和b
是在程序中较早计算的常数,但有数千个.
我需要对a
和b
的所有值重复此方程两次,一次使用n = 77/27
,一次使用n = 3
.
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
.
我如何在python中做到这一点?
我检查了numpy.roots(p)
,这在我认为n = 3
时适用.但是对于n = 77/27
,我该怎么做呢?
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))
打印
2.0672035922580592
这篇关于查找函数的根a x ^ n + bx-c = 0,其中n不是Numpy的整数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!