4 Replies - 10839 Views - Last Post: 25 March 2010 - 02:28 PM Rate Topic: -----

#1 ShayShep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-March 10

Using Taylor Series to find sin(x)

Posted 24 March 2010 - 11:26 AM

I am trying to write a program that finds sin(x) using the function double sine( double x, double tolerance, int limit, int & nTerms). I know I have to use the taylor series but I am having trouble getting started.

#include <iostream>
#include <cmath>
using namespace std;
 double factorial(double n){
double i;
double result =1;
if( n>0){
for(i=1; i<=n; i++){
result = result * i;
}
}return result;




This is all the code I have and I'm not sure how to make it work with the function

Is This A Good Question/Topic? 0
  • +

Replies To: Using Taylor Series to find sin(x)

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using Taylor Series to find sin(x)

Posted 24 March 2010 - 12:35 PM

You have the code for the factorial now write the code for the sine function.

The taylor series method:
sin(x) = x - x^3/3 + x^5/5 - x^7/7 + ...

Depending on how accurate you'd like the answer to be you can use that equation or add a few more terms.

Seeing as the accuracy depends on the tolerance value you could develop a method to set the number of decimal places of the final answer. To set the number of decimal places do something like this.

double finalAnswer;
finalAnswer *= pow(10, tolerance);
int trunc = finalAnswer;
// round here if you'd like
finalAnswer = trunc / pow(10, tolerance);


Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Using Taylor Series to find sin(x)

Posted 24 March 2010 - 04:35 PM

You could also use the actual equation:

Posted Image

Check out the snippet
Was This Post Helpful? 0
  • +
  • -

#4 ShayShep  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 11-March 10

Re: Using Taylor Series to find sin(x)

Posted 25 March 2010 - 08:31 AM

View PostKYA, on 24 March 2010 - 03:35 PM, said:

You could also use the actual equation:

Posted Image

Check out the snippet


I was told using the equation is inefficient, and leads to severe
numerical overflow problems. And a better approach is to calculate each term of the series from the previous term.
Was This Post Helpful? 0
  • +
  • -

#5 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Using Taylor Series to find sin(x)

Posted 25 March 2010 - 02:28 PM

View PostShayShep, on 25 March 2010 - 02:31 PM, said:

View PostKYA, on 24 March 2010 - 03:35 PM, said:

You could also use the actual equation:

Posted Image

Check out the snippet


I was told using the equation is inefficient, and leads to severe
numerical overflow problems. And a better approach is to calculate each term of the series from the previous term.

correct ! you can calculate each term from the previous one, e.g.

term(N) = term(N-2) * x * x / ((N-1) * N)

with the sign changing each term
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1