Regarding Taylors Sinx Series...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhinuke
    New Member
    • May 2010
    • 23

    #1

    Regarding Taylors Sinx Series...

    As I posted earlier,I ve been practicing C for my upcoming Graduate Studies Venture.Just reading online books and practicing some basic codes.I happen to play with taylors Sinx Series on Netbeans IDE and when I run the program with values of 'n' and angle in radians,the compiler is stuck.The only thing I can see is that my processor usage is full and I cant do anything else.

    Here is the code that I made using Sinx formula from calculus book.



    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        int n,m;
        double sesum;
        double sinx(double,double);
        printf("Enter the limit of series greater than 0\n");
        scanf("%f",&n);
        printf("Enter the angle in radians\n");
        scanf("%f",&m);
        sesum=sinx(n,m);
        printf("The value of series is %f",sesum);
        return 0;
    
     }
    double sinx(double p,double q)
    {
        int i;
        double fact(double);
        double sum=0;
        for(i=0;i<=p;i++)
                {
                    sum=sum+(pow(q,2*i+1)/fact(2*i+1))*(pow(-1,i));
                }
    
        return sum;
    }
    double fact(double a)
    {
        double fact=1;
        int j;
        for(j=a;j>=1;j--)
        {
            fact=a * (a-1);
        }
        return fact;
    }

    I just need some hints or implication as to where I can improve so that code will work without any problems with IDE.Further any suggestions for a good book of C and C++.Preferably American author.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Please post the mathematical formulas you are trying to implement.

    Something seems wrong. For example, I assume that fact is supposed to compute the factorial of the input; but the input is floating-point. Factorial only makes sense for integral arguments.

    Comment

    • abhinuke
      New Member
      • May 2010
      • 23

      #3
      Yeah...even I tried to insert int data type first.But still NetBeans was acting weird thats why I used float data type.
      I am inserting an image of that formula which I have here with me.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Your scanf()s are not correct.

        You say %f to put data into m and n which are ints. The result is a grebage value.

        On a test run I made the garbage value is so large the machine appears to hang.

        Use %d with ints.

        Comment

        • abhinuke
          New Member
          • May 2010
          • 23

          #5
          Originally posted by weaknessforcats
          Your scanf()s are not correct.

          You say %f to put data into m and n which are ints. The result is a grebage value.

          On a test run I made the garbage value is so large the machine appears to hang.

          Use %d with ints.

          Did that.....And then I tried again...Now when I enter the values I get following result...

          The value of series is 1.#INF00


          I guess I will have to choose data types that are more suitable for these operations,I mean something float.Using double is also not harmful...Is it?

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Your fact() function does not work.

            I suggest you use an int argument and an int result since factorial applies only to integers.

            The factorial of 4 is 24 but your function got 49 for an answer.

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Your variable m is of type int as mentioned above so it only takes whole radians.
              There are only Pi/4 radians in 90°!

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                For optimization, I would not use fact(), or pow()

                Store variables to hold the current factorial value and the current power value. You only need 2 easy multiplications to get to the next factorial value, and another multiplication to get the current power value (can be done in the for loop).
                For added efficiency, you can simply store the term value as opposed to the factorial and power values.

                Calculate -x^2 beforehand.
                eg, for each iteration i, you multiply by -x^2/(2i)/(2i+1)

                Comment

                • abhinuke
                  New Member
                  • May 2010
                  • 23

                  #9
                  Originally posted by jkmyoung
                  For optimization, I would not use fact(), or pow()

                  Store variables to hold the current factorial value and the current power value. You only need 2 easy multiplications to get to the next factorial value, and another multiplication to get the current power value (can be done in the for loop).
                  For added efficiency, you can simply store the term value as opposed to the factorial and power values.

                  Calculate -x^2 beforehand.
                  eg, for each iteration i, you multiply by -x^2/(2i)/(2i+1)

                  Sorry but I didnt understand what you said in your reply.....do you think I should calculate powers and factorials in other functions and store these values in some variables to use them in sinx function?

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Look at the mathematical formula you provided in reply #3. jkmyoug pointed out that you can compute term i by multiplying term i-1 by -(x^2)/(2i)/(2i+1). You can speed up each iteration a little more if you precompute x^2 in a variable and use that for computing term i.

                    Comment

                    Working...