简体   繁体   English

C中的未知数学系列

[英]Unknown mathematical series in C

Does anybody have any idea what this code does?有人知道这段代码是做什么的吗? I suspect it has something to do with Taylor series, but I'm not sure since I don't really know what Taylor series is.我怀疑它与泰勒级数有关,但我不确定,因为我真的不知道泰勒级数是什么。 However, it could be anything.然而,它可以是任何东西。 I'm really not sure.我真的不确定。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
  double x, y, a;
  int n;
  int z;
  x = 25.0;
  if (z < 0) {
    z = 1;
    x = -x;
  } else
    z = 0;
  n = 0;
  a = 1;
  y = 1;
  for (n = n + 1; n < 20; n++) {
    a = a * x / n;
    y = y + a;
    printf("%i \t %.20g \t %g \n", n, y, a);
  }
  if (z) {
    x = -x;
    y = 1.0 / y;
  }
  printf("%i \t %.20g \t %g \n", n, y, a);
  return 0;
}

Enable compiler warnings.启用编译器警告。

if (z < 0) { is a coding error. if (z < 0) {是编码错误。 @Pablo . @巴勃罗

It certainly should be当然应该是

if (x < 0) {

Unrolling展开

  n = 0;
  a = 1;
  y = 1;
  for (n = n + 1; n < 20; n++) {
    a = a * x / n;
    y = y + a;

The terms are条款是

  y = 1 + x/1 + x*x/(1*2) + x*x*x/(1*2*3) + ....  pow(x,20)/(1*2*3*...*20)

Review Taylor series to discern which one.查看泰勒级数以辨别哪一个。


BTW, For x==25 , code Taylor's series does not converge enough.顺便说一句,对于x==25 ,代码泰勒级数不够收敛。 Either more terms are needed or better - an alternate/additional approach is needed.要么需要更多的术语,要么更好——需要一种替代/额外的方法。

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

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