help on c programming involving taylor series

This is a discussion on help on c programming involving taylor series within the C Programming forums, part of the General Programming Boards category; Create a program that will approximate the error function erf(x) and erfc(x). Use the following Taylor series expansion: http://i49.photobucket.com/albums/f2...r/untitled.jpg Code: ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    5

    Question help on c programming involving taylor series

    Create a program that will approximate the error function erf(x) and erfc(x). Use the following Taylor series expansion:


    http://i49.photobucket.com/albums/f2...r/untitled.jpg

    Code:
    Restrictions:
    1. Use at most 6 variables in the main function.
    2. Create a separate function for power using recursion.
    3. Create a separate function for factorial using iteration.
    4. Calculate the approximation until the 100th term of the Taylor series and print out approximation up to 4 decimal places.
    
    *I have accomplished numbers 2 and 3.
    2.)
    int power(int b, int e){
        if(b==0 && e==0){
                printf("Invalid.\n");
                main();
                }
        if(e==1){
                 return b;
                 }
        if(e==0){
                 return 1;
                 }
        else{
             return b*power(b,e-1);
             }
    }
    
    3.)
    double factorial(double value){
        int ctr;
        double fact;
        if(fact<0){
                   printf("Invalid.\n");
                   main();
                   }
        else{
        fact=1;
        for(ctr=1;ctr<=value;ctr++){
                                   fact*=ctr;
                                   }
        return fact;
        }
    }
    
    
    ***Any help will be appreciated. Thanks!
    Last edited by harlow23; 05-04-2007 at 08:40 PM. Reason: switched 2 and 3

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You switched 2) and 3). The power function should use a floating-point type for b and its return value. The factorial should use an integer type for its argument and return value.

    Edit: Also, you shouldn't call main() explicitly from within the program. It should only be called automatically when the program starts. There's no reason to anyway since you're only calling these functions from within your own program so you have control over the arguments and can ensure that they are valid, so you don't really need to check them.
    Last edited by robatino; 05-04-2007 at 08:32 PM.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    5
    thanks for the response. i understand. but now my problem now is how to program that taylor series expansion. it's infinite..

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    As I read the specification, it says to calculate the sum of the first 100 terms of the series, and then print (to 4 decimal places) the resulting number. Seems clear enough. (If you had been asked to determine the number of terms necessary to get a given level of accuracy, that would be significantly harder.)

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    5
    oh yeah, so that would mean n=0 to n=100. basically the user has to enter the value of x.
    thanks!
    Last edited by harlow23; 05-04-2007 at 10:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2009, 02:35 AM
  2. Any tips on pitfalls involving STL's string?
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2008, 02:33 PM
  3. error involving char type
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 05-18-2007, 05:54 PM
  4. Inheritance involving data structures?
    By AusTex in forum C++ Programming
    Replies: 0
    Last Post: 06-12-2005, 10:28 PM
  5. Question involving using math operations...
    By Screwz Luse in forum C Programming
    Replies: 6
    Last Post: 12-04-2001, 04:20 PM

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