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

标签 python numpy precision trigonometry 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)

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

编辑:我试着让 error_sin 和 increment 都等于我设备的机器 epsilon 但它 a) 需要很多时间,b) sin(x) 不如 x 精确,所以我得到了很多非-解决方案(或者更确切地说是重复解决方案,因为 sin(x) 的增长比 x 慢得多)。因此问题。

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

最佳答案

答案

np.sin 通常会尽可能精确,给定 double(即 64 位 float)变量的精度其中存储了输入、输出和中间值。通过将 np.sinmpmath 中的任意精度版本的 sin 进行比较,您可以合理地衡量其精度:

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()

输出:

enter image description here

因此,可以说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()

输出:

enter image description here

您必须根据a 的值调整initial_guessinitial_guess 必须至少与实际解数一样大。

关于python - numpy 的 sin(x) 有多精确?我怎么知道? [需要它来数值求解 x=a*sin(x)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54207382/

相关文章:

python - Multiprocessing pool.map 不会从函数调用中返回

python - python map reduce与云计算map/reduce的关系?

Python dash 给了我 "Error loading layout"我该如何解决这个问题?

python - Numpy 矩阵乘法,而不是乘以 XOR 的元素

numpy - `out` 中的 `numpy.einsum` 参数无法按预期工作

python - 是否可以通过 South 维护全局(跨所有应用程序)迁移历史记录?

python - 为什么 numpy.mean 是无限的而值是有限的?

c++ - 在 float 和 double 之间选择

language-agnostic - 机器 epsilon 的使用是否适合浮点相等性测试?

math - float 学坏了吗?