简体   繁体   English

C中的Arctan Taylor系列

[英]Arctan Taylor Series in C

I need to perform the Taylor Series for arctangent 50 times. 我需要为反正切50次执行泰勒级数。 Meaning 50 numbers between the domain of the arctan Taylor Series which is [-1,1]. 意思是arctan泰勒系列域之间的50个数,即[-1,1]。 I've tested it out with manual user input and it works fine, but the for loop for the 50 different inputs which I increment in the code by 0.01 and their corresponding results has been unsuccessful. 我已经用手动用户输入测试了它并且它工作正常,但是50个不同输入的for循环,我在代码中增加0.01并且它们的相应结果不成功。 I've tried everything I could think of so far, I'm out of ideas. 到目前为止,我已经尝试了所有我能想到的东西,我没有想法。 Any help would be appreciated. 任何帮助,将不胜感激。 Is there an issue with my brackets surrounding the Taylor Series that's conflicting with the other for loop? 围绕泰勒系列的括号是否存在与其他环路冲突的问题? I've suspected it was the brackets but nothings worked when I attempted to fix it. 我怀疑它是括号,但是当我试图修复它时没有工作。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

main (void) {
    double n;
    double x;
    double tSeries=0.0;
    double t2;
    double t1;

    for(n=0;n<=50;++n) {
        for(x=-1;x<=1;x=x+0.01) {

            t1=(pow(x,2*n+1))*pow(-1,n);
            t2=t1/(2*n+1);
            tSeries+=t2;

            printf("arctan(%lf)=%lf\n",x,tSeries);
        }
    }
    return 0;
}

In the code you've posted the inner loop is over the variable x, and the outer loop is over the power n. 在您发布的代码中,内部循环位于变量x上,外部循环超过了幂n。

I think you want to sum over values of n for each value of x, so the loop over n should be the inner loop. 我想你想要为x的每个值求和n的值,所以n上的循环应该是内循环。

I think you also need to zero your sum, tSeries for each value of x. 我认为你还需要为x的每个值零和tSeries。

Finally, I expect you want to print the answer after calculating the sum, so printf should be outside the n loop. 最后,我希望您在计算总和后打印答案,因此printf应该在n循环之外。

There are a few tricks to the evaluation of power series. 评估电源系列有一些技巧。 I like Numerical Recipes for this sort of thing. 我喜欢Numerical Recipes这种东西。 Try chapter 5 on the evaluation of functions. 尝试第5章关于功能的评估。 (Numerical Recipes in C, Press et al., 2nd Ed., 1992, CUP.) (Numerical Recipes in C,Press et al。,2nd Ed。,1992,CUP。)

One thing to note right away is that with the upper limit of the power series fixed, you are evaluating a polynomial. 有一点需要注意的是,在固定功率系列的上限时,您正在评估多项式。 Section 5.3 of my copy of NR recommends strongly against using a sum of calls to pow(). 我的NR副本第5.3节建议强烈反对使用pow()的一个调用。 They are quite firm about it! 他们对此非常坚定!

Let me know if you want me to post correct code. 如果您希望我发布正确的代码,请告诉我。

You got the loops mixed, the inner one goes out and vice versa. 你把循环混合在一起,内部循环,反之亦然。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
  double n;
  double x;
  double tSeries = 0.0;
  double t2;
  double t1;

  for (x = -1; x <= 1; x += 0.01) {
    for (n = 0; n <= 50; n++) {
      t1 = (pow(x, 2 * n + 1)) * pow(-1, n);
      t2 = t1 / (2 * n + 1);
      tSeries += t2;
    }
    printf("arctan(%lf)=%lf (%lf)\n", x, tSeries, atan(x));
    tSeries = 0.0;
  }
  return 0;
}

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

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