简体   繁体   中英

Comping Sin Approximation

Trying to write a program in Java to compute the power series approximation for sin (x) using the following equation:

x - x^3/3! + x^5/5!... x^n/n!

I am doing this using a while loop and I cannot seem to get the value for the sin approximation 100% correct. Any idea where I am going wrong?

{
        double x = 0.0, sum = 1.0, y;
        int n = 1, count = 1;

        Scanner kb = new Scanner(System.in);

        System.out.println("Enter positive odd integer");
        n = kb.nextInt();
        System.out.println(n);

        System.out.println("Enter x");
        x = kb.nextDouble();
        y = x;
        System.out.println(x);

        while (true)
        {
            if (n == 1)
                {sum = x;

                }



            count = count + 2;
            sum = x + (x * -(x * x) / (count * (count - 1)));
            x = sum;

            if (count >= n)
                count = count + 2;
                sum = x + (x * -(x * x) / (count * (count - 1)));
                x = sum;
                break;






        }
        System.out.println("sum = " +sum);
        System.out.printf("sin("+y+") = " +Math.sin(y));


}

This currently gives me the following output:

Enter positive odd integer
99
Enter x
0.7
sum = 0.598559827320216
sin(0.7) = 0.644217687237691

you don't implement the formula correct - i can't see where power n...

i would have expect you to have something like x = x + (n^-1) * (x^(2*n)/fac( (2*n) ) ); having n steps

NOTE the ^operator doesn't work on doubles but this is not a copy/paste solution (as seen from @paxDiablo( https://stackoverflow.com/users/14860/paxdiablo ) merely a solution on how to solve your problem...

The expression (count * (count - 1)) is not count! , not by a long shot.

When count becomes four, you start getting errors. because 4! is 24 but 4 x 3 is only 12 . It gets worse, of course: 10 x 9 is 90 but 10! is a whopping three and half million.


Another problem you have is that your if (count >= n) condition may not be doing what you expect. Aside from the fact it's almost certainly the wrong condition for some of the statements (you'd only want to do another term if count was still less than n but the break makes sense with the given condition), your indentation leads me to believe that you think the whole block will be executed for that if statement:

if (count >= n)
    count = count + 2;
    sum = x + (x * -(x * x) / (count * (count - 1)));
    x = sum;
    break;

However, indentation doesn't control that, braces do. Without the braces, only the first indented statement is subject to the if , all the others happen in every iteration.


But, regardless of that, your code logic seems very ... err, tortured. The great thing about most infinite series calculations is that you really only to worry about the sum and the current term. It seems very convoluted to be swapping around all those variables continuously when you can just have a loop like this:

double mult = 1.0;
while (count <= n) {
    term = Math.pow(x,count) / facto (count);
    sum += mult * term;
    count += 2;
    mult = -mult;
}

A complete program showing this in action follows:

import java.util.Scanner;

public class Test {
    public static double facto (int inum) {
        double num = inum;
        while (--inum > 1)
            num *= inum;
        return num;
    }

    public static void main(String[] args) {
        double term = 0.0, sum = 0.0, x;
        int n, count = 1;

        Scanner kb = new Scanner(System.in);

        System.out.print("Enter positive odd integer: ");
        n = kb.nextInt();
        if ((n < 1) || (n % 2) == 0) {
            System.out.println("Apparently, someone can't read too well :-)");
            n = (n < 1) ? 1 : n + 1;
        }
        System.out.println("Using " + n);

        System.out.print("Enter x: ");
        x = kb.nextDouble();
        System.out.println("Using " + x);

        double mult = 1.0;
        while (count <= n) {
            term = Math.pow(x,count) / facto (count);
            sum += mult * term;
            count += 2;
            mult = -mult;
        }

        System.out.println("" + sum + " <-- sum");
        System.out.printf("" + Math.sin(x) + " <-- sin(" + x + ")");
    }
}

And a sample run using your test data, showing it in action:

Enter positive odd integer: 99
Using 99
Enter x: 0.7
Using 0.7
0.644217687237691 <-- sum
0.644217687237691 <-- sin(0.7)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM