1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python做泰勒展开_python中的泰勒展开-问答-阿里云开发者社区-阿里云

python做泰勒展开_python中的泰勒展开-问答-阿里云开发者社区-阿里云

时间:2024-04-23 13:44:00

相关推荐

python做泰勒展开_python中的泰勒展开-问答-阿里云开发者社区-阿里云

也许有点过头了,但这里有个不错的解决方法,用辛普森法来计算无穷级数。

from sympy.abc import k

from sympy import Sum, oo as inf

import math

x = 0.5

result = Sum(

(

x**(2*k-1) /

(2*k-1)

) - (

x**(2*k) / (2*k)

),

(k, 1, inf)).doit()

#print(result) # 0.5*hyper((0.5, 1), (3/2,), 0.25) - 0.14384103622589

print(float(result)) # 0.4054651081081644

print(math.log(x+1, math.e)) # 0.4054651081081644

编辑: 我认为您的原始代码的问题是您还没有完全实现这个系列(如果我正确地理解了您问题中的数字)。看起来您试图实现的系列可以表示为

x^(2n-1) x^(2n)

( + ---------- - -------- ... for n = 1 to n = infinity )

2n-1 2n

而您的代码实际上实现了本系列

(-1)^2 * (x * 1) ( (-1)^(n+1) * (x^n) )

----------------- + ( -------------------- ... for n = 2 to n = infinity )

1 ( n )

编辑2: 如果你真的必须自己做迭代,而不是使用症状,这里是工作的代码:

import math

x = 0.5

n=0

sums = []

while True:

n += 1

this_sum = (x**(2*n-1) / (2*n-1)) - (x**(2*n) / (2*n))

if abs(this_sum) < 1e-8:

break

sums.append(this_sum)

lnsum = sum(sums)

print('The sum of terms greater than 10^-8 is:\t\t', lnsum)

print('math.log yields:\t\t\t\t', math.log(x+1, math.e))

输出:

The sum of terms greater than 10^-8 is: 0.4054651046035002

math.log yields: 0.4054651081081644

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。