Taylor series in C++ ASAP

I wrote this code but it is not compiling as required

Page 1 of 1

5 Replies - 2656 Views - Last Post: 28 March 2009 - 06:07 PM Rate Topic: -----

#1 anonymous90  Icon User is offline

  • New D.I.C Head

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

Taylor series in C++ ASAP

Post icon  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  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 280
  • View blog
  • Posts: 1,787
  • 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;

const char headerStr[] =

"\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;
    puts( headerStr );
    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;

        x = x*degToRad;
        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));
    }
}

Was This Post Helpful? 1
  • +
  • -

#3 anonymous90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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: :code:


If what you have already posted IS you code... I would suggest you follow some basic C tutorials.
Was This Post Helpful? 0
  • +
  • -

#5 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 280
  • View blog
  • Posts: 1,787
  • 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();
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1