评估1/tanh(x)-极小x的1/x [英] Evaluate 1/tanh(x) - 1/x for very small x

查看:204
本文介绍了评估1/tanh(x)-极小x的1/x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算数量

1/tanh(x) - 1/x

用于x > 0,其中x可以很小也可以很大.

for x > 0, where x can be both very small and very large.

渐近地对于小x,我们有

1/tanh(x) - 1/x  ->  x / 3

以及大型x

1/tanh(x) - 1/x  ->  1

无论如何,在计算表达式时,已经来自10^-7且舍入误差较小,导致表达式的值恰好为0:

Anyhow, when computing the expression, already from 10^-7 and smaller round-off errors lead to the expression being evaluated as exactly 0:

import numpy
import matplotlib.pyplot as plt


x = numpy.array([2**k for k in range(-30, 30)])
y = 1.0 / numpy.tanh(x) - 1.0 / x

plt.loglog(x, y)
plt.show()

推荐答案

对于非常小的x,可以使用

For very small x, one could use the Taylor expansion of 1/tanh(x) - 1/x around 0,

y = x/3.0 - x**3 / 45.0 + 2.0/945.0 * x**5

误差约为O(x**7),因此,如果选择10^-5作为断点,则相对误差和绝对误差将大大低于机器精度.

The error is of the order O(x**7), so if 10^-5 is chosen as the breaking point, relative and absolute error will be well below machine precision.

import numpy
import matplotlib.pyplot as plt


x = numpy.array([2**k for k in range(-50, 30)])

y0 = 1.0 / numpy.tanh(x) - 1.0 / x
y1 = x/3.0 - x**3 / 45.0 + 2.0/945.0 * x**5
y = numpy.where(x > 1.0e-5, y0, y1)


plt.loglog(x, y)
plt.show()

这篇关于评估1/tanh(x)-极小x的1/x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆