Jump to content

Infinite summations and C++

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
rioters block

rioters block

    Newbie

  • Members
  • Pip
  • 6 posts
hello everyone. In my CS class we're doing trying to compute some taylor series expansions of basic math functions and thereby learning a lot about doing infinite summations on the computer. My assignment was to compute the cosine of whatever value I choose for x.

cos(x) = 1 - (x^2)/2! + (x^4)/4! - (x^6)/6! + ......

Now, I've written two programs, both of which use while loops, however they have diffrent terminating conditions. And, for smaller values of x they both get the exact answer you would when you use the cosine function in <cmath>. However, if you choose x to be 100 or something, there's a huge discrepancy between my program's answer and cmath.

I'm worried that I might be initializing the values of term and the sums incorrectly. If I'm not, then I'm stumped as to what i'm doing wrong! Here's my code, and if you guys can maybe make a suggestion, it would be sweet!

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
     double x, term = 1, oldsum = 0, newsum = term;
     int i = 1;
     cout<<"Enter value of x in radians:"<<endl;
     cin>>x;
     while(oldsum != newsum) 
*/the loop should stop when there's no more diffrence between the old and new sum (i.e the term has become too small to make a diffrence) */
     {
         oldsum = newsum;
         term = fabs(term)*pow(-1.0,i)*pow(x,2)/(2*i*(2*i-1));
         newsum += term;
         ++i;
     }
     cout<<"cos("<<x<<") = "<<newsum<<endl;
     cout<<"cos("<<x<<") = "<<cos(x)<<endl;
     system("pause");
     return 0;
}


Edited by Jaan, 21 September 2009 - 08:41 AM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The code you've implemented does correctly calculate the term. You aren't calculating the factorial at all, and the power is 2*i, not 2!
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
rioters block

rioters block

    Newbie

  • Members
  • Pip
  • 6 posts

WingedPanther said:

The code you've implemented does correctly calculate the term. You aren't calculating the factorial at all, and the power is 2*i, not 2!

I don't understand what you mean? Am I calculating the term wrong? I couldn't use the factorial function in cmath since it would get too large too quickly, that it would be bigger than INT_MAX which isn't good, so I had to go with this way. What did you mean that the power is 2*i?

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,716 posts
I think he meant:
pow(x,2)
//should be
pow(x,2*i)

sudo rm -rf /

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You have to use factorial. If you don't, you will get incorrect results (as you discovered)!
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog