简体   繁体   English

C中的泰勒级数

[英]Taylor Series in C

I'm trying to make a program to calculate the cos(x) function using taylor series so far I've got this: 到目前为止,我正在尝试制作一个使用taylor系列计算cos(x)函数的程序:

int factorial(int a){

if(a < 0)
    return 0;
else if(a==0 || a==1)
    return 1;
else
    return a*(factorial(a-1));
}

double Tserie(float angle, int repetitions){
    double series = 0.0;
    float i;

for(i = 0.0; i < repeticiones; i++){
    series += (pow(-1, i) * pow(angle, 2*i))/factorial(2*i);
    printf("%f\n", (pow(-1, i) * pow(angle, 2*i))/factorial(2*i));
}
return series;

} }

For my example I'm using angle = 90, and repetitions = 20, to calculate cos(90) but it's useless I just keep getting values close to the infinite, any help will be greatly appreciated. 对于我的示例,我使用angle = 90,重复数= 20来计算cos(90),但我一直保持值接近无穷大是没有用的,我们将不胜感激。

For one thing, the angle is in radians, so for a 90 degree angle, you'd need to pass M_PI/2 . 一方面,角度为弧度,因此对于90度角,您需要传递M_PI/2

Also, you should avoid recursive functions for something as trivial as factorials, it would take 1/4 the effort to write it iteratively and it would perform a lot better. 另外,对于像乘数这样琐碎的事情,您应该避免使用递归函数,这将花费1/4的精力来迭代编写它,并且性能会更好。 You don't actually even need it, you can keep the factorial in a temporary variable and just multiply it by 2*i*(2*i-1) at each step. 您实际上甚至不需要它,您可以将阶乘保留在一个临时变量中,然后在每个步骤将其乘以2*i*(2*i-1) Keep in mind that you'll hit a representability/precision wall really quickly at this step. 请记住,在此步骤中,您很快就会碰到可表示性/精度的墙。

You also don't need to actually call pow for -1 to the power of i , a simple i%2?1:-1 would suffice. 您也不需要实际将-1的pow称为i的幂,简单的i%2?1:-1就足够了。 This way it's faster and it won't lose precision as you increase i . 这样,它速度更快,并且在您增加i不会失去精度。

Oh and don't make i float , it's an integer, make it an integer. 哦,别让i float ,它是一个整数,请使其成为一个整数。 You're leaking precision a lot as it is, why make it worse.. 您确实在泄漏精度,为什么还要使其更糟。

And to top it all off, you're approximating cos around 0, but are calling it for pi/2 . 最重要的是,您将cos近似为0,但将其称为pi/2 You'll get really high errors doing that. 这样做会导致很大的错误。

The Taylor series is for the mathematical cosine function, whose arguments is in radians. 泰勒级数用于数学余弦函数,其参数为弧度。 So 90 probably doesn't mean what you thought it meant here. 所以90可能并不代表您的意思。

Furthermore, the series requires more terms the longer the argument is from 0. Generally, the number of terms need to be comparable to the size of the argument before you even begin to see the successive terms becoming smaller, and many more than that in order to get convergence. 此外,该系列要求参数从0开始的时间越长,则需要更多的项。通常,在您甚至开始看到连续项变得更小之前,项的数量必须与参数的大小可比,并且数量要更多获得融合。 20 is pitifully few terms to use for x=90. 20很少用于x = 90的术语。

Another problem is then that you compute the factorial as an int . 然后,另一个问题是您将阶乘计算为int The factorial function grows very fast -- already for 13! 阶乘函数增长非常快-已经有13个! an ordinary C int (on a 32-bit machine) will overflow, so your terms beyond the sixth will be completely wrong anyway. 普通的C int (在32位计算机上)将溢出,因此,超出第六位的条件将是完全错误的。

In fact the factorials and the powers of 90 quickly become too large to be represented even as double s. 实际上,阶乘和90的幂很快变得太大而无法表示为double s。 If you want any chance of seeing the series converge, you must not compute each term from scratch but derive it from the previous one using a formula such as 如果您希望看到级数收敛,则一定不要从头开始计算每个项,而应使用诸如

nextTerm = - prevTerm * x * x / (2*i-1) / (2*i);

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

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