泰勒级数展开近似sin(x)的值 C语言(C language for approximating the value of sin (x) by Taylor series expansion)

编写程序,从键盘输入x,利用幂级数展开计算sin(x)的近似值,要求某一项绝对值误差小于10^-5。 公式如下:

方法提示:对于类似的数列求和问题,关键是抽象出第i项的通用公式,将推导出的通用第i项累加到sum,直到第i项的绝对值小于1e-5为止。另外,注意奇偶项符号的处理。

输入格式:输入x。

输出格式:sin(x)的逼近结果。

输入样例:在这里给出一组输入。

例如:

0.5233结尾无空行

输出样例:保留4位小数。

例如:

0.4997结尾无空行

code:

 1 #include<stdio.h>
 2 #include<math.h>
 3 int main()
 4 {
 5     double a;
 6     scanf("%lf",&a);
 7     
 8     
 9     double zi=a,mu=1;//zi指的是分子 mu指的是分母 
10     double xiang=zi/mu; 
11     double sin=xiang;
12     int j=1;//统计第几项 
13     
14     for(int i=3;;i+=2)
15     {
16         if(fabs(xiang)<1e-5) break;//注意当有一项满足时跳出 
17         
18         j++;
19         zi=pow(a,i);
20         mu=i*(i-1)*mu;
21         xiang=zi/mu;
22         
23         if(j%2==0)//通过观察可知奇数项为正 偶数项为负 
24         xiang=-xiang;
25         
26         sin+=xiang;//sin的值 
27     }
28     printf("%.4lf",sin);
29     return 0;
30 }

这篇文章以互相学习为主,有什么错的还望告知,谢谢啦

————————

Write a program, input x from the keyboard, and use the power series expansion to calculate the approximate value of sin (x). It is required that the absolute value error of a certain item is less than 10 ^ – 5. The formula is as follows:

Method tip: for similar sequence summation problems, the key is to abstract the general formula of item I and add the derived general item I to sum until the absolute value of item I is less than 1e-5. In addition, pay attention to the processing of parity symbols.

Input format: enter X.

Output format: approximation result of sin (x).

Input example: a set of inputs is given here.

For example:

0.5233 no blank line at the end

Output example: keep 4 decimal places.

For example:

0.4997 no blank line at the end

code:

 1 #include<stdio.h>
 2 #include<math.h>
 3 int main()
 4 {
 5     double a;
 6     scanf("%lf",&a);
 7     
 8     
 9     double zi=a,mu=1;//zi指的是分子 mu指的是分母 
10     double xiang=zi/mu; 
11     double sin=xiang;
12     int j=1;//统计第几项 
13     
14     for(int i=3;;i+=2)
15     {
16         if(fabs(xiang)<1e-5) break;//注意当有一项满足时跳出 
17         
18         j++;
19         zi=pow(a,i);
20         mu=i*(i-1)*mu;
21         xiang=zi/mu;
22         
23         if(j%2==0)//通过观察可知奇数项为正 偶数项为负 
24         xiang=-xiang;
25         
26         sin+=xiang;//sin的值 
27     }
28     printf("%.4lf",sin);
29     return 0;
30 }

This article focuses on learning from each other. If there is anything wrong, please let me know. Thank you