【问题标题】:How to find roots of very complex equation on Python?如何在 Python 上找到非常复杂的方程的根?
【发布时间】:2018-06-22 19:12:03
【问题描述】:

如何在 Python 上解决这个问题 (f(r)=0)?:

def f(r):
     return -6*r**2*(pi - 1/cos(2.4e-6*sqrt(3)/r)) - 3*r**2*(2*cos(pi/6 - 1/cos(2.4e-6*sqrt(3)/r))*cos(1/cos(2.4e-6*sqrt(3)/r)) - pi/2 + 1/cos(2.4e-6*sqrt(3)/r)) - (-2.88e-5*sqrt(3) + 0.000207846096908265)*cos(pi/6 - 1/cos(2.4e-6*sqrt(3)/r)) + 5.19615242270663e-10

【问题讨论】:

  • 我的答案应该适用于通用函数,但您的特定方程可能没有合适的根,因为您要除以 r

标签: python scipy sympy equation solver


【解决方案1】:

一种选择是使用scipy.optimize.root。你只需要传递一个初步的猜测。

from scipy.optimize import root
initial_guess = 1
solution = root(fun=f, x0=initial_guess)

print(solution)
#    fjac: array([[-1.]])
#     fun: array([-4.73292067e-21])
# message: 'The solution converged.'
#    nfev: 50
#     qtf: array([7.59795519e-16])
#       r: array([-99.77979489])
#  status: 1
# success: True
#       x: array([1.46358719e-06])

作为检查,我们可以将解决方案重新插入到您的函数中:

print(f(solution.x[0]))
#-4.732920669875286e-21

本质上是 0。

【讨论】:

    猜你喜欢
    • 2012-10-14
    • 2014-03-17
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    相关资源
    最近更新 更多