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()"
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.
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.
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?
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.
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.
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?
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.