solving a series

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 5
Reputation: trinity_neo is an unknown quantity at this point 
Solved Threads: 0
trinity_neo trinity_neo is offline Offline
Newbie Poster

solving a series

 
0
  #1
Mar 12th, 2009
help needed to solve this series
E(x)=1-x^2/2!+x^3/3!-x^4/4!+x^5/5!+....................
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: solving a series

 
0
  #2
Mar 12th, 2009
Well it's almost like the series for exp(x), except the signs are alternating and it's missing the x^1 term and the x^0 term has the wrong sign. If consider the series you get for exp(-x), you should be close to the answer
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 73
Reputation: pspwxp fan is an unknown quantity at this point 
Solved Threads: 6
pspwxp fan pspwxp fan is offline Offline
Junior Poster in Training

Re: solving a series

 
0
  #3
Mar 12th, 2009
I'm a newbie here but as much as i know, shouldnt this thread not be in C++? Its math, for heavens sake. Also, its wierd that someone has come asking for help with random math equations. Like a binomial theorem that goes on forever .
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: solving a series

 
0
  #4
Mar 12th, 2009
Perhaps his real question is how can he compute the value of that series to a given figure amount using C++?
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: solving a series

 
0
  #5
Mar 12th, 2009
In that case, the right solution would still be to use the closed form expression
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 5
Reputation: trinity_neo is an unknown quantity at this point 
Solved Threads: 0
trinity_neo trinity_neo is offline Offline
Newbie Poster

Re: solving a series

 
0
  #6
Mar 13th, 2009
I missed a term in series.Really sorry. the actual series is

E(x)=1-x/1!+x^2/2!-x^3/3!+........................x^n/n!

Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso

Re: solving a series

 
0
  #7
Mar 13th, 2009
This should get you pretty close. Each term has the form (-1^n)*((x^n)/n!) where 0! is defined as 1 and x^0 is also defined as 1. E(x) is the sum of the number of terms you want to use. You calculate the sum using a running total within a loop adding each term as it is calculated. Calculate each term by changing the form to: a *(b/c) and then calculate a, b and c before calculating the term. a, b, and c can each be calculated using loops. As an alternative a and b could be calculated using pow(). As a further alternative a could be calculated using an if statement and n % 2.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: solving a series

 
0
  #8
Mar 13th, 2009
You want to calculate the n-term series and not the infinite series?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: solving a series

 
0
  #9
Mar 13th, 2009
I wrote two simple C++-functions:
-> One wich raises a number x to the power of y ( apow(x, y); )
-> One which calculates the faculty of number x ( faculty(x); )

  1. long long faculty(long x)
  2. {
  3. long long y = 1;
  4. y = 1;
  5.  
  6. for(int i = 1; i < (x+1); i++)
  7. y = y * i;
  8.  
  9. return y;
  10. }
  11.  
  12. long double apow(float x, int y)
  13. {
  14. long double result = 1;
  15. if(y == 0)
  16. return result;
  17.  
  18. if(y < 0)
  19. {
  20. y = -y;
  21. for(int i = 0; i < y; i++)
  22. result = result * x;
  23. return 1/result;
  24. }
  25.  
  26. for(int i = 0; i < y; i++)
  27. result = result * x;
  28.  
  29. return result;
  30. }

And here's a full example where you can see these functions at work:
  1. #include <iostream>
  2.  
  3. long long faculty(long x);
  4. long double apow(float x, int y);
  5.  
  6. using namespace std;
  7.  
  8. int main(void)
  9. {
  10. long double answer;
  11. int x = 1;
  12.  
  13. cout << "6! = " << faculty(6) << endl;
  14. cout << "10^2 = " << apow(10, 2) << endl;
  15. cout << "10^-2 = " << apow(10, -2) << endl;
  16. cout << "10^0 = " << apow(10, 0) << endl;
  17.  
  18. return 0;
  19. }
  20.  
  21. long long faculty(long x)
  22. {
  23. long long y = 1;
  24. y = 1;
  25.  
  26. for(int i = 1; i < (x+1); i++)
  27. y = y * i;
  28.  
  29. return y;
  30. }
  31.  
  32. long double apow(float x, int y)
  33. {
  34. long double result = 1;
  35. if(y == 0)
  36. return result;
  37.  
  38. if(y < 0)
  39. {
  40. y = -y;
  41. for(int i = 0; i < y; i++)
  42. result = result * x;
  43. return 1/result;
  44. }
  45.  
  46. for(int i = 0; i < y; i++)
  47. result = result * x;
  48.  
  49. return result;
  50. }
Last edited by tux4life; Mar 13th, 2009 at 4:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 5
Reputation: trinity_neo is an unknown quantity at this point 
Solved Threads: 0
trinity_neo trinity_neo is offline Offline
Newbie Poster

Re: solving a series

 
0
  #10
Mar 14th, 2009
Originally Posted by Rashakil Fol View Post
You want to calculate the n-term series and not the infinite series?
YES
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC