# Using Taylor Series to find sin(x)

Page 1 of 1

## 4 Replies - 23861 Views - Last Post: 25 March 2010 - 02:28 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=164029&amp;s=d2da1d863a5ba647ddd8af865589fb01&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ShayShep

• New D.I.C Head

Reputation: 0
• 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

• Software Engineer

Reputation: 379
• 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);

```

### #3 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• 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:

Check out the snippet

### #4 ShayShep

• New D.I.C Head

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

## Re: Using Taylor Series to find sin(x)

Posted 25 March 2010 - 08:31 AM

KYA, on 24 March 2010 - 03:35 PM, said:

You could also use the actual equation:

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.

### #5 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: Using Taylor Series to find sin(x)

Posted 25 March 2010 - 02:28 PM

ShayShep, on 25 March 2010 - 02:31 PM, said:

KYA, on 24 March 2010 - 03:35 PM, said:

You could also use the actual equation:

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