# Taylor series in C++ ASAP

Page 1 of 1

## 5 Replies - 3710 Views - Last Post: 28 March 2009 - 06:07 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=95918&amp;s=5b1567fada25d86ac362fe244252f8aa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 anonymous90

Reputation: 0
• Posts: 2
• Joined: 28-March 09

# Taylor series in C++ ASAP

Posted 28 March 2009 - 02:58 PM

First the series I need is:{ z+z^3/3+z^5/(3*5)+z^7/(3*5*7)+...)
I need at least 50 summations of the approximation.
The partial code I wrote is:
long double z;
long double n=1;
long double seq;
for (int i=1;i<100;i+=2)
{n*=i;pow(z,n)/n;seq=pow(z,n);cout<<seq;

This post has been edited by anonymous90: 28 March 2009 - 03:55 PM

Is This A Good Question/Topic? 0

## Replies To: Taylor series in C++ ASAP

### #2 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Taylor series in C++ ASAP

Posted 28 March 2009 - 03:34 PM

This may give you some ideas ...

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cfloat>

const double EPSILON = DBL_EPSILON;
const double degToRad = M_PI /180;

"\n Finding sin(x) as the sum of the series x - x^3/3! + x^5/5! - x^7/7! ..."
"\n and comparing that sum with the value for the <cmath> function for sin(x)"
"\n";

int main()
{
double sum, newTerm, x;
int n, count;
for(;;)/>
{
n = 1;
count = 0;
sum = 0.0;
printf(" Enter a value, 0 to quit, for x in range >0 and <180 : ");
x = -1;
scanf("%lf",&x);
while( getchar() != '\n' ); /* flush stdin ...*/
if( x<0 || x>=180 ) continue;
if( x == 0 ) return 0;

newTerm = x;

while( fabs(newTerm) > EPSILON )
{
sum += newTerm;
n += 2;
newTerm = -newTerm / n / (n-1) * x * x;
++count;
}
printf("\n After %d terms, the Taylor Series summed to %.14g \n\n", count, sum);
printf(" sin(%.14g) was %.14g \n\n", x, sin(x));
}
}

### #3 anonymous90

Reputation: 0
• Posts: 2
• Joined: 28-March 09

## Re: Taylor series in C++ ASAP

Posted 28 March 2009 - 04:01 PM

I would prefer if you see what is wrong with my code.thanks

### #4 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Taylor series in C++ ASAP

Posted 28 March 2009 - 04:29 PM

can you actually post your code please. remeber to use code tags:

If what you have already posted IS you code... I would suggest you follow some basic C tutorials.

### #5 Pwn

• D.I.C Regular

Reputation: 19
• Posts: 458
• Joined: 25-November 07

## Re: Taylor series in C++ ASAP

Posted 28 March 2009 - 04:57 PM

We are the pirates who don't do anything...and ASAP is not in our vocabulary. ASAP is what you hear from your employer...so are you paying?

This post has been edited by Pwn: 28 March 2009 - 04:58 PM

### #6 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Taylor series in C++ ASAP

Posted 28 March 2009 - 06:07 PM

You might like to try this often preferred iterative approach ... that alternates multiplying and dividing, to keep the numbers more manageable ... and ... to give more accurate results.

#include <iostream>
using namespace std;

//  SUM = z + z^3/3 + z^5/(3*5) + z^7/(3*5*7) + .....

int main()
{
long double z=1.1;
long double next_term=z;
long double sum=0;
int next_div_fac=3;
for( int i=0; i<20; ++i )
{
sum += next_term;
next_term *= z/next_div_fac*z; // next_term = next_term * z*z/next_div_fac
next_div_fac += 2;

cout << "sum=" << sum << " and " << "next_term =" << next_term
<< " and next_div_fac =" << next_div_fac << endl;
}

cin.get();
}