Maclaurins Series

This is a discussion on Maclaurins Series within the C Programming forums, part of the General Programming Boards category; Whats wrong with the following code to find maclaurin series? Code: #include <stdio.h> #include <math.h> #define TRUE 1 #define FALSE ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Maclaurins Series

    Whats wrong with the following code to find maclaurin series?

    Code:
    #include <stdio.h>
    #include <math.h>
    #define TRUE 1
    #define FALSE 0
    
    int main ()
    {
        int x, n, i;
             double approx = 0;
    	/* Write your code here */
    	scanf("%d", &x);
           scanf("%d", &n);
           printf("%d", x);
           printf("%d", n);
           while (n <= 1)
     {   approx = pow(x, n);
          approx += approx;
          n++;
    }	
    while ( n > 1)
      {   		i = n;
            	while ( i > 0)
            {
               approx = (pow(x, n) * 1.0) / (i);
               approx += approx;
            	i--;
    }     
         
    }
    printf("%.10lf\n", approx);
    return 0;
    }
    Last edited by Salem; 10-05-2010 at 11:15 AM. Reason: Added code tags - learn to use them yourself

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What's wrong with using code tags around your code? It's hard to study code that looks like html text.

    Also, you really should tell us WHAT your program is doing wrong. If you want help, respect that we need you to save us some time trying to zero in on the problem(s).

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Vancouver
    Posts
    5
    Is the code not compiling or is the output incorrect?

    -----------------------------

    No advance in wealth, no softening of manners, no reform or revolution has ever brought human equality a millimeter nearer.
    - George Orwell

    Lainoox

  4. #4
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Ok, from a very quick initial look here are potential problems I've found.

    1) I am assuming that the variable 'n' refers to the degree you want to take the Maclaurin Series to (i.e. the level of the approximation. As n -> infinity, the error tends to zero). In that case, why is it that if you put in a value for n which is anything other than 0 or 1 the first while loop is completely skipped? What is the point in that?

    i.e. This line of code is questionable
    Code:
    while(n <= 1 )
    2) Even more interestingly, in the second loop you have two loops inside one another and for the terminating condition of the first loop you have n > 1 although no 'n--' statement anywhere in either loop - How exactly does the loop ever terminate if it is initially executed?

    3) In the central loop you have these two lines:
    Code:
    approx = (pow(x, n) * 1.0) / (i);
    approx += approx;
    That is completely equivilent to this line of code:
    Code:
    approx = 2*pow(x,n)/i;
    Because you are saying approx = [some crap], and then saying approx = approx + [same crap again].

    4) By saying approx = [some_number] you complete lose the value it was before which is obviously causing you problems in your program.

    5) This isn't a problem, although a remark - Why do you bother having two loops inside each other when one would work fine? It would make things far easier and not to mention easier to write.

    6) Again, another remark. Ever thought about doing something like this:
    Code:
    double FunctionTerm(double x, double n)
    {
           /* Function to return the n-th term of a function based on the position, x and the term number */
    
          /* E.g. for sin(x) this would be something like this */
          double val = 0.0;
          val = (pow(-1, n)/Factorial(2*n+1))*pow(x, 2*n+1);
          return val;
    }
    
    int main()
    {
           /* Do loop here */
          int i = 0;
          for(i=0; i <=n; i++)
          {
                 /* You can do this bit */
          }
         return 0;
    }
    You would have to write the 'Factorial' function and do all the for loop stuff, but you catch my drift. The reason it is better to structure it like that is that if you want to approximate another function all you have 2 do is change the FunctionTerm function so that it gives the value of each term for a given x and n.

    Btw, I'm still not sure what function you were trying to approximate.

    Edit: I'm almost certain there are more problems than I've pointed out, but the ones above are the obvious ones.

    I know it has already been said, but from now on, please use code tags. lol
    Last edited by Swarvy; 10-04-2010 at 03:47 PM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    here is what the code is for:

    To calculate maclaurin series for e^x which is given by the formula:

    (x^n)/n!=1+x+ (x^2) /2! + (x^3)/3! +....

    n is the number of approximations used. the reason i used n<=1 loop is because for n<=1 the equation has to sum up only 1 and the number ( x )..

    I am new to programming and hence finding it difficult to understand the tricks.. the code compiles well.. there is no error.. but after i enter the two values for x and n in the program it stops and nothing happens.. thanks for your help.

    one more thing, is there any problem with the power function in this code?

  6. #6
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    The reason the program does nothing after you enter a value is because it gets stuck in an infinite loop (as I said in my last post). One of the reasons why it gets stuck in an infinite loop is because you have a while loop which does not decrease the value of 'n' for each iteration.

    The formula you gave in your last statement isn't right. I think what you meant was this:
    e^x = 1 + x + (x^2) /2! + (x^3)/3! +....+(x^n)/n!

    I.e. In your program you should loop over 'n' (as you have done. I personally would use a for loop instead of a while loop, but ultimately it doesn't matter which one you use). What you want is a function/technique for working out the value of each term. Something like this would work better than what you have (assuming you really don't want to break the technique up into functions):

    Code:
    int i=0;
    double sum = 0;
    int val;
    
    while(i <= n )
    {
          j = 1;
          val = j;
          while( j < i )
          {
                  val *= j;
                  j++;
          }
          sum += pow(x, i)*val;
          i++;
    }
    One of the reasons we use functions is because the code can get quite messy if you don't try to break it up. The inner loop calculates the factorial, but using functions and recursion this is far easier than the method above. By not breaking the technique up you are only making life harder for yourself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining the value of a term in a series
    By jbour12 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2010, 09:28 AM
  2. Series and Parallel Circuit Calculator
    By LOBH in forum C Programming
    Replies: 1
    Last Post: 11-22-2009, 12:06 PM
  3. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 01:30 AM
  4. assembly for M8 microprocessor series
    By GanglyLamb in forum Tech Board
    Replies: 3
    Last Post: 03-15-2006, 04:34 PM
  5. Problem with implementing Liebniz series
    By C_Necessity in forum C Programming
    Replies: 6
    Last Post: 06-15-2005, 12:39 PM

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