Sine calculation with Taylor series not working

醉酒当歌 提交于 2019-11-29 16:20:08

With such a low value of n, the Taylor series is a poor approximation of sine for high numbers. For degree 8, any angle higher than around 4 will produce a significantly inaccurate result.

Because sine is periodic, one easy solution would be to modulus divide the angle (in radians) by 2*pi. The StrictMath Class in Java reduces the angle in a different way but the idea is the same. In that case, they reduce the angle to between [-pi/4, pi/4] to improve accuracy. This tool demonstrates how accuracy improves as the degree of the Taylor series increases.

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.

As monolyth421 already mentioned, your problem is that the Taylor expansion works good for small (close to zero) values of x. If you want to obtain a good accuracy, you should consider reducing the argument to the interval [-pi/2 , pi/2] (using trigonometric identities) and then use the Taylor expansion. Only a few terms should be then enough to get good accuracy.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!