Taylor series

I am extremely lost on how to write this code and would appreciate any and all advice for it. These are the instructions I was given:

"Create a new source file called taylorSeries.cpp so you can create and test a Taylor Series version of the Sine(x) function. Call your function mySine(x) which takes a double as a parameter and returns a double as a result.
The input to this function is in radians.

Implement the Taylor Series for the Sine function using a loop. The mySine() function compares the previous calculated Sine to the current calculated value of Sine. Once the difference between these two values is within the error (0.0001) indicated in the specification the value should be returned to the calling function.
Note: Make sure to create a function prototype for mySine() and place it above main()"

Then I'm supposed to do the same for other trig functions. In the end I'm expected to have an output table like this: https://i.gyazo.com/8cc08111ddedc8533165d9ec06134aac.png

I have no idea how to do the taylor series, nor how to make it into a huge table like that. All I've managed to write is code for converting degrees to radians, and how to do factorials. I REALLY need help.

This is my weak attempt so far:
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
  #include <iostream>
#include <cmath>

double mySine(double x);
double factorial (int f);
double sine(double x);
double cosine(double x);
const double PI = atan(1.0)*4.0;

using namespace std;

int main(){
  double x= 0.0;
  double angle_in_radians;
  double angle_in_degrees;
  angle_in_radians = angle_in_degrees * PI/180.0;
  while (x < 0.7853){
    x = x + 0.261799;
    angle_in_degrees = x * (180/PI);
  }
  cout << endl;
  double factorial(int x);
  double f = 1.0;
  for (int i = 1; i <= x; i++){
    f = f * i;
  }
  return f;
  }
  double sine (double x){
  double sum_positive = 0.0;
  double sum_negative = 0.0;
  double output = 0.0;
  double angle_in_degrees;
  for (int i = 1; i <= 1000; i += 4){
    sum_positive = sum_positive + factorial (i);
  }
  for (int i = 3; i <= 1000; i += 4){
    sum_negative = sum_negative + factorial (i);
  }
  output = (sum_positive - sum_negative);
  return output;
  return angle_in_degrees = x * (180/PI);
}

I always advise people to not use a separate function for calculating the factorial. Though that is a valid approach, there is another way of looking at the series which makes it unnecessary. That is, each term in the series can be derived from the previous one by one multiplication and one or two divide operations (depending on which function one is calculating).

Take for example ex
the series is
1 + x + x2/2! + x3/3! + x4/4! ... etc.

My suggestion is to look at it like this:
1st term is 1
2nd term is [1st term] * x / 1
3rd term is [2nd term] * x / 2
4th term is [3rd term] * x / 3
...

Then to calculate the function exp(x), just add up the sum of the first n terms of the series. In this case you will need to stop when the latest sum is close enough to the previous sum, within your specified limit.

I mentioned exp(x) here, the other functions sin() and cos() are very similar.

I would do those two first (sin and cos), get the basic design up and running before considering the rest.
Last edited on
Hello, after getting some help from a friend this is the code I now have. It does appear to be working, however, now I am struggling with the outputs. I need to get it into a big table like the image above, and I need to include other trig functions. Advice from this point?

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cmath>

double mySimpleSine (double x);
double mySimpleCosine (double x);
double factorial (int f);
double angleInRadians (double x);
double mySimplerSine(double x);
double mySimplerCosine(double x);
double mySimplestCosine(double x);

const double PI = atan(1.0)*4.0;

using namespace std;

int main(){
  double out;
  for(int i=0;i<=360;i+=15){
      out = mySimpleSine( angleInRadians( (double)i ) );
      cout << "the sine of "<<i<<" degrees is "<<out<<"\n";
  }
}
double factorial(int x){
  double f = 1.0;
  for (int i = 1; i <= x; i++){
    f = f * i;
  }
  return f;
}
double angleInRadians (double x){
    return x / 180.0 * PI;
}
double power (double x, int exponent){
    double out = 1.0;
    for(int i=1;i<=exponent;i++){
        out *= x;
    }
    return out;
}

double mySimpleSine (double x){
  double output = 0;
  double oldOutput = 9999;
  int i = 1;
  bool shouldAdd = true;
  while(abs(output-oldOutput) > 0.0001){
      oldOutput = output;
      output += (shouldAdd?1.0:-1.0) * power(x,i)/factorial(i);
      shouldAdd = !shouldAdd; 
      i += 2; 
  }
  return output;
}

double mySimpleCosine (double x){
  double output = 1; 
  double oldOutput = 9999;
  int i = 2; 
  bool shouldAdd = false; 
  while(abs(output-oldOutput) > 0.0001){
      oldOutput = output;
      output += (shouldAdd?1.0:-1.0) * power(x,i)/factorial(i); 
      shouldAdd = !shouldAdd;
      i += 2; 
  }
  return output;
}

double mySimplerSine(double x){
    double out = x;
    double term = x;
    double i = 1.0;
    while(abs(term) >= 0.0001){ 
        i++;
        term *= x/i;
        i++;
        term *= x/i;
        term *= -1;
        out += term;
    }
    return out;
}

double mySimplerCosine(double x){
    double out = 1;
    double term = 1;
    double i = 0.0;
    while(abs(term) >= 0.0001){
        i++;
        term *= x/i;
        i++;
        term *= x/i;
        term *= -1;
        out += term;
    }
    return out;
}

double mySimplestCosine(double x){ 
    return mySimplerSine(x + PI/2.0);
}
Well, a suggestion for at least making a start on the table of values. I used the angle in degrees to control the loop, then printed some values in what I think is the required format.
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
#include <iostream>
#include <iomanip>

using namespace std;

const double PI = 3.1415926535897932385;

double degToRad(double n) { return n * (PI/180.0); }
double radToDeg(double n) { return n * (180.0/PI); }

int main()
{
    cout << left 
         << setw(8) << " Degrees"
         << setw(8) << " Radians";
    
    cout << '\n' << right << fixed << setprecision(4); 
    for (int x = 0; x<=360; x+= 15)
    {
        double rad = degToRad(x);
        cout << setw(8) << x
             << setw(8) << rad
             << setw(8) << 0.0     // change this line / add extra lines here
             << '\n';        
    }

}

You can change/ insert extra values at line 23 where I just print zero as a placeholder.

Output:
 Degrees Radians
       0  0.0000  0.0000
      15  0.2618  0.0000
      30  0.5236  0.0000
      45  0.7854  0.0000
      60  1.0472  0.0000
      75  1.3090  0.0000
      90  1.5708  0.0000
     105  1.8326  0.0000
     120  2.0944  0.0000
     135  2.3562  0.0000
     150  2.6180  0.0000
     165  2.8798  0.0000
     180  3.1416  0.0000
     195  3.4034  0.0000
     210  3.6652  0.0000
     225  3.9270  0.0000
     240  4.1888  0.0000
     255  4.4506  0.0000
     270  4.7124  0.0000
     285  4.9742  0.0000
     300  5.2360  0.0000
     315  5.4978  0.0000
     330  5.7596  0.0000
     345  6.0214  0.0000
     360  6.2832  0.0000


The cosine function is similar to the sine. Tan is just sin/cos though you may want to put all the code inside its own function in order to get the required accuracy.
@Chervil that looks pretty good so far, but do you have any advice for how I can output my trig functions? This is what I have set up:
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
#include <iostream>
#include <cmath>
#include <iomanip>

const double PI = atan(1.0)*4.0;
double degToRad(double n) { return n * (PI/180.0); }
double radToDeg(double n) { return n * (180.0/PI); }

double mySine (double x);
double myCosine (double x);
double factorial (int f);
double angleInRadians (double x);
double sine(double x);
double cosine(double x);
double tangent(double x);
double cotan(double x);
double secant(double x);
double cosecant(double x);

using namespace std;

int main(){
  
  cout << left
       << setw(8) << " Degrees"
       << setw(8) << " Radians"
       << setw(8) << " mySine";
       
  cout << "\n" << right << fixed << setprecision(4);
  for (int x = 0; x <= 360; x+=15){
    double rad = degToRad(x);
    cout << setw(8) << x
         << setw(8) << rad
         << setw(8) << mySine
         << "\n";
       }
   }


But I know it's not right, how I'm trying to get it to display mySine. However, it doesn't seem to let me put in the out statements like I had before either, so how should I proceed?
Maybe replace line 34:
 
         << setw(8) << mySine
with:
 
         << setw(8) << mySine(rad)

so you are calling the function.

Mostly that part should be straightforward once you get a foothold. However, dealing with the infinity values may require you to break up the code a little, instead of having one long cout << statement.
Last edited on
Topic archived. No new replies allowed.