python - 通过对一系列求和来计算 cos(x)

标签 python

我对计算机编程很陌生,最近刚刚开始学习Python。在这个作业中,我需要通过对系列 1 - x^2/2 求和来计算 cos(x)! + x^4/4! - x^6/6! 。 。 .

在不使用 numpy 或 m.factorial 的情况下如何做到这一点?我想我只应该使用 while 循环。这是到目前为止我的代码

print("INVESTIGATION OF COMPLEX INFINITE SERIES")
print("")
print("Part A: exp, cos and sin series for real value 1")
print("Using convergence criterion of 1e-20")
print("")
print("count         exp terms       sign         cos terms         sin terms")
print("----------------------------------------------------------------------")

count = 0.0 # number of terms added so far
total = 0.0 # total of terms so far
termSign = 1 
term = 1.0 # initial term
xx = 1

while abs(term) > 1e-20:
    count += 1
    print("%2d   %22.16g    %2d" % (count, term, termSign))
    termSign = (-1)**(count//2)
    total = total + term
    term = term/count

代码的输出应如下所示:

 count     exp terms          sign          cos terms 
 ----------------------------------------------------------
 1                      1      1       1.00000000000000000                                  
 2                      1      1       
 3                    0.5     -1      -0.50000000000000000                                                                                          
 4     0.1666666666666667     -1    
 5    0.04166666666666666      1       0.04166666666666666                                                                                          
 6   0.008333333333333333      1       
 7   0.001388888888888889     -1      -0.00138888888888889                                                                                      
 8  0.0001984126984126984     -1                                                                                                        
 9   2.48015873015873e-05      1       0.00002480158730159                                                                                          
10  2.755731922398589e-06      1
11  2.755731922398589e-07     -1      -0.00000027557319224                                                                                          
12  2.505210838544172e-08     -1                                                                                                    
13   2.08767569878681e-09      1       0.00000000208767570                                                                                          
14  1.605904383682162e-10      1                                                                                    
15  1.147074559772973e-11     -1      -0.00000000001147075                                                                                          
16  7.647163731819817e-13     -1                                                                                                        
17  4.779477332387386e-14      1       0.00000000000004779                                                                                          
18  2.811457254345521e-15      1                                                                                                            
19  1.561920696858623e-16     -1      -0.00000000000000016                                                                                          
20  8.220635246624331e-18     -1                                                                                                        
21  4.110317623312165e-19      1       0.00000000000000000                                                                                          
22  1.957294106339126e-20      1                                                                                                        
-----------------------------------------------------------

最佳答案

你已经很接近了......你遗漏了几个计算步骤。

x = 3.1415926535 / 4

sum_up = 1
term = 1
converge = 1e-20
i = 1
while abs(term) > converge:
    term = -term * x * x / (i * (i+1))
    sum_up += term
    i += 2

print sum_up

输出:

0.707106781202

关于python - 通过对一系列求和来计算 cos(x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35234763/

相关文章:

python - 从提要内容中获取图标

java - 谷歌 Foobar : Free the Bunny Prisoners clarification

python - 从 txt 文件读取到 3 个不同列表时如何防止硬编码 - python

python - 在大图像中使用 findCirclesGrid()

python - Heroku 仍然重定向到 https

python - 当等于或大于 0 时,将三个连续数字相加 - Python

python - 扭曲回溯中的 "<exception caught here>"行是什么意思?

python - 阅读 Python 源代码以提高编程技能

python - 是否存在报告实验室 rml 的所见即所得编辑器?

python - 使用 Google App Engine 时无法导入 Flask