File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Code to model Taylor Series Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Code to model Taylor Series" Watch "Code to model Taylor Series" New topic
Author

Code to model Taylor Series

Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
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!
 
Consider Paul's rocket mass heater.
 
subject: Code to model Taylor Series