用无穷级数评估sinx [英] Evaluation of sinx using infinite series
本文介绍了用无穷级数评估sinx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用无限系列评估sinx
我尝试过:
evaluation of sinx using infinite series
What I have tried:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int terms;
int count=0;
int sum = 0;int x;int n;
printf("enter number of terms");
scanf("%d",&terms);
printf("enter value of x");
scanf("%d",&x);
while(count <= terms){
n = 2*count+1;
sum = sum + ((pow(-1,count))*(pow(x,n)/(fact(n))));
count++;
}
printf("%d",sum);
}
int fact(int f){
int number = 1;
int product = 1;
while(number<=f){
product = number*product;
number++;
}
return product;
}
推荐答案
使用无限级数计算函数需要很长时间。在此之前,问问自己:
1.函数是否定期?如果是这样,将参数减少到一个句点。
2.是奇数还是偶数(f(x)== -f(-x)或f(x)== f(-x ))?如果是这样,写奇数函数,例如x * F(x ^ 2),偶数函数,例如G(x ^ 2)。
3.是否有任何其他属性可以提供帮助的功能吗?
完成后,您会发现只需要有限数量的元素即可获得所需的精确度。
祝你好运!
Using an infinite series to calculate a function will take a long time. Before doing so, ask yourself:
1. Is the function periodic? If so, reduce the argument to a single period.
2. Is it odd or even (f(x) == -f(-x) or f(x) == f(-x) )? If so, write the odd function as for example x*F(x^2), and the even function as for example G(x^2).
3. Are there any other attributes of the function that can help?
Once you have done that, you will find that only a finite number of elements are required to get the accuracy that you want.
Good luck!
根据建议,使用double
而不是INT
。享受:
As suggested, usedouble
s instead ofint
. Enjoy:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double fact(unsigned n)
{
double result =1.0;
unsigned int i;
for (i=2; i<=n; ++i )
result *= i;
return result;
}
int main()
{
unsigned terms;
unsigned count=0;
double result = 0.0;
double angle;
printf("enter number of terms ");
scanf("%d",&terms);
printf("enter value of x ");
scanf("%lf",&angle);
while(count <= terms)
{
double sign = count & 1 ? -1.0 : 1.0;
unsigned n = 2*count+1;
result += sign*(pow(angle,(double)n)/fact(n));
count++;
}
double br = sin(angle);
printf("approximation %g, builtin function result %g, difference %5.2f%%", result, br, fabs(result-br)/br*100.0);
}
这篇关于用无穷级数评估sinx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文