Sum the Series for an Inverse Exponential
Corey Mutnik
PHY 305

Introduction
Code
Results
Analysis
Conclusion
References
Acknowledgements
Home

Introduction:

    For this weeks lab we had to write a program that would sum the series [1]:
                                                                                                                                                                (1)

The first way of approaching this problem was to write an equation that would generate a factorial.  Next an equation to calculate the numerator of this series was written.  Finally, the two generated equations were combined and summed over a specified number of terms.  As a way of checking the accuracy of written program for the factorial, the function lgamma() was used.  Since lgamma() returns the natural log of its input, it was necessary to use exp(lgamma()) - which returns the input raised to the power e.


Code
:

//Value of series generated from e^(-z), using summation notation

using namespace std;                         // sets up some standard names for things
#include <iostream>                        // This is  library containing input-output functions you need
#define _USE_MATH_DEFINES       // tells the compiler to define math constants (like pi) for you
#include <cstdio>                            // includes printf
#include <cmath>                            // The standard C library math.h, has lots of pre-defined math functions



//calculates factorial to be called later
float fac(int k)
{
    double i;
    double fact = 1.0;
for (i=1.0; i<=k; i++)
{
fact *= i;
}
return fact;
}

//calculate a term in the series
double expterm(int k, double z)
{
    double num;
{
num = pow(-z,k)/fac(k);             //calling fac(k) defined above
}
return num;
}

//calculate using lgamma instead of my factorial

#include <math.h>        //for lgamma function
double actterm(int k, double z)
{
    double g;
{
g = pow(-z,k)/exp(lgamma(k+1));
}
return g;
}
   


//sum e^(-z) series
int main()
{
    double ans;
    double a;
    double k;
    double z;
    double d;
    double j;
    int iterations;
    double ferr;
    double ans2;
    double ans3;
cout << "Enter desired value of z" << endl;
cin >> z;
cout << "Enter number of terms " << endl;
cin >> k;
for (j=0.0; j<=k; j++)
{
ans += expterm(j,z);                     //this calls function defined above
ans2 += actterm(k,z);

ferr = abs((exp(z)-ans)/exp(z));

iterations = j+1;
 
    printf("%9d interation(s), fractional error = %20.18f\n",iterations,ferr);    
}
//cout << "The value of this series with " << k << " terms is " << ans << endl;
//return ans;

}


Results:


Figure 1: Fractional Error as a Function of Iterations with z=5 using lgamma


Figure 2: Fractional Error as a Function of Iterations with z=10 using lgamma


Figure 3: Fractional Error as a Function of Iterations with z=15 using lgamma



Figure 4: Fractional Error as a Function of Iterations with z=20 using lgamma


Figure 5: Value of Series for Particular Number of Terms using my factorial


Figure 6: Value of Series for Particular Number of Terms using lgamma


Figure 7: Fractional Error as a Function of Iterations using my factorial (please disregard y-axis label and title)


Analysis
:

    Figures 1-3 show that the fractional error decreases as the number of iterations increases.  For higher values of z this does not always hold true.  Figure 4 shows us where the subtractive cancellation breaks down.  This occurs just past 10 iterations for z=20.  This is also shown, using my factorial program instead of lgamma, in figure 7Figure 7 has a lesser degree of accuracy since one was defined using "float" while the other was a "double". Figures 5 and 6 show how the value of the sum of the series changes with each additional term.  For larger values of z the computer has trouble calculating values since it only holds a certain number of characters when completing calculations.


Conclusion
:

    Whether using my factorial program (figure 5) or lgamma (figure 6) the same trend in the series emerges.  The variation in the value of the series changing rapidly with each additional term.  This is expected since the signs of each term alternates between positive and negative.  From this we can conclude that using a for loop code has the same level of accuracy as lgamma.  The difference comes from how you define your variables - float, double, etc.  A computer only maintains a certain amount of precision, limiting the accuracy of its outputs.  The number of characters one wants the computer to use in calculations is controlled by how variables are defined within ones code.


References:

1. Gorgam, Peter. "P305_lab2." P305_lab2. N.p., n.d. Web. 25 Jan. 2015.

2. Landau, Rubin H., and José M. Páez. Computational Physics Problem Solving with Computers. New York: Wiley, 1997. Print.


Acknowledgements:

1. Alexander, Ryan

2. Yanashiro, Brian


Home - Lab1 - Lab2