繁体   English   中英

F解决精度问题(python)

[英]Fsolve precision issue (python)

我遇到了 fsolve 的精度问题。

import numpy as np
from scipy.optimize import fsolve


ens=np.arange(0,50,1)


def f(x):
     return x*(np.sin(x)/np.cos(x))-1000

s=[]        

roots=fsolve(f,ens)
roots=np.around(roots, decimals=3 , out=None)
a = roots[roots >= 0]
g = np.unique(a)
g=g[:5]
s.append(g)
print(s)
            

result :

[array([10.842, 11.006, 15.165, 21.116, 22.382])]

结果应该是:[ 1.569,4.708,7.846 ,10.985,14.123]

我的代码错过了前三个解决方案,其他的都不准确。 您知道如何提高结果的精度吗?

您可以使用scipy.optimize.newton()与 function f的一阶和二阶导数来获得更准确的结果。 您可以手动完成,或使用导数计算器.net 或wolframalpha 将一阶导数fprime和二阶导数fprime2newton() 如果您这样做,将使用Halley 方法而不是更准确的简单Newton-Raphson方法(请参阅newton()文档中fprime2下面的描述)。

def f(x):
     return x*(np.sin(x)/np.cos(x))-1000

def fprime(x):
    # first derivative of f
    # do this by hand or use derivative-calculator.net or wolframalpha
    return np.tan(x)+ x*(1/np.cos(x))**2

def fprime2(x):
    # second derivative of f
    # do this by hand or use derivative-calculator.net or wolframalpha
    return 2*1/(np.cos(x)**2)*(x*np.tan(x)+1)

res = newton(f,x0=[1,4,7,10,14],fprime=fprime, fprime2=fprime2)
res = np.around(res, decimals=3)

res将是:

array([ 1.569,  4.708,  7.846, 10.985, 14.123])

上面newton()中的x0参数是所谓的根在哪里的初始猜测列表。 由于您的 function 有无限多的根(请参阅下面的 plot),因此传递其中一些有助于获得您真正关心的根。

这就是f的样子(嗯, f/1000使特征可见):

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM