【发布时间】:2011-11-04 22:40:14
【问题描述】:
我正在尝试编写一个程序来使用泰勒级数计算 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;
}
对于我的示例,我使用角度 = 90 和重复次数 = 20 来计算 cos(90),但它没有用,我只是不断获得接近无限的值,任何帮助将不胜感激。
【问题讨论】:
-
你的意思不是 90 度,对吗?泰勒级数仅在您使用弧度时才有效。即便如此,它们也只能在更接近开发点(在你的情况下为零)时工作得更好。
标签: c math taylor-series