python - 泰勒级数展开以导出 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 使用了哪些方法?(即泰勒级数展开以外的方法)
解决方案
推荐阅读
- google-bigquery - 在 Big Query 中使用不同的日期格式查询多个表
- python - Pandas Dataframe 中的同时出现计数
- python - bot.event on_ready 使我的命令停止工作 discord.py
- javascript - 我在将我发现的现有反应组件集成到我的应用程序中时遇到问题。“尝试导入错误:”
- scala - 不处理消息的 Akka(Typed)actor Behavior 的类型是什么?
- react-native - 使用 GitLab 构建时 Expo 中的环境变量
- python - Scrapy spider 在同一个项目中显示另一个不相关的spider 的错误
- google-cloud-platform - Apache Beam - Bigquery Upsert
- r - 在函数结束前通过 eval 调用 on.exit
- r - 从修改后的距离矩阵计算加权距离