Thread: taylor series expansion

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

    taylor series expansion

    Hello,

    I'm writing a program to calculate the value of e^x using taylor series expansion as my algorithm( e^x= 1 + x + (x^2)/(2!) + (x^3)/(3!)+......(x^n)/(n!)) . I think I'm going wrong somewhere within the while loop. Can you please help me out?
    Thanks

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    int main()
    {
      double xvalue, factorial, ex, nthterm, factnum, numerator, sumofterms;
      double xcount = 1;
      numerator = pow(xvalue,factnum);
      factorial *= (factnum - xcount);
      nthterm = (numerator / factorial);
    
      cout<< "This program will calculate the value\n"
          << "of e^x using the Taylor Series Expansion Method\n"
          << endl;
      cout<< "Please enter a value for 'x' for which you need to calculte"
          << endl;
      cin>> xvalue;
      cout<< endl;
    
      while(nthterm > 10E-6)
      {
        while(xcount != factnum)
        {
          factorial *= (factnum - xcount);
          factnum++;
        }
        sumofterms += nthterm;
    
    
      cout<< "This program will calculate the value\n"
          << "of e^x using the Taylor Series Expansion Method\n"
          << endl;
      cout<< "Please enter a value for 'x' for which you need to calculate"
          << endl;
      cin>> xvalue;
      cout<< endl;
    
      while(nthterm > 10E-6)
      {
        while(xcount != factnum)
        {
          factorial *= (factnum - xcount);
          factnum++;
        }
        sumofterms += nthterm;
    
      }
    
      ex = 1+xvalue+sumofterms;
      cout<< ex;
      cout<< endl;
      return 0;
    
    
    
    }


    Code tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  2. Macro expansion
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 11-02-2007, 03:08 AM
  3. Replies: 1
    Last Post: 08-02-2006, 09:45 AM
  4. expected primary expansion before ')' token.
    By agent d00nut in forum C++ Programming
    Replies: 10
    Last Post: 11-06-2005, 05:54 AM
  5. Expansion
    By Generator in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-02-2001, 05:45 AM