Sine Caculation/Approximation Problem?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • company1484
    New Member
    • Oct 2007
    • 5

    Sine Caculation/Approximation Problem?

    I've been given an assignment to approximate the value of sine of an angle in degrees by using the formula: sin(x) = x −x3/3!+x5/5!−x7/7!+x9/9!· · ·

    x is an angle in radians. He wants the program outline to be like so:

    1 read in a new value for angle
    2 while(angle != 0){
    3 convert angle to radians (multiply angle by pi/ 180 )
    4 sine = 0
    5 n = 1
    6 while(n ≤ 15) {
    7 calculate next term of series and add it to/subtract it from sine
    8 increment n
    9 }
    10 display sine
    11 read in a new value for angle
    12 }


    Here is what i wrote:
    Code:
    #include<math.h>
    #include<stdio.h>
     
    #define PI 3.141592653589793
     
    int main()
    {
      float angle,radian,sine,n,x,factorial,s,i;
      x = 1;
    
      printf("Enter angle in degrees (0 to quit)\n");
      scanf("%f",&angle);
     
      while (angle!=0){
          radian=angle*(PI/180);
          sine=0;
          n=1;
          while (n<=15)
        {
          s = (radian + (pow(-1,n+1))+(pow(radian, n)) / (n));
          n++;
        }
     
          printf("sin(%f)=%f\n",angle,s);
     
          printf("Enter angle in degrees (0 to quit): ");
          scanf("%f",&angle);
     
        }
      }
     
      return 0;
    }
    Im not getting the right value. I think my problem lies in the s variable. how would i write that. I made it so n would increment [n++] and it would switch from positive to negative like the formula [(pow(-1,n+1)]. It looks right. Can someone help me figure that part out? Thanks
  • mattmao
    New Member
    • Aug 2007
    • 121

    #2
    Hi.

    Just point out one thing, you need to write a method to calculate the factorial value of n.

    And the calculation should be something like this

    for (i=1; i=7; i++)
    temp = temp +pow(-1, n+1)*pow(input, 2*n+1)/factorial(n)

    I hope this would solve your problem.

    Comment

    • company1484
      New Member
      • Oct 2007
      • 5

      #3
      ok I feel kinda dumb now. Its the factorial of the denominator, lol. I changed it a little, but now when i type in an angle, it just says "press any key to continue" then it ends.

      Code:
      #include<math.h>
      #include<stdio.h>
       
      #define PI 3.141592653589793
       
      int main()
      {
        double angle,radian,s;
        int i,n,factorial,x,sin;
        x = 1;
       
        printf("Enter angle in degrees (0 to quit)");
        scanf("%d",&angle);
      
        for (i=1; i<2*n+1; i++)
        {
      	  factorial=x*i;
      
         while (angle!=0)
         {
            radian=angle*(PI/180);
            sin=0;
            n=1;
            while (n<=15)
      	 {
            s = radian+pow(-1,n+1)*pow(radian,2*n+1)/factorial*(2*n+1);
            n++;
      	  }
       
            printf("sin(%d)=%d\n",angle,s);
       
            printf("Enter angle in degrees (0 to quit): ");
            scanf("%d",&angle);
       
          }
        }

      Comment

      • company1484
        New Member
        • Oct 2007
        • 5

        #4
        im going to mess with the factorial to see if i can get it fixed. Any tips would be appreciated :)

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          I think you need to invert the for...loop calculating the factorial, and the while loop that continues your program's execution. In other words, your while loop should be the outside loop, and inside this, you should calculate the factorial - for each term.

          Comment

          • mattmao
            New Member
            • Aug 2007
            • 121

            #6
            Hi.

            I would think for the algorithm and architecture of the program before rushing into coding. Your logic looks not good...

            This is not a difficult problem to solve and here is my hint for you:

            you need to design a "stand-alone" method to calculate the fatorial value of any given integer n, outside the main method.

            And a robust method to receive user input, validate it, if it is not a valid input, ask for a new entry; if it is valid, go on to the next step.

            When you calculate the value, you can use a loop, since part of the resulting sine value is derived from a sort of series... Roughly as I mentioned before.

            Anyway, with a clear logic and a careful implementaion, this problem is not that hard to solve.

            Comment

            • company1484
              New Member
              • Oct 2007
              • 5

              #7
              I've been trying to figure this out for days. It probably is easy, but im just not getting it. Still cant figure out the factorial part.

              Comment

              • thegeneralguy
                New Member
                • Aug 2007
                • 10

                #8
                You shouldn't calculate the factorial for every term in the series. Think about what is happening --- you are dividing first by 1!, then 3!, then 5!. They differ by only 2 multiplications . Keep a running total of what the denominator should be instead.

                Comment

                Working...