查找函数的根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?

查看:73
本文介绍了查找函数的根a x ^ n + bx-c = 0,其中n不是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 

其中,ab是在程序中较早计算的常数,但有数千个. 我需要对ab的所有值重复此方程两次,一次使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆