dus*_*tin 6 python numpy scipy
import numpy as np
from scipy.optimize import fsolve
musun = 132712000000
T = 365.25 * 86400 * 2 / 3
e = 581.2392124070273
def f(x):
return ((T * musun ** 2 / (2 * np.pi)) ** (1 / 3) * np.sqrt(1 - x ** 2)
- np.sqrt(.5 * musun ** 2 / e * (1 - x ** 2)))
x = fsolve(f, 0.01)
f(x)
print x
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?它似乎行不通.
HYR*_*YRY 10
因为sqrt为nagative参数返回NaN,所以函数f(x)不能计算所有实数x.我将你的函数改为使用numpy.emath.sqrt(),当参数<0时,它可以输出复数值,并返回表达式的绝对值.
import numpy as np
from scipy.optimize import fsolve
sqrt = np.emath.sqrt
musun = 132712000000
T = 365.25 * 86400 * 2 / 3
e = 581.2392124070273
def f(x):
return np.abs((T * musun ** 2 / (2 * np.pi)) ** (1 / 3) * sqrt(1 - x ** 2)
- sqrt(.5 * musun ** 2 / e * (1 - x ** 2)))
x = fsolve(f, 0.01)
x, f(x)
Run Code Online (Sandbox Code Playgroud)
然后你就可以得到正确的结果:
(array([ 1.]), array([ 121341.22302275]))
Run Code Online (Sandbox Code Playgroud)
解决方案非常接近真正的根,但f(x)仍然非常大,因为f(x)有一个非常大的因素:musun.