Please Help with e^x estimating program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • intro2Cpp
    New Member
    • Oct 2006
    • 3

    Please Help with e^x estimating program

    Hello,

    I have been trying to figure out what I'm doing wrong on my program for two days. My professor keeps "forgetting " about me. It is due tomorrow. We are supposed to use the "summing estimator" to calculate an estimated value for exp(x) using the formula 1+ x^n/n! for a user entered value for x, and values for n from 1 to 100. I know my problem is in the logic of how to implement the formula in C++ because it compiles but gives the wrong values. I can't figure it out. Please help. Here is my program so far:

    //
    //
    // This program will take an entered value 'x'
    // and run an estimated calculation for e^x and
    // also a calculation based on existing c++ exponent
    // functions.
    //
    #include <iostream>
    #include <cmath>
    #include <iomanip>

    using namespace std;

    double x; // Value entered by the user
    double factorial(doubl e n);
    double sum (double factors);

    int main()
    {

    double estimate;

    cout << setiosflags(ios ::fixed) << setprecision(5) ;

    cout << "Enter a value for 'x' =>";
    cin >> x;

    estimate = 1 + sum(x);

    cout << "The estimated value for pi is " << estimate << ".\n";

    return 0;
    }

    // Function calculates n!

    double factorial (double n)
    {
    double ans;
    if (n<=1)
    ans = 1;
    else
    ans = n * factorial(n-1);
    return ans;
    }

    double sum (double factors)
    {
    double sum;
    for (double n=1; n<=100; n++)
    sum = pow(x,n)/factorial(x);

    return sum;
    }
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    Code:
    double sum (double factors)
    {
            double sum;
            for (double n=1; n<=100; n++)
            [b]sum = pow(x,n)/factorial(x);[/b]
    
    return sum;
    }
    should be
    Code:
    sum += pow(x,n) / factorial(x);
    Also, the first term of the series is 1, so you could start the loop at 0 instead of assigning
    Code:
    estimate = 1 + sum(x);

    Comment

    • intro2Cpp
      New Member
      • Oct 2006
      • 3

      #3
      Originally posted by D_C
      Code:
      double sum (double factors)
      {
              double sum;
              for (double n=1; n<=100; n++)
              [b]sum = pow(x,n)/factorial(x);[/b]
      
      return sum;
      }
      should be
      Code:
      sum += pow(x,n) / factorial(x);
      Also, the first term of the series is 1, so you could start the loop at 0 instead of assigning
      Code:
      estimate = 1 + sum(x);

      THANK YOU SO VERY MUCH! I will enter that in and give it a whirl!

      Comment

      • intro2Cpp
        New Member
        • Oct 2006
        • 3

        #4
        Originally posted by intro2Cpp
        THANK YOU SO VERY MUCH! I will enter that in and give it a whirl!

        Okay, I did that, but now all it will return no matter what I enter as the 'x' value is 1.000. That would normally tell me there is an integer division, but I intentionally used 'double' type for everything. Please help???

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          The formula is summation (x^n)/n! and you are calculating summation (x^n)/x!. Change your parameter for factorial from x to n. You could also do an iterative version, it would be much faster.
          Code:
          #include <iostream>
          #include <iomanip>
          
          using namespace std;
          
          
          int main()
          {
              cout << setiosflags(ios::fixed) << setprecision(5);
          
              double x;
              cout << "Enter a value for 'x' =>";
              cin >> x;
          
              double estimate = 0;
              double exp = 1;
              double fact = 1;
              double r;
              int n = 1;
          
              do
              {
                  r = exp / fact;
                  estimate += r;
          
                  exp *= x; // x^n
                  fact *= n++; // n!
              }   while(r > 10e-6);
          
              cout << "The estimated value for pi is " << estimate << ".\n";
          
              system("PAUSE");
              return EXIT_SUCCESS;
          }

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by D_C
            You could also do an iterative version, it would be much faster.
            Are you sure about that? can you provide references.

            I only ask becasue to call a function requires a branch and the creation of a stack frame, where as a loop requires a branch and possibly the additional increment of a variable so it would seem to me that looping would be faster.

            Comment

            • tyreld
              New Member
              • Sep 2006
              • 144

              #7
              Originally posted by Banfa
              Are you sure about that? can you provide references.

              I only ask becasue to call a function requires a branch and the creation of a stack frame, where as a loop requires a branch and possibly the additional increment of a variable so it would seem to me that looping would be faster.
              I suspect you have misread iterative as recursive. The factorial function is currently implemented recursively. Changing it to a loop (ie. iterative implementation) would make it faster. Leaving you both in agreement. ;)

              Comment

              Working...