- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试用 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
必须至少与实际解数一样大。
关于python - numpy 的 sin(x) 有多精确?我怎么知道? [需要它来数值求解 x=a*sin(x)],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54207382/
我正在开发一个 Java 脚本,为此我需要正则表达式来检查文本框中输入的文本是否应该是字母和数值的组合。 我尝试了 Java 脚本的 NaN 函数,但字符串的最小长度和最大长度应为 4,并以字母作为第
我给出了两个长方体,其中只有一个轴对齐(另外两个不需要对齐)和顶点坐标(在全局坐标系中),我知道它们相交。我正在寻找一种可以计算路口体积的算法。 为了检查交点,我使用了分离轴定理。 最佳答案 可以通过
我有一个类似这样的对象的 json 列表 [{ "something": "bla", "id": 2 }, { "something": "yes", "id": 1
这是一篇很长的文章,但请留在我身边... 我有一个字典,它将“PO”保存为Key,将“SO”保存为项目(在某些情况下,某个“PO”可能有多个“SO”) . 工作表中的我的 Excel 数据,字典在其中
我的问题是是否有办法使用 terms include在 numeric field在 elasticsearch aggregation . 我在 Elasticsearch 中对多个字段使用通用查询
我有一个 perl 代码片段 use JSON::XS; $a = {"john" => "123", "mary" => "456"}; print encode_json($a),"\n"; 输出
我想对 python 进行一个条件测试,以检查给定输入数字的值是否等于或小于 9,并且大于或等于 0。 number =input( "Please enter a number! :" ) Plea
我有一个这样的对象: var rock = { 5: 0.5, 0: 0.8, 10: 0.3, 2: 1.0, } 我有一个像 4.3 这样的数字,我需要前后数字的索引和值。在这个例子中我会
对于 iOS 中的 Objective-C: 如果我有一个字符串,如何读取单个字符的 unicode 数值? 例如,如果我的字符串是:“Δ”,unicode 字符是 U+0394,那么我如何读取该字符
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我有这样的数组 var arrayVal_Int = ["21", "53", "92", "79"]; var arrayVal_Alpha = ["John", "Christine", "L
就像标题暗示我需要做这样的事情...... $i++;//we all know this. $value = 'a'; increment($value);// i need this functi
我有一个文件,其中包含一些不同值的概率,例如: 1 0.1 2 0.05 3 0.05 4 0.2 5 0.4 6 0.2 我想使用此分布生成随机数。是否存在处理此问题的现有模块?自己编写代码相当简单
因此,我在从使用 RCPP 创建的函数返回值时遇到了一些问题。它只返回 NumericVector 的第一个值。问题是当我在自身内部调用函数并将 NumericVector 传递回 out 变量时。任
我有下面的数字 vector 模板类(用于数值计算的 vector )。我正在尝试使编写 D=A+B+C 成为可能,其中所有变量都是 Vector 对象。 A、B 和 C 不应修改。我的想法是使用 V
本文实例讲述了mysql常用函数。分享给大家供大家参考,具体如下: 本文内容: mysql函数的介绍 聚集函数 avg count max
我正在尝试使用 python(无关)为我的公司自动化一些事情,这就是我的问题。首先,我正在从邮箱中的特定文件夹创建数据框。(到这里没问题)” RangeIndex: 36 entries, 0 to
我在让 Angular ng-if 工作时遇到了一些麻烦。我希望我的 DOM 元素之一在 $scope.week = 1 时消失。 在我的 Controller 中我设置了 $scope.week =
我正在阅读 Ingersoll、Morton 和 Farris 撰写的 Taming Text,但我不明白 solr 的数字 trie 实现如何帮助搜索文本?我对 solr.TrieField fie
这个问题已经有答案了: What is the difference between client-side and server-side programming? (3 个回答) 已关闭 9 年前
我是一名优秀的程序员,十分优秀!