#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    15
    Rep Power
    0

    Prog, for computing maclaurin series, need help...


    hi there, i made this program to calculate the estimated value using the maclaurin series. can someone please tell me where im wrong, i know im wrong so, and i typed it up knowing so hoping to find a direction, anyways thanks in advance

    it must print to a table like so

    x = vvvv
    Number of terms Estimated value exp( x) Difference
    1 xxxx xxxx xxxx
    2 xxxx xxxx xxxx
    3 xxxx xxxx xxxx
    ...
    numTerms xxxx xxxx xxxx



    PHP Code:
    #include <stdio.h>
    #include <math.h>
    #define OUTFILENAME "exponentialFn.txt"

    int main(void)
    {
        
    /* Variables */
        
    int numTermsnkterm;
        
    float esteXXdiff;
        
    long factorial(int k);
        
    FILE *outfile;
       
        
    outfile fopen(OUTFILENAME"w");
        
        
    /* Input */
        
    printf("Enter the number of terms:");
        
    scanf("%d", &numTerms);
        
    printf("Enter the value of X:");
        
    scanf("%f", &X);
        
        
    /* Term */
        
    for(0numTermsk++)
        {
            
    term k;
            
    scanf("%d", &term);
        }
            
        
    /* Compute e^x */
        
    for(0numTermsk++)
        {
            
    eX expf(X);
            
    scanf("%f", &eX);
        }    
        
        
    /* Compute Estimate */
        
    for(0numTermsk++)
        {   
            
    est pow(X,(k))/factorial(k) + pow(X,(k-1))/factorial(k-1);
            
    scanf("%f", &est);
        }
        
        
    /* Compute the diffance */
        
    for(0numTermsk++)
        {
            
    diff =(pow(X,(k))/factorial(k) + pow(X,(k-1))/factorial(k-1)) - expf(X);
            if(
    diff 0)
            {  
                
    diff diff*(-1.0);
            }
            
    scanf("%f", &diff);
        }
        
        
    /* Print computations to a .txt table */
        
    fprintf(outfile,"%f\n"X);
        
    fprintf(outfile,"Number of terms | Estimated value | exp( x) | Difference\n");
        
    fprintf(outfile,"%d | %f | %f | %f\n"termesteXdiff);         
         
        
    system("PAUSE");
        return 
    0;
        
    }    
    /*----------------------------------------------------------------------------*/
    /*This function computes the factorial value*/

    long factorial(int n)
    {
        if (
    == 0)
            return 
    1;
        else
            return 
    factorial(1);
    }

    /*----------------------------------------------------------------------------*/ 
  2. #2
  3. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    It helps a lot to tell your potential respondents what errors are occurring or how the code isn't performing as expected. Very few of us here are psychic; I am, but my crystal is in the shop for 'ball' joints, so I'd just have to wing it, examining your code and trying to read your mind. I will confidently predict that I can sit down at the keyboard and break your program, though.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    15
    Rep Power
    0
    Originally Posted by DaWei_M
    It helps a lot to tell your potential respondents what errors are occurring or how the code isn't performing as expected. Very few of us here are psychic; I am, but my crystal is in the shop for 'ball' joints, so I'd just have to wing it, examining your code and trying to read your mind. I will confidently predict that I can sit down at the keyboard and break your program, though.
    haha, ya okay, i changed the setup a bit. the problem is, it doesnt run... no okay, when i run it, it runs the first input statements then it just closes. it prints out nothing to the text file. the maclaurin series is ex = 1 + x1 / 1! + x2 / 2! + x3 / 3!... so i think i got that covered in the "/* Compute Estimate */" statement. im not to sure why its not working...

    PHP Code:
    #include <stdio.h> 
    #include <math.h> 
    #define OUTFILENAME "exponentialFn.txt" 

    int main(void)
    {
        
    /* Variables */
        
    int numTermsnkterm;
        
    float esteXXdiff;
        
    long factorial(int k);
        
    FILE *outfile;
       
        
    outfile fopen(OUTFILENAME"w");
        
        
    /* Input */
        
    printf("Enter the number of terms:");
        
    scanf("%d", &numTerms);
        
    printf("Enter the value of X:");
        
    scanf("%f", &X);
        
        
        for(
    0numTermsk++)
        {    
            
    /* Term */
            
    term 1;
            
            
    /* Compute e^x */
            
    eX expf(X);
            
            
    /* Compute Estimate */
            
    est pow(X,(k))/factorial(k) + pow(X,(k-1))/factorial(k-1);
            
            
    /* Compute the diffance */
            
    diff =(pow(X,(k))/factorial(k) + pow(X,(k-1))/factorial(k-1)) - expf(X);
            if(
    diff 0)
            {  
                
    diff diff*(-1.0);
            }
            
            
    /* Print computations to a .txt table */
        
    fprintf(outfile,"%f\n"X);
        
    fprintf(outfile,"Number of terms | Estimated value | exp(x) | Difference\n");
        
    fprintf(outfile,"%d | %f | %f | %f\n"termesteXdiff);
            
        }
         
        
    system("PAUSE");
        return 
    0;
        
    }    
    /*----------------------------------------------------------------------------*/
    /*This function computes the factorial value*/

    long factorial(int n)
    {
        if (
    == 0)
            return 
    1;
        else
            return 
    factorial(1);
    }

    /*----------------------------------------------------------------------------*/ 
  6. #4
  7. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    Code:
    for(k = 0; k < numTerms; k++)
        {    
            /* Term */
            term = k = 1;   **** YOU JUST BLEW YOUR 'K' CONTROL RIGHT HERE *****
    If you want to know what I meant by breaking your program, just enter the letter, 'A', at one of your first couple of prompts. Then read about scanf (or see the post, COMMONLY ASKED QUESTIONS) to see how to deal with human users that make typos, or with malicious users that snicker if they can make it go south while you're being evaluated for a grade or a raise.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Adelaide, Australia
    Posts
    880
    Rep Power
    11
    I'm not sure what all those scanf's are for - scanf is for user input, normally, but you seem to be overwriting the contents of your calculations with it.

    I think you just want one loop, for each term. Then you can output the values as you go. You probably don't need to calculate expf in each iteration either, as it won't change.
    In pseudo code
    Code:
      Get user input
      write the output header
      calculate eX
      for (term = 0 term < numTerms; term++)
      {
        calculate estX using term
        diff = abs(extX - eX)
        fprintf the output to the file
      }
      close the output file
    Edit: Bah, thats what I get for not refreshing before I post. Move along, nothing to see here. As you were.
    Last edited by The Dark; November 14th, 2004 at 08:07 PM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    15
    Rep Power
    0
    DaWei_M-I actually spotted that but after I posted. Thanks though

    So with what i have the terms and eX collum returns a value if i take out the "/* Compute Estimate */" and "/* Compute the difference */" statements. if i put either in the program does nothing. i think ive narrowed it down, so what could be wrong with those two statements and how they affect the code?
  12. #7
  13. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    By "does nothing" do you mean it gives no sign of output or error message or dialog of any kind, just goes away? Until you do what? Ctrl-C? Reboot? Kick the tar out of the tower? We still can't see your code, you know; you've changed it in at least one way. Is that all? You're being hell on wheels to help.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Adelaide, Australia
    Posts
    880
    Rep Power
    11
    Your factorial function does not handle negative numbers as its input, yet you are passing k-1 to it when k is zero. This will cause it to loop approximately 4 billion times (on a 32 bit machine).
  16. #9
  17. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    I envy you your spotting ability, Dark, but do you think that'd slow it down? :D
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    15
    Rep Power
    0
    Originally Posted by DaWei_M
    By "does nothing" do you mean it gives no sign of output or error message or dialog of any kind, just goes away? Until you do what? Ctrl-C? Reboot? Kick the tar out of the tower? We still can't see your code, you know; you've changed it in at least one way. Is that all? You're being hell on wheels to help.
    haha, ya just goes away then i kick the tar out of the tower! okay, it prompts me for the first two printfs, then it just closes and nothing is printed to the file. if i get rid of two of the statements mentioned, i get the "press any key to continue" thing whatever it is, and it prints out the terms collum and the eX value to my text file. i dont know what to say, does nothing obvious stand out as being wrong? i cant imagine thats the case. well im troubled by this, im sure its easy, i dont know what to do though... thanks
  20. #11
  21. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3314
    Read The Dark's post about the looping....
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Help......


    Please can someone help me with this C Programming question...

    Write a C function mysin (double x, int n) which calculates the Maclaurin series for
    sin(x) up to power 2n + 1, i.e.

    x - x^3/3! + x^5/5! -... + ... + (-1)^n *[x^(2n+1)]/(2n + 1)!

    Suppose we have x = 1:0. Use the mysin function in a main program to produce a table
    similar to the following (15:10 format for all doubles):

    n approx exact approx-exact
    0 1.0000000000 0.8414709848 0.1585290152
    1 0.8333333333 0.8414709848 -0.0081376515
    2 0.8416666667 0.8414709848 0.0001956819
    3 0.8414682540 0.8414709848 -0.0000027308
    4 0.8414710097 0.8414709848 0.0000000249
    5 0.8414709846 0.8414709848 -0.0000000002
    6 0.8414709848 0.8414709848 0.0000000000
    7 0.8414709848 0.8414709848 -0.0000000000
    8 0.8414709848 0.8414709848 0.0000000000
    9 0.8414709848 0.8414709848 0.0000000000
    10 0.8414709848 0.8414709848 0.0000000000

    Thank You
  24. #13
  25. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    First, I think you should limit the range of x.
    Use mod(x,TAU) where TAU=2 pi to find the quadrant.
    Cut the range further using
    mod(x,TAU/8)
    Now compute the Taylor series on that value,
    use trigonometric identities (double angle formulas and the sign from quadrant) to figure out the answer.

    In computing the Taylor series you should combine the small magnitude terms first, which you'll be able to predict given that you cut the domain to [0,TAU/8)

    Subtraction is a real problem because it throws away the significant information.

    Has anyone got thoughts about
    A = sum(positive terms sorted low to hi)
    B = sum(abs(negative terms sorted low to hi))
    A - B

    or is there a better way to compute the series?
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo