Skip to main content
 首页 » 编程设计

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

2025年05月04日10lonelyxmas

我正在尝试用 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.sin 与 mpmath 中的任意精度版本的 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() 

输出:

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

输出:

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