泰勒级数的正弦计算不起作用 [英] Sine calculation with Taylor series not working

查看:97
本文介绍了泰勒级数的正弦计算不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中编写了一个正弦函数,它应该计算x的正弦,就像





code> public static double sine(int x){
double result = 0;
for(int n = 0; n <= 8; n ++){
double temp = 2 * n + 1;
result + = Math.pow(-1,n)* Math.pow(x,temp)/ fact(temp); // fact()计算教员

}
返回结果;




$ b

只有几个小数位与Math.sin()匹配,但只有当x小于6时n的限制等于8.

有时,如果x大于6 sine()返回偶数4.106,或者如果n的限制大于32,返回NaN或-Infinity ...

我在做什么错?



希望你能帮助我,并提前致谢!

解决方案

退一步说,惊人的事实是,正弦序列首先评估为有界函数。毕竟,这是一个与指数函数密切相关的幂级数。作为部分和出现的多项式对于较大的参数具有较大的值是多项式的一般属性,因此不会令人惊讶。




避免重复递归函数,如同一个参数和阶乘的权力总是一个好主意。因此,减少到(最小数量的)基本算术运算看起来像这样:

  public static double sine(int terms,double x){
double result = 1;
double mxx = -x * x;
double addens = 1;
double temp = 2;
for(int n = 2; n <= terms; n ++){
addens * = mxx / temp ++ / temp ++;
结果+ = addens;
}
return x * result;


请注意,x的类型现在是双倍。 p>




循环的另一个破坏条件是

  while(1 + addens!= 1)

使用灵活的数字直到addens的贡献变得微不足道。这为更大范围的争论提供了准确的结果,但是对于大量的争论,成本会大幅增加。然后,人们可以同时探索一个平分和平方策略,计算sin和cos。

I wrote a sine function in Java, that should calculate the sine of x like

My function looks like

public static double sine(int x) {
    double result = 0;
    for(int n = 0; n <= 8; n++) {
        double temp = 2 * n + 1;
        result += Math.pow(-1, n) * Math.pow(x, temp) / fact(temp); // fact() calculates faculty

    }
    return result;

}

Only a few decimal places match to Math.sin(), but only if x is smaller then 6 and the limit of n equals to 8.
Sometimes, if x is greater than 6 sine() returns even 4.106 or if limit of n is greater than 32, it returns NaN or -Infinity...
What am I doing wrong?

Hope you can help me and thanks in advance!

解决方案

Taking a step back, the astonishing fact is that the sine series evaluates to a bounded function in the first place. After all, it is a power series closely related to the exponential function. That the polynomials that occur as partial sums have large values for large arguments is a general property of polynomials and thus not astonishing at all.


Avoiding the recomputation of recursive functions like powers of the same argument and factorials is always a good idea. Thus a reduction to (a minimal amount of) elementary arithmetic operations looks like this:

public static double sine(int terms, double x) {
    double result = 1;
    double mxx = -x*x;
    double addens = 1;
    double temp = 2;
    for(int n = 2; n <= terms; n++) {
        addens *= mxx/temp++/temp++;
        result += addens; 
    }
    return x*result;

}

Note that the type of x is now double.


Another breaking condition for the loop would be

   while(1+addens != 1)

to use a flexible number of terms until the contribution of addens becomes negligible. This gives accurate results for a larger range of arguments, however for large arguments the cost will dramatically increase.

Then one can explore a halving-and-squaring strategy with a simultaneous computation of sin and cos.

这篇关于泰勒级数的正弦计算不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆