[英]Sn(x) = (x-1)^2n+1/(2n+1)!(n+1) - in C language
I tried to use this code to solve the question in the picture but I can't solve it, I'd be happy to help.我尝试使用此代码来解决图片中的问题,但我无法解决,我很乐意提供帮助。
how can I fix the code that works for the question in the picture?如何修复适用于图片中问题的代码? thanks.谢谢。
#include
void main() {
int n, i;
double sum, x, last;
do {
printf("Enter an positive number: ");
scanf("%d", & n);
} while (n<0);
printf("Please enter x: ");
scanf("%lf", & x);
sum = last = x;
for (i = 1; i<= n; i++) {
last = last * (-1) * ((x * x) / ((2 * i - 1) * (2 * i)));
sum = sum + last;
}
printf("S = %lf\n", sum);
}
As mentioned in the comments, you are using the wrong quotient.如评论中所述,您使用了错误的商。 Also, a closed expression for the value of the series is available.此外,还可以使用序列值的封闭表达式。
From the last term of the formula you can read off the general form of the terms从公式的最后一项,您可以读出这些项的一般形式
a[k] = (x-1)^(2k+1) / ( (2k+1)! * (k+1) ), k = 0,..., n
The quotient of two such adjacent terms is两个这样的相邻项的商是
a[k]/a[k-1] = (x-1)^2 / ( (2k+1)*2(k+1) )
so that the modified code computing your sum is这样修改后的代码计算你的总和是
#include<math.h>
#include<stdio.h>
void main() {
int n, i;
double sum, x,x1s last;
do {
printf("Enter an positive number: ");
scanf("%d", & n);
} while (n<0);
printf("Please enter x: ");
scanf("%lf", & x);
sum = last = x-1;
x1s = last*last;
for (i = 1; i<= n; i++) {
last = last * (x1s / ((2*i+1) * 2 * (i+1)));
sum = sum + last;
}
printf("S = %20.15lf, S_infty = %20.15lf\n", sum, 2*(cosh(x-1)-1)/(x-1));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.