我们的教授给我们这个作业,要求我们使用泰勒级数计算给定数字的正弦,直到连续两项的差异小于 10-6,但由于我是计算机科学的新手,所以我不会真的很确定如何从这里继续下去。我相信阶乘计算正确。
我期望 sum 的输出与内置库的 sin(x) 输出相匹配
e.x.对于输入:1.0,sum 的输出应该是 0.841471,与在预制函数中输入 1.0 的结果匹配,但其输出是 0.158529。
#include <stdio.h>
#include <math.h>
int main()
{
int val = 0,neg;
long double sum = 0.0, x, prev = 0.0, curr, factorial = 1.0;
printf("Type X:\n");
scanf("%Lf",&x);
curr = x;
while (fabs(curr- prev) >= 0.000001){
val++;
prev = curr;
factorial *= (2*val) * (2*val+1);
curr = (pow(x, 2*val+1))/factorial;
if (val % 2 == 1)
neg = -1;
else
neg = 1;
sum += neg*curr;
}
printf("taylor series sinx:%Lf\n", sum);
printf("math library sinx:%lf\n", sin(x));
return 0;
}
您设置了
curr = x;
但也没有设置 sum = x;
。
当您打印操作时,这是显而易见的:
/* SO Staging Ground 7916-4500 */
/* SO 7916-4500 */
#include <stdio.h>
#include <math.h>
int main(void)
{
int val = 0, neg;
long double sum = 0.0, x, prev = 0.0, curr, factorial = 1.0;
printf("Type X:\n");
scanf("%Lf", &x);
curr = x;
while (fabsl(curr - prev) >= 0.000001)
{
val++;
prev = curr;
factorial *= (2 * val) * (2 * val + 1);
curr = (powl(x, 2 * val + 1)) / factorial;
if (val % 2 == 1)
neg = -1;
else
neg = 1;
sum += neg * curr;
printf("val = %2d: neg = %+2d; factorial = %12.0Lf; term = %15.9Le; "
"old = %15.9Le; new = %15.9Le\n",
val, neg, factorial, curr, prev, sum);
}
printf("Taylor series sinx: %Lf\n", sum);
printf("math library sinx: %Lf\n", sinl(x));
return 0;
}
产生:
Type X:
1
val = 1: neg = -1; factorial = 6; term = 1.666666667e-01; old = 1.000000000e+00; new = -1.666666667e-01
val = 2: neg = +1; factorial = 120; term = 8.333333333e-03; old = 1.666666667e-01; new = -1.583333333e-01
val = 3: neg = -1; factorial = 5040; term = 1.984126984e-04; old = 8.333333333e-03; new = -1.585317460e-01
val = 4: neg = +1; factorial = 362880; term = 2.755731922e-06; old = 1.984126984e-04; new = -1.585289903e-01
val = 5: neg = -1; factorial = 39916800; term = 2.505210839e-08; old = 2.755731922e-06; new = -1.585290154e-01
val = 6: neg = +1; factorial = 6227020800; term = 1.605904384e-10; old = 2.505210839e-08; new = -1.585290152e-01
Taylor series sinx: -0.158529
math library sinx: 0.841471
第一次计算的输出显示错误。 解决方法是将
sum
(以及 curr
)设置为 x
:
/* SO Staging Ground 7916-4500 */
/* SO 7916-4500 */
#include <stdio.h>
#include <math.h>
int main(void)
{
int val = 0, neg;
long double sum = 0.0, x, prev = 0.0, curr, factorial = 1.0;
printf("Type X:\n");
scanf("%Lf", &x);
sum = curr = x;
while (fabsl(curr - prev) >= 0.000001)
{
val++;
prev = curr;
factorial *= (2 * val) * (2 * val + 1);
curr = (powl(x, 2 * val + 1)) / factorial;
if (val % 2 == 1)
neg = -1;
else
neg = 1;
sum += neg * curr;
printf("val = %2d: neg = %+2d; factorial = %12.0Lf; term = %15.9Le; "
"old = %15.9Le; new = %15.9Le\n",
val, neg, factorial, curr, prev, sum);
}
printf("Taylor series sinx: %Lf\n", sum);
printf("math library sinx: %Lf\n", sinl(x));
return 0;
}
输出:
Type X:
1
val = 1: neg = -1; factorial = 6; term = 1.666666667e-01; old = 1.000000000e+00; new = 8.333333333e-01
val = 2: neg = +1; factorial = 120; term = 8.333333333e-03; old = 1.666666667e-01; new = 8.416666667e-01
val = 3: neg = -1; factorial = 5040; term = 1.984126984e-04; old = 8.333333333e-03; new = 8.414682540e-01
val = 4: neg = +1; factorial = 362880; term = 2.755731922e-06; old = 1.984126984e-04; new = 8.414710097e-01
val = 5: neg = -1; factorial = 39916800; term = 2.505210839e-08; old = 2.755731922e-06; new = 8.414709846e-01
val = 6: neg = +1; factorial = 6227020800; term = 1.605904384e-10; old = 2.505210839e-08; new = 8.414709848e-01
Taylor series sinx: 0.841471
math library sinx: 0.841471