Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,233 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,745 people online right now. Registration is fast and FREE... Join Now!




Formula Implementation

 
Reply to this topicStart new topic

Formula Implementation, 1 + x + x2/2! + x3/3! + … + xn/n!

madawa
13 Mar, 2007 - 09:50 PM
Post #1

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 9


My Contributions
The value ex can be approximated by the sum
1 + x + x2/2! + x3/3! + … + xn/n!
Write a program that takes a value x as input and outputs this sum for n taken
to be each of the values 1 to 100. The program should also output ex
calculated using the predefined function exp. The function exp is a predefined
function such that exp(x) returns an approximation to the value ex. The
function exp is in the library with the header file cmath. Your program should
repeat the calculation for new values of x until the user says she or he is
through.

This post has been edited by madawa: 13 Mar, 2007 - 09:51 PM
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Formula Implementation
13 Mar, 2007 - 09:56 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(madawa @ 14 Mar, 2007 - 11:20 AM) *

The value ex can be approximated by the sum
1 + x + x2/2! + x3/3! + … + xn/n!
Write a program that takes a value x as input and outputs this sum for n taken
to be each of the values 1 to 100. The program should also output ex
calculated using the predefined function exp. The function exp is a predefined
function such that exp(x) returns an approximation to the value ex. The
function exp is in the library with the header file cmath. Your program should
repeat the calculation for new values of x until the user says she or he is
through.



Then where is the problem?
you need to write a method to calculate power of x.
and need to write a method to calulate e^x.

just try to code the steps which we follow while calculating it manually.
and still if there is any problem the put the code here.
we will try to solve it.
User is offlineProfile CardPM
+Quote Post

madawa
RE: Formula Implementation
14 Mar, 2007 - 02:49 AM
Post #3

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 9


My Contributions
i tried i dont get it am trying for da 3rd day nw
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Formula Implementation
14 Mar, 2007 - 04:20 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Please post the code you have written, and we'd be happy to provide some guidance.
User is online!Profile CardPM
+Quote Post

msg555
RE: Formula Implementation
14 Mar, 2007 - 05:56 AM
Post #5

D.I.C Head
Group Icon

Joined: 4 May, 2006
Posts: 136



Thanked: 2 times
Dream Kudos: 25
My Contributions
The nice thing about this problem is there is a simple constant time relationship between each term, therefore, you don't need nor want a power function or a factorial function. Also this will eliminate problems caused by the fact the factorial function and the pow function are difficult to calculate for large values.

T0 = 1
T1 = x
T(n + 1) = Tn * x / n

and exp(x) = T0 + T1 + ... + Tn + ...

This post has been edited by msg555: 14 Mar, 2007 - 06:01 AM
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Formula Implementation
14 Mar, 2007 - 07:55 AM
Post #6

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
And take care not to exceed a certain value for finding factorials.
Finding factorials of numbers beyond the range of int of long data types, results in garbage values which may mess up the final result.

So don't keep the loop exit condition too large.
User is offlineProfile CardPM
+Quote Post

ajwsurfer
RE: Formula Implementation
14 Mar, 2007 - 08:17 AM
Post #7

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 292



Thanked: 2 times
Dream Kudos: 50
My Contributions
Why don't you build an interface that calls a function such as:

answer = calculate( x );

at least.

The function calculate(x)
could just be:

long calculate(int x) {
return 10;
}

for now.

Then you can add the math latter.
The last line describes the interface.

I would also do the math out on paper to see if there are any paterns that can be found.
User is offlineProfile CardPM
+Quote Post

madawa
RE: Formula Implementation
15 Mar, 2007 - 07:27 AM
Post #8

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 9


My Contributions
#include<iostream>
#include<cmath>

using namespace std;

double fact( int x)
{
if ( x == 1 )
return 1;
else
return x * fact( x - 1) ;
}

int main()

{
char ans;
do{
double sum =0;
system("color 2b");
double Entry;
cout<<endl;
system("color 1b");
cout<<"please enter two values x :"<<endl;
cout<<endl;
cin>>Entry;
sum = sum + 1 + Entry ;
double facto;

for ( int i = 2 ; i < 101; i++)
{
facto = fact(i);
system("color 2d");
sum += pow(Entry,i) / facto;
cout<<"\tSUM="<<sum;
system("color 2a");
if( i % 3 == 0)
cout<<"\n"<<endl;
system("color 1d");

}

cout<<endl;
cout<<endl;
system("color 2a");
cout<<endl;
cout<<"Do you want to continue?(y/n)" ;
cout<<endl;
system("color 1a");
cout<<endl;
cin>>ans;
cout<<endl;
}while(ans == 'y');
system("color 1e");

system("pause");
system("pause");
system("pause");
cout<<endl;





system("color 2b");
system("color 2c");
return 0;

}

IS THIS CORRECT ???????
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Formula Implementation
15 Mar, 2007 - 12:39 PM
Post #9

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
assuming that x^2 = pow(x,2) then is this the summ you are trying to computer?

F(x,n) = 1 + x + (x^2)/2 + (x^3)/3! + (x^4)/4! + ... + (x^n)/n!

or

G(x,n) = 1 + x + (x*2)/2 + (x*3)3! + (x*4)/4! + ... + (x*n)/n!

The original post makes it look like you are trying for the second, but your code looks like the first.

You ask for two values, but only read in 1 (entry) that causes the "DO you wish to continue..." part to be passed up.

other than that it seems to preform F(x, n) as defined above. You could improve the program with a little math and or precalculation. For example if you were to allocate an array large enough to hold all n numbers, then you can calculate out n! while saving all of the intermediate values in the table.

CODE
#include<iostream>

using namespace std;

double factorialTable(double *table, int n);

int main()
{
    double facts[101];
    int i;
    factorialTable (facts, 100);
    for (i=1; i<101;i++)
        {
        cout << i<< '\t' << facts[i] <<'\n';
        }
    return 0;
}

double factorialTable(double *table, int n)
{
    double retValue;
    if (n>=1)
    {
        if (n==1)
        {
            table[n] = 1;
            retValue = 1;
        } else
        {
            retValue = n * factorialTable(table, n-1);
            table[n] = retValue;
        }
    } else { retValue = 0; }
    
    return retValue;
}

The above code calculates a table containing all of the factorial values between n=1 and n=100. It does so all at once so you don't waste so much time calculateing the save values over and over. (5!=5*4!, 4!=4*3! so if you have a loop from 1 to 5, you calcuate 1! 5 time, 2! 4 times, 3! 3 times, 4! twice, and 5! once... the above program calculates all factorials once).

There is a neat little algorithm for calculating polynomials

say p(n) = a(0) + a(1)* x +a(2)*x^2 +a(3)*x^3 +... + a(n)*x^n

for the computer to calculate x^n it takes n multiplications, then x^(n-1) is n-1 multiplications... so this formula has n + (n-1) + (n-2)... +1 multiplications to preform.

We can speed things along by re-writing this as:

p(n) = a(0) + x*(a(1) + x*(a(2) + x*(a(3) + ... + x*(a(n)) ... )))

This preforms the same calculation in only n multiplications. That is significantly faster!

If you combine the two ideas you can make the calculation much faster... of course you need to really think about what they are doing, else when your teacher says, "oh very good! now why did you do it this way?" you will not look bad when you explain.
User is offlineProfile CardPM
+Quote Post

madawa
RE: Formula Implementation
15 Mar, 2007 - 06:47 PM
Post #10

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 9


My Contributions
tks for the support ,, i need to see the following code matches for this question .. and any way i can improve its accuricy


An approximate value of pi can be calculated using the series given below:
Pi = 4 [ 1 – 1/3 + 1/5 – 1/7 + 1/9 … + ((-1)n)/(2n+1)]
Write a C++ program to calculate the approximate value of pi using this
series. The program takes an input n that determines the number of terms in
the approximation of the value of pi and outputs the approximation. Includes a
loop that allows the user to repeat this calculation for new values n until the
user says she or he wants to end the program.



#include<iostream>
#include<cmath>

using namespace std;

int main()
{
double firstFactor;
double pi ;
double determinated =-1;
int numberOfInputs;
system("color 7f");
cout<<endl;
system("color 7a");
cout<<endl;
system("color 7c");
cout<<"please input a number "<<endl;
system("color 2f");
cout<<endl;
system("color 7b");
cout<<endl;
system("color 5a");
cin>>numberOfInputs;
for(int i =0; i<= numberOfInputs; i++)
{

firstFactor += pow(determinated,i) / (2*i +1);
}

pi= 4 * firstFactor;
cout<<endl;
cout<<pi;
cout<<endl;

cout<<endl;
system("pause");
system("color 2a");
system("color 2b");
system("color 2e");
cout<<endl;
system("pause");

cout<<endl;
return 0;
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Formula Implementation
15 Mar, 2007 - 06:56 PM
Post #11

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
not sure how you can improve accuracy on the one, I know that there is no reason to use the pow() function which will increase the speed.

basicly you need to alternate between two values +1 and -1, if i is even, then (-1)^i is +1 else it is -1 so using (i % 2) ? -1 : 1 would be faster than pow(). If the conditional operator is too confusing you can also do it with:
CODE
if (i%2) { determinated = -1; } else {determinated = 1;}
firstFactor += determinated / (2*i +1);
Which would basicly be the same thing as the conditional.


User is offlineProfile CardPM
+Quote Post

msg555
RE: Formula Implementation
16 Mar, 2007 - 03:15 AM
Post #12

D.I.C Head
Group Icon

Joined: 4 May, 2006
Posts: 136



Thanked: 2 times
Dream Kudos: 25
My Contributions
I just thought I throw this out there. This is the way I would calculate exp(x).

CODE
double myExp(double x, int terms)
{
    double ret = 1.0;
    double term = 1.0;
    for(int i = 1; i < terms; i++)
    {
        term *= x / i;
        ret += term;
    }
    return ret;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:46PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month