首页 > 解决方案 > 泰勒级数展开以导出 cos 角值

问题描述

通过泰勒级数展开推导三角角的值是最有用的方法之一。对于三角角,我们必须将角度转换为弧度。泰勒多项式的前 2 到 3 次展开给出了接近 4 位的准确值。但是如果我们需要更高的精确度和更精确的小数位,我发现泰勒级数展开有些困难。

例如,对于派生cos(4Pi/15)cos80(degrees)通过 Python 编程,我尝试如下

#Taylor series Cosine value for cos 80^^
import time
import math
from decimal import*
getcontext().prec = 100
b = 3.141592653589793238462643383279
print(b)

a = 80 # float(input("Enter required angle to calculate cos value: \n"))

#Calculate angle in radians

R = b*a/180
print("Angle in radians is: ",Decimal(R))
#r = math.radians(a)
#print(r)
#Taylor expansion

c = int(input("Enter number of terms to expand cos angle: \n"))
begin = time.time()
V = 0
for i in range(c):
    coef = (-1)**i
    num = Decimal(R)**(2*i)
    denom = math.factorial(2*i)
    V = V + (coef)*((num)/(denom))
print(V)
end = time.time()
print("Time for execution is \n", end - begin)
#Prints accurate to 16 digits
#print(math.sqrt(1)/2)

n = int(input("Enter a positive integer to get number of cycles to caluculate cos80\n "))
x = Decimal(2).sqrt()
begin = time.time()
for i in range(n):
    x = Decimal(2 + x).sqrt()
    x = Decimal(2 + x).sqrt()
    x = Decimal(2 - x).sqrt()

print(x/2)
end = time.time()
print("Time for execution is \n", end - begin)

我手动给出了 Pi 的 30 位小数精度来计算泰勒级数展开中的弧度

但与泰勒级数展开相比,另一种方法 - 2 的无限嵌套平方根的有限循环给出了更好的结果(我与Wolfram alpha进行了比较。

这种谬误发生在泰勒级数展开中,因为泰勒多项式的展开涉及幂和阶乘。这是十进制值不准确的原因吗?

还有哪些其他方法可以将这种角度的结果精确到多位数?

Wolfram alpha 使用了哪些方法?(即泰勒级数展开以外的方法)

标签: pythontrigonometrywolframalphataylor-series

解决方案


推荐阅读