-1

Can someone help me for geting out this code of sin(x) Tailor function to get followings:

  1. The first 4 sin(x) Tailor series.
  2. To calculating the sin function using the sum-formel
  3. How to write a method public static double MySinApproximate( double x)?

That is what i get so far, and it has to be in this way!!

import java.lang.Math;

public class mysin {
public static void main(String[] args){

    double x= Math.PI;

    System.out.println( MySin(x) + "\t \t" + Math.sin(x) + "\n" );

}

public static double MySin(double x){

    double sumNeu, sumOld, sum;
    int i = 1;
    sum = sumNeu = x;                    // This should calculating the first term Value
    do                                  //the loop do will calculating the Tailor Series
    {
        sumOld = sumNeu;
        i++; sum = + sum * x * x / i;
        i++; sum = sum / i;
        sumNeu = sumOld + sum;
    }
    while( sumNeu != sumOld);

    return sumNeu;
   }

} // 11.548739357257745              1.2246467991473532E-16 (as output)
Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52
Achim
  • 1
  • 1
  • 2
  • 2
    You mean **Taylor** series? –  Jan 06 '16 at 18:29
  • Possible duplicate of [How to use Math.cos() & Math.sin()?](http://stackoverflow.com/questions/13951136/how-to-use-math-cos-math-sin) – smac89 Jan 06 '16 at 19:09
  • @Smac89 That is not a duplicate. Your linked question is about needing to convert to radians before passing values to trigonometric methods in Java, but this is about calculating the sine with a Taylor series, where the value is already in radians (`Math.PI`). – rgettman Jan 06 '16 at 19:19

2 Answers2

1

Your loop isn't calculating the Taylor series correctly. (This is really a Maclaurin series, which is the special case of a Taylor series with a = 0.) For the sine function, the terms need to be added and subtracted in an alternating fashion.

sin(x) = x - x3/3! + x5/5! - ...

Your method only adds the terms.

sin(x) = x + x3/3! + x5/5! + ...

Flip the sign of sum on each iteration, by adding the designated line:

do    // The loop will calculate the Taylor Series
{
    sumOld = sumNeu;
    i++; sum = + sum * x * x / i;
    i++; sum = sum / i;
    sum = -sum;  // Add this line!
    sumNeu = sumOld + sum;
}

With this change I get a result that is very close:

2.3489882528577605E-16      1.2246467991473532E-16

Due to the inherent inaccuracies of floating-point math in Java (and IEEE in general), this is likely as close as you'll get by writing your own sine method.

I tested an additional case of π/2:

System.out.println( MySin(x/2) + "\t \t" + Math.sin(x/2) + "\n" );

Again, it's close:

1.0000000000000002      1.0
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

1.I want to write all again like that - 2.I try to writing the first 4 series from sine Taylor and the proximity all together but anyhow doesn't work correctly - 3.i get this output

0.0     0.8414709848078965

0.8414709848078965      0.9092974268256817

0.8414709848078965      0.1411200080598672

0.9092974268256817      -0.7568024953079282

4.How can i get the same accuracy

1.0000000000000002      1.0

and the series of sine(x)?

public class MySin {
    public static void main(String[] args){
        double y = 0;
        y = 4;
        for (int i = 1; i<= y; i++){
        System.out.println( MySin(i/2) + "\t \t" + Math.sin(i) + "\n" );
       }
    }
    public static double MySin(double x){
        double sumNew, sumOld, sum;
        int i = 1;
        sum = sumNew = x;                    // This should calculating the first term Value
        do                                  //the loop do will calculating the Tailor Series
        {
            sumOld = sumNew;
            i++; sum = - sum * x * x / i; // i did change the sign to - 
            i++; sum = sum / i;
             sum = - sum;                // so i don't need this line anymore 
            sumNew = sumOld + sum;
        }
        while( sumNew != sumOld);
        return sumNew;
    }
    public static double MySineProximity ( double x) {  
        while ( x <=  ( Math.PI /2 ) )
       {
             x = 0;
       }
        return MySin (x);
    }
}
Achim
  • 1
  • 1
  • 2