Thread: Damsel in distress...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Wink Damsel in distress...

    Would anyone happen to have an exaple of a program to approximate the eponential function e^x?

    Thanks

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Sorry, damsel - don't double post. Wait a while for someone to answer your question.

    You could always use
    #define e whatever e is

    and use the pow function.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Sorry about the double post..

    I felt bad because there was a mean face attaced to it and I ment to put a happy face.

    Could you explain a little more your idea?

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    OK...

    You can use

    #define e 2.1345 //I don't know the value of e off the top of my head

    and then use the pow(x,y) function from math.h to do the exponent.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    33
    Last edited by Dummies102; 02-25-2002 at 12:21 PM.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Hi

    Would you happen to have an example program so I have something to reference?????

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    33
    #include <iostream.h>
    #include <math.h>

    int main()
    {
    double x, y;
    cout <<"This program will find x^y. please enter x(number) and then y(power)";
    x = pow(x, y);//this is the pow function, it takes the first number to the power of the second.
    cout <<"\nx to the power of y ="<<x;
    return 0;
    }



    that should do it for ya, post again if you still need help.

  8. #8
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    There is a function in the math header:

    double exp( double arg )

    returns the value of e^arg.

    Conversely,

    double log( double num )

    returns the natural logarithm of num.

    There are many other useful functions in that header you should look at a reference for it. I suggest picking up the C/C++ programmer's reference (good reference book to have).
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Hi every one!!!

    The thing that keeps tripping me up is that I need to obtain an output as follows:
    e to the x 7.38
    approximation 6.333
    the difference 1.056

    With a keyboard inut such as: 2.0

    I am just not getting it???

  10. #10
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Well... what do you base your approximation and actual answer on?

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    The formula looks like this

    = 1 +(x/1) + (x^2/2) + (x^3/6) = (x^4/24)...X^n/n!

    I have to read in x from the keyboard and use the exponential function on x and write the result. Then do the approximation for the first four terms, starting at 1 and write the results. Then write the difference between the two.

    And I am just starting to learn this stuff.
    All of your answeres were great thanks

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    4
    if you need to write each function and it's ok to use a little recursion, try something like:

    int factorial(a) //computes factorial
    {
    if(a > 1)
    a = a * N(a-1);
    else
    return 1;
    return a;
    }

    float power(x,y) //computes the power
    {
    int i;
    float result = x;
    for(i=y; i>1; i--)
    result = result * x;
    return result;
    }


    main()
    {
    float x, result;
    printf("\nPlease enter value for x :>");
    scanf("%f", &x);

    for(i=1; i<10;i++) //make the "10" bigger if you want more loops
    {
    result += power(x, i) / factorial( i );
    printf("Term %i = %f", i, x); // move the print outside the for() loop if you only want the final answer
    }
    }




    //I think all that would work, I just wrote it out off the top of my head, there might be some minor issues or something, but that's the basics of what you'll need. Also, it's assuming you'll only be using positive, non zero numbers. You'll have to add a couple modifications to fix those, but it's not hard and is just more of the same. Those are fun programs to write.
    Last edited by bgrahambo; 02-25-2002 at 08:15 PM.

  13. #13
    What you probably want is in the <iomanip.h> header. It is scientific notation (2.55e+32) is what it looks like you want. With iomanip I believe you can just denote the output format like so...

    cout>>dec>>x; //outputs the x variable in decimal format.

    ...The scientific notation manip(dec) needs to be looked up.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed