954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

solving a series

help needed to solve this series
E(x)=1-x^2/2!+x^3/3!-x^4/4!+x^5/5!+....................

trinity_neo
Newbie Poster
5 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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 :P.

pspwxp fan
Junior Poster
100 posts since Feb 2009
Reputation Points: 43
Solved Threads: 7
 

Perhaps his real question is how can he compute the value of that series to a given figure amount using C++?

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

In that case, the right solution would still be to use the closed form expression :)

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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!

trinity_neo
Newbie Poster
5 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

You want to calculate the n-term series and not the infinite series?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

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

long long faculty(long x)
{
    long long y = 1;
    y = 1;

    for(int i = 1; i < (x+1); i++)
        y = y * i;

    return y;
}

long double apow(float x, int y)
{
    long double result = 1;
    if(y == 0)
        return result;

    if(y < 0)
    {
        y = -y;
        for(int i = 0; i < y; i++)
            result = result * x;
        return 1/result;
    }

    for(int i = 0; i < y; i++)
        result = result * x;

    return result;
}


And here's a full example where you can see these functions at work:

#include <iostream>

long long faculty(long x);
long double apow(float x, int y);

using namespace std;

int main(void)
{
    long double answer;
    int x = 1;

    cout << "6! = " << faculty(6) << endl;
    cout << "10^2 = " << apow(10, 2) << endl;
    cout << "10^-2 = " << apow(10, -2) << endl;
    cout << "10^0 = " << apow(10, 0) << endl;

    return 0;
}

long long faculty(long x)
{
    long long y = 1;
    y = 1;

    for(int i = 1; i < (x+1); i++)
        y = y * i;

    return y;
}

long double apow(float x, int y)
{
    long double result = 1;
    if(y == 0)
        return result;

    if(y < 0)
    {
        y = -y;
        for(int i = 0; i < y; i++)
            result = result * x;
        return 1/result;
    }

    for(int i = 0; i < y; i++)
        result = result * x;

    return result;
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
You want to calculate the n-term series and not the infinite series?

YES

trinity_neo
Newbie Poster
5 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

First convert your equation to a closed form. The close form clearly is [tex]\sum_{n=0}^{\infty}(-1)^n\frac{x^n}{n!}[/tex]


Now construct a loop and evaluate it to desired n
You would need a function to calculate factorial and powers.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You