Taylor Series Function


Posted on 16th Feb 2014 07:03 pm by admin

So I am working a a project for my intro C++ class, and I am having some trouble with a function for approximating exp(x) using a taylor series. The taylor series approximations for exp(x) is: 1+x+((x^2)/2!)+((x^3)/3!+...., and I am supposed to approximate to "n" number of terms.

Thus far, my function is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
double fact(int c) { int factorial=1; for ( int i = 1; i <= c; i++ ) { factorial *=i; } return factorial; } double taylor(double x, int n) { double approx; double sum; double value; for (int i=1; i<=n; i++) { int factorial=1; factorial*=i; sum=((pow(x,i))/fact(i)); value+=sum; } approx = 1+value; return approx;

Now this does work if I just run it by itself, i.e. just do something like "cout << taylor(x,n)". But whenever I call this function within another function, it does not work! It is kinda hard to explain, but anyone have any ideas?

Thanks

No comments posted yet

Your Answer:

Login to answer
197 Like 49 Dislike
Previous forums Next forums
Other forums

how make 2 column in table with data tybe long row
hi every one

I want make in my table 2 column with data tybe long raw

how thi

GIS appliction help
I found some tutorials in the internet to develop a map application I don't want to use google maps

Validation
Hello

I know this is a really vag question.

I need to have a input field on a form th

Please help - should be a simple fix.. driving me nuts
Everything seemed to be working fine. I have a table, it alphabetically lists a bunch of cities and

getAlexaRank($url) function not working
I have made a function to get alexa rank
the site is here: http://mytestsite.rack111.com/1

How to sum these output values
Hi

I need sum the month totals
SELECT region_name, area_name ,SUM (CASE WHEN S

PHP and Javascript
Hello Everyone,

I have a page that needs a javascript code to be written between PHP code but

New to PHP and just trying to understand a little code.
I hope I'm not annoying anyone or breaking the rules but I was wondering about this bit of code righ

Passing Variables
What I'm wanting to do is when the user clicks on Arenas it will know the name of the country it's w

Problem with shopcart code
Hello, I am having a bit of trouble being able to add a product to my shopcart. My mysql database i

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash