Pi and e to x approximation

Im supposed to write an e^x approximation using a taylor series (link to taylor series pdf http://people.math.sc.edu/girardi/m142/handouts/10sTaylorPolySeries.pdf) and im having some trouble. I was given a pi approximation program to help but I don't really understand the code. Please explain to me the pi code and tell me if im anywhere on track for the e^x code. I don't think I am but I just need someone to look at it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  Pi Approximation code
#include <iostream>
#include <math.h>
#include <cmath>

using namespace std;


int main() {

    double n = 1;
    int i = 1;
    bool add = false;
    double sum = 0;
    double oldsum = 0;

    do {

        oldsum = sum;

        if(i%2 != 0) sum += (1/n);
        if(i%2 == 0) sum -= (1/n); // set sum as 1

        n += 2; // add 2 to n for taylor series
        i++; // add one to term number

    } while(  abs((4.0)*sum - (4.0)*oldsum) > .00000001  );

    cout << "Term # " << i << '\t' << "pi approx: " << (4.0)*sum << '\n';


    return 0;


}
My code so far
#include <iostream>
#include <math.h>
#inlcude <cmath>

using namespace std;

int main() {

    double n = 0;
    int i = 0; // term number
    bool add = false;
    double sum = 2;
    double oldsum = 0;
    double x;

    cout << "Input x:";
    cin >> x;
    1 + x;


    do {
        double r = (x / !(sum));
        sum++;
        i++;

    }while (  r > .0000001);

    cout << "Term number:"<< i <<'\t' << r;


return 0;
}

Please help thank you!
closed account (48bpfSEw)
I like PI and I want to thank you for publishing the taylor serie. I never heard about therefore I can help you partially...

The first function of approximation of pi is the Gregory–Leibniz series
https://en.wikipedia.org/wiki/Approximations_of_%CF%80

You'll understand it better when you see the mathematic formula.




https://de.wikipedia.org/wiki/Taylorreihe
see: Exponentialfunktionen und Logarithmen

The formula is simple:

f(x) = 1 + x/1! + x²/2! + x³+3! + ...


first you need a function for faktultät (!)

PseudoCode:
1
2
3
  double sum=1; 
  for (int x=1;x<10;x++)
    sum += power(x,x+1) / faculty(i);

Last edited on
ok well i just found out I'm calculating e^1 so i dint need an input. So i hour use a for loop instead of a dowhile? Sorry I'm very new to programming. And i would put the fact(i) not! ?
closed account (48bpfSEw)
I didn't realy understand you. If you like you can write your question in your native language.

If you mean what fact(i) is:

fact(1) = 1
fact(2) = 1*2 = 2;
fact(3) = 1*2*3 = 6;
etc.

a simple function (without plausiblity check) for "fact" is this:

1
2
3
4
5
6
7

int fact(int val) {
  int p = 1;
  for (int i=1;i<=val;i++) 
    p *= i;
  return p;
}


so far I understood your task is to write a function which calculates e with the taylor serie.
Last edited on
yeah i just need to write a function that calculates e with the taylor series. Ive already got a working factorial function.
actually nevermind I got it!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <math.h>
#include <cmath>

using namespace std;

double fact(double c);
//returns factorial of a number

int main() {

    int i = 1;   // term number
    int n = 0;  // exponent and divisor number
    double sum = 0;
    double oldsum = 0;

    do {
        oldsum = sum;

        sum += pow(1, n)/fact(n);
        n++;
        i++;


    } while( sum - oldsum > .0000001);

    cout << "Term #:" << i << '\t' << "Approximation:" << sum;


	return 0;
}

double fact(double c) {
    if( c == 0) return 1;

    else {
        double m = 1;
        while(c > 0) {
            m *= c;
            c--;

        }
        return m;
        }
}
closed account (48bpfSEw)
congratulations, good job! ^^
I see the task is completed, and that is fine. Still, there's one thing I would suggest as a challenge. Try doing the calculation of e without making use of either the pow() or fact() functions. It can be done with very small amount of calculations, since each factorial is derived from the previous with a single multiplication. The same applies to the power, each can be derived from the previous with just a single multiply.

Don't worry about this though, it's only an idea for those who are interested in trying it.
Thank you i wanna try that cause i really am interested in programming and wanna get better cause im not very good now. But i did think about this before i used those functions.
Topic archived. No new replies allowed.