gpt4 book ai didi

python - x tan(x) - b 的零点

转载 作者:太空宇宙 更新时间:2023-11-04 04:28:09 26 4
gpt4 key购买 nike

scipy.special 有很多用于贝塞尔函数零点的函数,但我找不到返回

零点的函数

x cot(x) + b

其中 b 是一个给定的常数。这在拉普拉斯变换解决方案中很常见。我错过了什么?有人知道计算这些零的 Python 实现吗?

最佳答案

基于括号的寻根方法 brentq 在这里表现良好。两者 x*tan(x)x*cot(x)具有明确的单调性区域,每个区域包含一个根(根据 b 的比较和左边界 0 处的函数值进行一些调整)。这些函数为搜索选择一个合适的区间 [left, right]。

需要避免奇点,我通过添加或减去 1e-12 懒惰地做到了这一点;更好的解决方案是根据 b 的值来确定这个数量,或者将 brent 放在 try-catch block 中,并在搜索失败时调整左右边界,因为间隔不包围。

import numpy as np
from scipy.optimize import brentq

def xtan_root(b, n): # returns the nth root of x*tan(x) - b, starting with n = 0
if b >= 0:
left = np.pi*n
right = left + np.pi/2 - 1e-12
else:
right = np.pi*(n + 1)
left = right - np.pi/2 + 1e-12
return brentq(lambda x: x*np.tan(x) - b, left, right)

def xcot_root(b, n): # returns the nth root of x*cot(x) - b, starting with n = 0
if b <= 1:
left = np.pi*n + 1e-12
right = np.pi*(n + 1) - 1e-12
else:
left = np.pi*(n + 1) + 1e-12
right = np.pi*(n + 2) - 1e-12
return brentq(lambda x: x/np.tan(x) - b, left, right)

测试:

print([xtan_root(4.2, k) for k in range(7)])
print([xtan_root(-4.2, k) for k in range(7)])
print([xcot_root(4.2, k) for k in range(7)])
print([xcot_root(-4.2, k) for k in range(7)])

输出:

[1.2758753927614157, 3.956797869352993, 6.83423679964421, 9.828615139392694, 12.881549851182765, 15.965206716324374, 19.066376218026804]
[2.0188718954154634, 5.64338450641679, 8.98762321307202, 12.235715130594514, 15.442408472271651, 18.6277946237861, 21.800827083037863]
[3.888498133022309, 7.3338897678859825, 10.618936065272592, 13.842583205828442, 17.03705753823714, 20.215505142368496, 23.38423148227209]
[2.589154213035516, 5.375625747913372, 8.32140029715318, 11.349993150773297, 14.420577443300068, 17.514121595194442, 20.621277153352]

关于python - x tan(x) - b 的零点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53160468/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com