问题描述
通过泰勒级数展开来得出三角角的值是最有用的方法之一。对于三角角,我们必须将角度转换为弧度。泰勒多项式的第2到3个扩展给出的准确值几乎为4位数字。但是,如果我们需要更高的精确度和更精确的小数位,我会发现泰勒级数展开会遇到一些困难。
例如,通过Python编程派生cos(4Pi/15)
或cos80(degrees)
,我尝试如下
#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使用哪些方法? (即泰勒级数展开以外的方法)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)