I am trying to compute the Taylor series expansion for e^x at x_0 = 1. I am having a very hard time understanding what it really is I am looking for. I am pretty sure I am trying to find a decimal approximation for when e^x when x_0 = 1 is. However, when I run this code when x_0 is = 0, I get the wrong output. Which leads me to believe that I am computing this incorrectly.
Here is my class e.hpp
#ifndef E_HPP
#define E_HPP
class E
{
public:
int factorial(int n);
double computeE();
private:
int fact = 1;
int x_0 = 1;
int x = 1;
int N = 10;
double e = 2.718;
double sum = 0.0;
};
Here is my e.cpp
#include "e.hpp"
#include <cmath>
#include <iostream>
int E::factorial(int n)
{
if(n == 0) return 1;
for(int i = 1; i <= n; ++i)
{
fact = fact * i;
}
return fact;
}
double E::computeE()
{
sum = std::pow(e,x_0);
for(int i = 1; i < N; ++i)
{
sum += ((std::pow(x-x_0,i))/factorial(i));
}
return e * sum;
}
In main.cpp
#include "e.hpp"
#include <iostream>
#include <cmath>
int main()
{
E a;
std::cout << "E calculated at x_0 = 1: " << a.computeE() << std::endl;
std::cout << "E Calculated with std::exp: " << std::exp(1) << std::endl;
}
Output:
E calculated at x_0 = 1: 7.38752
E calculated with std::exp: 2.71828
When I change to x_0 = 0.
E calculated at x_0 = 0: 7.03102
E calculated with std::exp: 2.71828
What am I doing wrong? Am I implementing the Taylor Series incorrectly? Is my logic incorrect somewhere?
Yeah, your logic is incorrect somewhere.
Like Dan says, you have to reset
fact
to 1 each time you calculate the factorial. You might even make it local to thefactorial
function.In the return statement of
computeE
you are multiplying the sum bye
, which you do not need to do. The sum is already the taylor approximation of e^x.The taylor series for e^x about
0
is sum _i=0 ^i=infinity (x^i / i!), sox_0
should indeed be 0 in your program.Technically your
computeE
computes the right value forsum
when you have x_0=0, but it's kind of strange. The taylor series starts ati=0
, but you start the loop withi=1
. However, the first term of the taylor series isx^0 / 0! = 1
and you initializesum
tostd::pow(e, x_0) = std::pow(e, 0) = 1
so it works out mathematically.(Your
computeE
function also computed the right value forsum
when you hadx_0 = 1
. You initializedsum
to std::pow(e, 1) = e, and then the for loop didn't change its value at all because x - x_0 = 0.)However, as I said, in either case you don't need to multiply it by
e
in the return statement.I would change the
computeE
code to this:and set
x_0 = 0
.