Approximating Euler’s Constant From Scratch
Posted on March 17, 2008
Filed Under Math, Programming |
Or: Calculus You Will Use Either Every Day or Never
Approximating Constants
We will be calculating Euler’s constant, e, by starting off close to the correct value, and then moving closer and closer until we get to the correct value. This is known as approximation.
There are a few different ways to do this. A few are listed below:
- Find a function that evaluates to e and approximate the function.
- Rephrase the problem as a root-finding problem and find solutions via famous root-finding methods (any of Householder’s Methods, Bisection, the Secant Method, etc).
- Rephrase the problem as a fixed-point problem (I.E., find an
such that
.
I have opted to write about number 1., find an approximating function using Taylor Series. Why did I pick it? The math is easy, and helps teach basics of approximation. Taylor approximation is generally slow, and is often better used to approximate functions over intervals rather than particular constants. This is just a particular application of Taylor series.
If you have not taken a Calculus course, the material presented here may or may not be lost on you.
Taylor Series
Assume that
is continuous and infinitely differentiable. If we want to know values of
near
, we can use the formula below to determine just how many decimal places we need to use in our approximation.
Formula for a Taylor Series with infinite terms:
If
is infinitely differentiable and continuous, a Taylor Series of
about
is written:
This may look a little confusing, but once you start trying a few test functions, it gets really easy to start approximating the function.
Example:
about
The zeroth term:
= f(\alpha) = e^\alpha$[/tex]
The first term:
The result:
Error in Taylor Series
If we add up infinitely many terms, we will have an infinite degree polynomial that exactly represents the function. However, I can’t wait for my computer to calculate infinite terms, so we need to know when to stop.
Remainder of a Taylor Polynomial
In general, the error for an nth degree Taylor Polynomial of a function
about
can, for some
be written as follows:
When we are calculating how many terms we need, we need, in part, to find the maximum value of
over the area
.
Actually Calculating e
We want to (for some twisted reason) compute e using a Taylor series without the calculation being in terms of e. One way to do this is to calculate the Taylor series for 
Taylor Series for
about
:
We should also note that
, so we can go on ahead and substitute 1 for x:
Taylor Series for
about
:
Set
equal to zero. Magically, all of the es will disappear, and we will be able to evaluate e without having to use e.
Taylor Series for
about 0:
What Degree Polynomial do we Use?
We can play around with our error formula until we determine how many steps will be needed.
Simplifying Error Formula
We see that we need to determine an upper bound on the value of
between 0 and 1. An easy upper bound for us is 2.72, as we know that it is larger than e.
Final error formula
The error in our Taylor Series
about 0 is:
Naieve C++ Implementation
Please note that this code is susceptible to a few calculation errors, including mismatched-precision addition.
double calc_e(int degree) { double answer = 0; double current_factorial = 1; for(int i=0; i<degree;> { answer+=1./current_factorial; current_factorial*=(i+1); } return answer; }</degree;>
Output for various Taylor polynomials:
Taylor poly of degree 1:2 Taylor poly of degree 2:2.5 Taylor poly of degree 3:2.66666667 Taylor poly of degree 4:2.70833333 Taylor poly of degree 5:2.71666667 Taylor poly of degree 6:2.71805556 Taylor poly of degree 7:2.71825397 Taylor poly of degree 8:2.71827877 Taylor poly of degree 9:2.71828153 Taylor poly of degree 10:2.7182818 Taylor poly of degree 11:2.71828183
This gives us precision of less than
with 11 steps. Checking the error formula, we see that
is less than
, but
is greater than
, so our error is also correct.
Popularity: 18% [?]
Comments
Leave a Reply

is infinitely differentiable and continuous, a Taylor Series of 
about
= f(\alpha) = e^\alpha$[/tex]

be written as follows:
about 
about 





about 0 is: