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

Not sure why this script is not working?
Hi I am new to php programing and I was trying to make up a simple script like a captcha but I canno

How to search for several parameters from objects in a database?
I have a database with lots of information about objects.
Now I would like to search for 4 or 5 p

Lining up columns nicely.
Hello everyone, I'm having problems with making columns line up properly. Here is my code:

Can I use a loop
Hi buddies!

Once again with my doubts here.

Right now I am using this sql stat

pls clear my confusion
Hi friends,

Pls solve my query .

what is the exact use of Scope Resolution Operator( i

Retreiving objects from Sessions
Hi all,

I am getting really frustrated with storing and retreiving objects from a session. Bu

defining website tags
Hi,

What would be the best way to define tags for my site, such as website title, url etc.

MYSQL gen help
This is my Mysql gen. can anyone tell me why this echos

MID(networkset.networkid, 3, 3) AS &q

Captcha problem users just refresh and it will let them auto submit again
Users will just re submit and not have to enter the new captcha.. ? Lol but if i refresh the page, t

background color imagefill
Hello

I would like to ask you why I see this square in red color just in my local xampp insta

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