943,881 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1107
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 12th, 2009
0

solving a series

Expand Post »
help needed to solve this series
E(x)=1-x^2/2!+x^3/3!-x^4/4!+x^5/5!+....................
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trinity_neo is offline Offline
5 posts
since Mar 2009
Mar 12th, 2009
2

Re: solving a series

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
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 12th, 2009
0

Re: solving a series

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 .
Reputation Points: 43
Solved Threads: 7
Junior Poster in Training
pspwxp fan is offline Offline
93 posts
since Feb 2009
Mar 12th, 2009
0

Re: solving a series

Perhaps his real question is how can he compute the value of that series to a given figure amount using C++?
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Mar 12th, 2009
1

Re: solving a series

In that case, the right solution would still be to use the closed form expression
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 13th, 2009
0

Re: solving a series

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!

Reputation Points: 10
Solved Threads: 0
Newbie Poster
trinity_neo is offline Offline
5 posts
since Mar 2009
Mar 13th, 2009
0

Re: solving a series

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 13th, 2009
3

Re: solving a series

You want to calculate the n-term series and not the infinite series?
Team Colleague
Reputation Points: 1135
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Mar 13th, 2009
0

Re: solving a series

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); )

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Mar 14th, 2009
0

Re: solving a series

You want to calculate the n-term series and not the infinite series?
YES
Reputation Points: 10
Solved Threads: 0
Newbie Poster
trinity_neo is offline Offline
5 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: did you know you can do this?
Next Thread in C++ Forum Timeline: Using Functions and fractions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC