-
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
-
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