简体   繁体   English

在 python 中通过其泰勒级数实现 cos(x)

[英]Implementing cos(x) by its Taylor series in python

I have to implement the cosine function in python by using its Taylor series.我必须使用其泰勒级数在 python 中实现余弦 function。 I have to print its values and the absolute and relative errors for all the values in listt from below.我必须从下面打印它的值以及 listt 中所有值的绝对和相对误差。 Here is what I tried:这是我尝试过的:

import math

def expression(x, k):
   return (-1)**k/(math.factorial(2*k))*(x**(2*k))
pi=math.pi
listt=[0, pi/6, pi/4, pi/3, pi/2, (2*pi)/3, (3*pi)/4, (5*pi)/6, pi]
sum=0
for x in listt:
   k=0
   relative_error=1
   while relative_error>10**(-15):
       sum+=expression(x, k)
       absolute_error=abs(math.cos(x)-sum)
       relative_error=absolute_error/abs(math.cos(x))
       k+=1
    print("%.20f"%x, '\t', "%.20f"%sum, '\t', "%.20f"%math.cos(x), '\t', "%.20f"%absolute_error, '\t', "%.20f"%relative_error )

This, however, produces a huge error.然而,这会产生一个巨大的错误。 The cause is probably that I perform all those subtractions.原因可能是我执行了所有这些减法。 However, I don't know how to avoid them.但是,我不知道如何避免它们。 I ran into the same problem when computing e^x for negative x, but there I just computed e^(-x) if x was negative and I could then just write that e^x=1/e^(-x).我在为负 x 计算 e^x 时遇到了同样的问题,但是如果 x 为负,我只计算 e^(-x),然后我可以写出 e^x=1/e^(-x)。 Is there some similar trick here?这里有类似的技巧吗?
Also, note that my while is infinite, because the relative error is always huge.另外,请注意我的 while 是无限的,因为相对误差总是很大。

Fatal error: You set sum=0 outside the loop over the test points.致命错误:您在测试点的循环之外设置了sum=0 You need to reset it for every test point.您需要为每个测试点重置它。 This error could be easily avoided if you made the approximate cosine computation a separate function.如果您将近似余弦计算作为单独的 function 进行,则可以轻松避免此错误。


It is always a better idea to compute the terms of the series by multiplying with the quotient to the previous term.通过乘以前一项的商来计算级数的项总是一个更好的主意。 This avoids the computation of the factorial and all the difficulties that its growth may produce.这避免了阶乘的计算及其增长可能产生的所有困难。

That said, the growth of terms is the same as those for the exponential series, so the largest term is where 2*k is about |x|也就是说,项的增长与指数级数的增长相同,因此最大项是2*k约为|x| . . For the given points that should not be prohibitively large.对于不应过大的给定点。

To be a little more precise, the error of a cosine partial sum is smaller than the next term, as the series is alternating.更准确地说,余弦部分和的误差小于下一项,因为级数是交替的。 The term of degree 2*k for |x|<=4 has the approximate bound, using Stirling's formula for the factorial, |x|<=42*k度项具有近似界,使用斯特林的阶乘公式,

4^(4*k)/(4*k/e)^(4*k) = (e/k)^(4*k) < (3/k)^(4*k)

which for k=6 gives an upper bound of 2^(-24) ~ 10^(-7) .对于k=6 ,其上限为2^(-24) ~ 10^(-7) Thus there should be no catastrophic errors for the test values.因此,测试值不应该有灾难性的错误。

See also Calculate maclaurin series for sin using C , Issue with program to approximate sin and cosine values , Sine calculation with Taylor series not working另请参阅使用 C 计算正弦的麦克劳林级数程序问题以近似正弦和余弦值泰勒级数的正弦计算不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM