需要麦克劳伦级数和泰勒级数编程帮助吗?

发布于 2024-09-03 09:29:04 字数 2908 浏览 5 评论 0原文

好吧,这就是我到目前为止所得到的:

#include <stdio.h>
#include <math.h>
//#define PI 3.14159

int factorial(int n){
  if(n <= 1)
    return(1);
  else
    return(n * factorial(n-1));
}

void McLaurin(float pi){
  int factorial(int);
  float x = 42*pi/180;
  int i, val=0, sign;

  for(i=1, sign=-1; i<11; i+=2){
    sign *= -1; // alternate sign of cos(0) which is 1
    val += (sign*(pow(x, i)) / factorial(i));
  }

  printf("\nMcLaurin of 42 = %d\n", val);
}

void Taylor(float pi){
  int factorial(int);
  float x;
  int i;
  float val=0.00, sign;
  float a = pi/3;

  printf("Enter x in degrees:\n");
  scanf("%f", &x);
  x=x*pi/180.0;
  printf("%f",x);

  for(i=0, sign=-1.0; i<2; i++){
    if(i%2==1)
      sign *= -1.0; // alternate sign of cos(0) which is 1
    printf("%f",sign);
    if(i%2==1)
      val += (sign*sin(a)*(pow(x-a, i)) / factorial(i));
    else
      val += (sign*cos(a)*(pow(x-a, i)) / factorial(i));
    printf("%d",factorial(i));
  }

  printf("\nTaylor of sin(%g degrees) = %d\n", (x*180.0)/pi, val);
}

main(){
  float pi=3.14159;
  void McLaurin(float);
  void Taylor(float);

  McLaurin(pi);
  Taylor(pi);
}

这是输出:

McLaurin of 42 = 0
Enter x in degrees:
42
0.733038-1.00000011.0000001
Taylor of sin(42 degrees) = -1073741824

我怀疑这些令人震惊的数字的原因与我混合了浮点数和整数这一事实有关?但我就是想不出来......!!也许这是一个数学问题,但它从来都不是我的强项,更不用说微积分编程了。麦克劳林也失败了,它如何等于零?卧槽!请帮助纠正我的菜鸟代码。我还是一个初学者...

----更新----

#include <stdio.h>
#include <math.h>
//#define PI 3.14159

int factorial(int n){
  if(n <= 1)
    return(1);
  else
    return(n * factorial(n-1));
}

void McLaurin(float pi){
  int factorial(int);
  float x = 42*pi/180, val=0;
  int i, sign;

  for(i=1, sign=-1; i<11; i+=2){
    sign *= -1; // alternate sign of cos(0) which is 1
    val += (sign*(pow(x, i)) / factorial(i));
  }

  printf("\nMcLaurin of of sin(%f degrees) = %f\n", (x*180.0)/pi, val);
}

void Taylor(float pi){
  int factorial(int);
  float x;
  int i;
  float val=0, sign;
  float a = pi/3;

  printf("Enter x in degrees:\n");
  scanf("%f", &x);
  x=x*pi/180.0;
  printf("%f",x);

  for(i=0, sign=-1.0; i<2; i++){
    if(i%2==0)
      sign *= -1; // alternate sign of cos(0) which is 1
    printf("%f",sign);
    if(i%2==0)
      val += (sign*sin(a)*(pow(x-a, i)) / factorial(i));
    else
      val += (sign*cos(a)*(pow(x-a, i)) / factorial(i));
    printf("%d",factorial(i));
  }

  printf("\nTaylor of sin(%f degrees) = %f\n", (x*180.0)/pi, val);
}

main(){
  float pi=3.14159;
  void McLaurin(float);
  void Taylor(float);

  McLaurin(pi);
  Taylor(pi);
}

给了我奇怪的错误。

/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot open output file a.exe: Device or resource busy
collect2: ld returned 1 exit status
Can anyone explain what's wrong now?

Ok so here's what i have so far:

#include <stdio.h>
#include <math.h>
//#define PI 3.14159

int factorial(int n){
  if(n <= 1)
    return(1);
  else
    return(n * factorial(n-1));
}

void McLaurin(float pi){
  int factorial(int);
  float x = 42*pi/180;
  int i, val=0, sign;

  for(i=1, sign=-1; i<11; i+=2){
    sign *= -1; // alternate sign of cos(0) which is 1
    val += (sign*(pow(x, i)) / factorial(i));
  }

  printf("\nMcLaurin of 42 = %d\n", val);
}

void Taylor(float pi){
  int factorial(int);
  float x;
  int i;
  float val=0.00, sign;
  float a = pi/3;

  printf("Enter x in degrees:\n");
  scanf("%f", &x);
  x=x*pi/180.0;
  printf("%f",x);

  for(i=0, sign=-1.0; i<2; i++){
    if(i%2==1)
      sign *= -1.0; // alternate sign of cos(0) which is 1
    printf("%f",sign);
    if(i%2==1)
      val += (sign*sin(a)*(pow(x-a, i)) / factorial(i));
    else
      val += (sign*cos(a)*(pow(x-a, i)) / factorial(i));
    printf("%d",factorial(i));
  }

  printf("\nTaylor of sin(%g degrees) = %d\n", (x*180.0)/pi, val);
}

main(){
  float pi=3.14159;
  void McLaurin(float);
  void Taylor(float);

  McLaurin(pi);
  Taylor(pi);
}

and here's the output:

McLaurin of 42 = 0
Enter x in degrees:
42
0.733038-1.00000011.0000001
Taylor of sin(42 degrees) = -1073741824

I suspect the reason for these outrageous numbers goes with the fact that I mixed up my floats and ints? But i just cant figure it out...!! Maybe its a math thing, but its never been a strength of mine let alone program with calculus. Also the Mclaurin fails, how does it equal zero? WTF! Please help correct my noobish code. I am still a beginner...

----Update----

#include <stdio.h>
#include <math.h>
//#define PI 3.14159

int factorial(int n){
  if(n <= 1)
    return(1);
  else
    return(n * factorial(n-1));
}

void McLaurin(float pi){
  int factorial(int);
  float x = 42*pi/180, val=0;
  int i, sign;

  for(i=1, sign=-1; i<11; i+=2){
    sign *= -1; // alternate sign of cos(0) which is 1
    val += (sign*(pow(x, i)) / factorial(i));
  }

  printf("\nMcLaurin of of sin(%f degrees) = %f\n", (x*180.0)/pi, val);
}

void Taylor(float pi){
  int factorial(int);
  float x;
  int i;
  float val=0, sign;
  float a = pi/3;

  printf("Enter x in degrees:\n");
  scanf("%f", &x);
  x=x*pi/180.0;
  printf("%f",x);

  for(i=0, sign=-1.0; i<2; i++){
    if(i%2==0)
      sign *= -1; // alternate sign of cos(0) which is 1
    printf("%f",sign);
    if(i%2==0)
      val += (sign*sin(a)*(pow(x-a, i)) / factorial(i));
    else
      val += (sign*cos(a)*(pow(x-a, i)) / factorial(i));
    printf("%d",factorial(i));
  }

  printf("\nTaylor of sin(%f degrees) = %f\n", (x*180.0)/pi, val);
}

main(){
  float pi=3.14159;
  void McLaurin(float);
  void Taylor(float);

  McLaurin(pi);
  Taylor(pi);
}

Gives me weird error.

/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot open output file a.exe: Device or resource busy
collect2: ld returned 1 exit status
Can anyone explain what's wrong now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉月流沐 2024-09-10 09:29:04

一些潜在的错误来源:

  • 您的 pi 值没有足够的位数来保证浮点精度;
  • 在您的 Maclaurin 函数中,您将 val 作为整数,对其进行除法;
  • 在泰勒中你只使用了系列中的两个术语?

我想

Some potential sources of error:

  • your value of pi doesn't have sufficient digits for even float accuracy;
  • in your function Maclaurin you have val as an integer, on which you perform division;
  • in Taylor you only use 2 terms in the series

I think ?

空城旧梦 2024-09-10 09:29:04

有几件事显而易见:

  1. 在 McLauren 中,您将 val(您的计算值)定义为 int。尝试将其定义为泰勒中的浮点数
  2. ,您只是每隔一段时间翻转一次符号,因此它会变为 -1, 1, -1, -1, 1, 1 .... 等等,
  3. 我认为您的泰勒级数公式不正确,在 wikipedia 刷新自己之后,您不应该调用 sincos (因为这就是您要计算的内容)

祝您好运!

a few things jump out:

  1. in McLauren you define val (your calculated value) as an int. try defining it as a float
  2. in Taylor you are only flipping your sign every other time so it goes -1, 1, -1, -1, 1, 1 .... and so on
  3. i think your formula for the Taylor series is incorrect, after refreshing myself on wikipedia, you shouldn't be calling sin or cos (because that's what you are trying to calculate)

good luck!

倾城°AllureLove 2024-09-10 09:29:04

已经提到的这个肯定很麻烦:

  • McLaurin() 中的 val 应该是一个浮点数

另外,你需要使用 %f%g 而不是 %d 当使用 printf() 打印出您的 val 时。这就是为什么你会得到如此疯狂的数字的最终原因:浮点数被解释为整数。

另外,在 Taylor() 中,您需要将两个 if 语句更改为 ,

if(i%2==0)

因为您是泰勒展开 sin,而不是 cos。

执行这些操作,使用与代码中相同数量的项,分别为 MacLaurin 和 Taylor 级数答案生成 0.669130 和 0.708945。如果我在泰勒级数答案中再添加两项(使用 i<4 而不是 i<2),我会得到 0.668793。真正的答案(使用与您使用的相同的 pi)是 0.669130,这是泰勒级数答案通过添加 3 个附加项得到的。

This one that have already been mentioned is definitely troublesome:

  • val should be a float in McLaurin()

In addition, you need to use %f or %g instead of %d when using printf() to print out your vals. That's ultimately why you're getting such crazy numbers: floats being interpreted as ints.

Also, in Taylor(), you need to change your two if statements to be

if(i%2==0)

because you are Taylor expanding sin, not cos.

Doing these things yields 0.669130 and 0.708945 for the MacLaurin and Taylor series answers, respectively, using the same number of terms as in your code. If I add two more terms to the Taylor series answer (use i<4 instead of i<2), I get 0.668793. The true answer (using the same pi as you used) is 0.669130 which the Taylor series answer gets to with 3 more additional terms added in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文