Calculating the sine of a number from the Taylor sum

This is a discussion on Calculating the sine of a number from the Taylor sum within the C Programming forums, part of the General Programming Boards category; I'm trying to write code to basically mimic the sum to infinity expression, Sine - Wikipedia, the free encyclopedia , ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    9

    Calculating the sine of a number from the Taylor sum

    I'm trying to write code to basically mimic the sum to infinity expression, Sine - Wikipedia, the free encyclopedia, but with a finite number of terms, and have the user input the number of which the sine is to be calculated.

    This is what I've done so far and I'm stuck now, I dont know where to go and it's not working for me... Any help is appreciated.
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    
    
    int main()
    
    
    { double i,n,x, factorial, sinx;
        printf("Enter angle x \n");
        scanf("%lf", &x);
        
        factorial = 1;
        for(i=0; i<((2*n)+1); i++)
            {
                factorial=(factorial)*(i+1);
            }
        
        for (n=0; n<100; n++) 
            {    
                sinx = (pow(-1,n)*pow(x,(2*n)+1))/((2*n+1)*factorial);
            }
            printf("sinx = %f\n", sinx);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Here's one problem:
    Code:
    $ gcc -Wall sine.c -lm
    sine.c: In function ‘main’:
    sine.c:14: warning: ‘n’ is used uninitialized in this function
    You can't use n before it has a value. Right now, you calculate factorial once in the entire program, and you calculate it for a bogus value of n. Move that code into it's own function, so you pass in a number n and it returns n!.
    Code:
    double factorial(double n)
    {
        // calculate n! and return it
    }
    Then, you need to actually calculate the sum of all these terms. Initialize sinx to 0, then do
    Code:
    sinx += (pow(-1, n) * pow(x, (2 * n) + 1) / (factorial((2 * n) + 1);
    Note the use of += instead of just =. That adds the calculation to the running total each time through the loop. Similarly, you can use *= in calculating the factorial.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    What do you mean by 'move that code into its own function'? I understand everything else you said, thanks

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    You know what a function is, right? main is one function, that you wrote. pow is another function that you just use. You need to write a function called factorial. I gave you the shell in my previous post (the second code snippet). You need to fill in what goes between the curly braces. The loop you currently use to calculate factorial needs to be moved inside those curly braces, and modified slightly. If you need more help with functions, read our tutorial here, and Google for some more. Read any class notes and textbooks you have too.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    Ah yeah I get that, but I dont understand how I need to modify my loop?

    I put it all together and changed factorial=(factorial).. to factorial*=(factorial)..., and I get the error 'lvalue required as left operand of assignment' on that line, Which I have looked up and don't really understand what it means...
    Last edited by meadmead; 10-31-2011 at 05:02 PM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Generally that lvalue error means you're trying to assign a value to something that doesn't allow assignment (string literal, function name, a whole array, etc). Post your new code so we can see it.

    The loop modification I was talking about was that you no longer want your loop to go to (2*n)+1. You should always code in small chunks and test as you go. That makes it easy to track the source of your problems. Test your factorial function all by itself before you go on with the full calculation of the series:
    Code:
    printf("0! = %d\n", factorial(0));
    printf("1! = %d\n", factorial(1));
    printf("5! = %d\n", factorial(5));
    That should give you the outputs 1, 1 and 120. If not, you know your factorial calculation is wrong. If that works, and your final output is wrong, you can at least be sure the problem isn't in your factorial code.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    this is what I have now
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    
    
    int main()
    
    
    { double i,n,x,sinx;
        printf("Enter angle x \n");
        scanf("%lf", &x);
        
        double factorial(double n);
        {
            for(i=0; i<(2*n); i++)
            {
                factorial*=factorial*(i+1);
            }
        
        }
        sinx = 0;
        for (n=0; n<50; n++) 
            {    
                sinx += pow(-1,n)*pow(x,(2*n)+1)/factorial((2*n)+1);
            }
            printf("sinx = %f\n", sinx);
    return 0;
    }
    which now gives me the error 'invalid operands to binary ('have double(*)(double) and double')' on the same line...

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    You have a few errors:
    • You can't put one funciton inside another. You need to move your factorial function above main.
    • The error you ask about is probably because you're using factorial (the name of the function) as a variable. You can't assign to the name of a function in C like you can in Pascal. You need to declare a variable for holding the results of your factorial calculations: double fact; Make sure you initialize it before use, probably to 1.
    • x *= y is the same as x = x * y, so factorial *= factorial*(i+1) is like saying factorial = factorial * factorial * (i+1). Notice the extra 'factorial'. You just need fact *= i+1.
    • Your loop condition is still wrong for factorial. Don't guess at what it should be. Think about how you calculate a factorial, and think about what your loop condition should be. If I ask you for 5!, do you multiply all the numbers up to 2*5 = 10? No, you only go to 5.
    • You need to return a value from your function when you're done: return fact;

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    9
    Fixed it thanks a lot for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help calculating number of spaces with tabs
    By Programmer_P in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2011, 05:04 PM
  2. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 11:06 AM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  4. Replies: 8
    Last Post: 02-23-2002, 08:50 PM
  5. Calculating a BIG number
    By Robert_Ingleby in forum C Programming
    Replies: 19
    Last Post: 01-10-2002, 07:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21