numpy 的 sin(x) 有多精确?我怎么知道?[需要它来数值求解 x=a*sin(x)]

faz*_*zan 3 python precision trigonometry numpy numerical-methods

我正在尝试在 python 中数值求解方程x=a*sin(x),其中a是一些常数。我已经尝试先象征性地求解方程,但似乎这种特殊的表达形式并没有在 sympy 中实现。我也尝试使用 sympy.nsolve(),但它只给了我它遇到的第一个解决方案。

我的计划是这样的:

x=0
a=1
rje=[]
while(x<a):
    if (x-numpy.sin(x))<=error_sin:
        rje.append(x)
    x+=increment

print(rje)
Run Code Online (Sandbox Code Playgroud)

我不想浪费时间或冒丢失解决方案的风险,所以我想知道如何找出我的设备上 numpy 的窦性有多精确(这将成为 error_sin)。

编辑:我尝试使 error_sin 和 increment 都等于我设备的机器 epsilon,但它 a) 需要很长时间,并且 b) sin(x) 不如 x 精确,所以我得到了很多非解决方案(或相当重复的解,因为 sin(x) 的增长比 x 慢得多)。因此这个问题。

编辑2:你能帮我回答一下关于numpy.sin(x)精度的问题吗?我提供有关目的的信息纯粹是为了上下文。

tel*_*tel 5

答案

np.sin考虑到存储输入、输出和中间值的double(即 64 位float)变量的精度,通常会尽可能精确。您可以np.sin通过将 的精度与sinfrom的任意精度版本进行比较来获得精度的合理度量mpmath

import matplotlib.pyplot as plt
import mpmath
from mpmath import mp

# set mpmath to an extremely high precision
mp.dps = 100
x = np.linspace(-np.pi, np.pi, num=int(1e3))

# numpy sine values
y = np.sin(x)

# extremely high precision sine values
realy = np.array([mpmath.sin(a) for a in x])

# the end results are arrays of arbitrary precision mpf values (ie abserr.dtype=='O')
diff = realy - y
abserr = np.abs(diff)
relerr = np.abs(diff/realy)

plt.plot(x, abserr, lw=.5, label='Absolute error')
plt.plot(x, relerr, lw=.5, label='Relative error')
plt.axhline(2e-16, c='k', ls='--', lw=.5, label=r'$2 \cdot 10^{-16}$')
plt.yscale('log')
plt.xlim(-np.pi, np.pi)
plt.ylim(1e-20, 1e-15)
plt.xlabel('x')
plt.ylabel('Error in np.sin(x)')
plt.legend()
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明

因此,可以合理地说 的相对误差和绝对误差np.sin都有一个上限2e-16

更好的答案

如果你increment的方法足够小以保证你的方法准确,那么你的算法很有可能在实际使用中太慢。标准方程求解方法对您不起作用,因为您没有标准函数。相反,您有一个隐式的多值函数。这是获得此类方程的所有解的通用方法:

import matplotlib.pyplot as plt
import numpy as np
import scipy.optimize as spo

eps = 1e-4

def func(x, a):
    return a*np.sin(x) - x

def uniqueflt(arr):
    b = arr.copy()
    b.sort()
    d = np.append(True, np.diff(b))
    return b[d>eps]

initial_guess = np.arange(-9, 9) + eps
# uniqueflt removes any repeated roots
roots = uniqueflt(spo.fsolve(func, initial_guess, args=(10,)))
# roots is an array with the 7 unique roots of 10*np.sin(x) - x == 0:
#     array([-8.42320394e+00, -7.06817437e+00, -2.85234190e+00, -8.13413225e-09,
#            2.85234189e+00,  7.06817436e+00,  8.42320394e+00])

x = np.linspace(-20, 20, num=int(1e3))
plt.plot(x, x, label=r'$y = x$')
plt.plot(x, 10*np.sin(x), label=r'$y = 10 \cdot sin(x)$')
plt.plot(roots, 10*np.sin(roots), '.', c='k', ms=7, label='Solutions')

plt.ylim(-10.5, 20)
plt.gca().set_aspect('equal', adjustable='box')
plt.legend()
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明

您必须initial_guess根据a. initial_guess必须至少与解决方案的实际数量一样大。