# Code to model Taylor Series

Amy Lee

Greenhorn

Posts: 25

posted 12 years ago

Hello all, I have a question related to code that I'm trying to construct that will model a Taylor series for the sin and cos functions. I've posted to another forum where they were not too friendly...I'm new, so obvious solutions may not be so obvious to me.

That said, the Taylor series for the sin function is:

sin(x) = x - ( x ^ 3 / 3 ! ) + ( x ^ 5 / 5 ! ) - ( x ^ 7 / 7 ! ) + ...

And for cos function it is:

cos(x) = 1 - ( x ^ 2 / 2 ! ) + ( x ^ 4 / 4 ! ) - ( x ^ 6 / 6 ! ) + ...

My code is as follows:

When I would call the method, I would enter x = 90 and numTerms = 10, expecting a return of 1 - instead it returned a huge number: 1.1131025626634878E21.

Someone in another forum told me that I should be entering x as degrees and not radians. When I do this, x = Math.PI / 2.0 and numTerms = 10, it returns 1.0.

Long story short, I want a "second opinion" on my code - is it structured correctly? I know code can be modeled many different ways, but is my way OK? And was the radian thing correct? THANKS!

That said, the Taylor series for the sin function is:

sin(x) = x - ( x ^ 3 / 3 ! ) + ( x ^ 5 / 5 ! ) - ( x ^ 7 / 7 ! ) + ...

And for cos function it is:

cos(x) = 1 - ( x ^ 2 / 2 ! ) + ( x ^ 4 / 4 ! ) - ( x ^ 6 / 6 ! ) + ...

My code is as follows:

When I would call the method, I would enter x = 90 and numTerms = 10, expecting a return of 1 - instead it returned a huge number: 1.1131025626634878E21.

Someone in another forum told me that I should be entering x as degrees and not radians. When I do this, x = Math.PI / 2.0 and numTerms = 10, it returns 1.0.

Long story short, I want a "second opinion" on my code - is it structured correctly? I know code can be modeled many different ways, but is my way OK? And was the radian thing correct? THANKS!