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

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




Math Problem(Integrate a read in definite function and calculate it by

 
Reply to this topicStart new topic

Math Problem(Integrate a read in definite function and calculate it by

jackoblee
31 May, 2008 - 08:29 PM
Post #1

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 2

I can do the part of calculating Reiman sum, but i cannot think of the way to read in a function and then integrate it.
User is offlineProfile CardPM
+Quote Post

KYA
RE: Math Problem(Integrate A Read In Definite Function And Calculate It By
31 May, 2008 - 08:39 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,045



Thanked: 124 times
Dream Kudos: 1200
My Contributions
Show some code. All you'd have to is send the function as a parameter to the integration function. The more appropriate question: do you know how to code an integrating function? The math needs to come before the code.
User is online!Profile CardPM
+Quote Post

jackoblee
RE: Math Problem(Integrate A Read In Definite Function And Calculate It By
31 May, 2008 - 08:44 PM
Post #3

New D.I.C Head
*

Joined: 31 May, 2008
Posts: 2

I don't know how to integrate a function in programming.
Btw, is it very difficult?
User is offlineProfile CardPM
+Quote Post

joske
RE: Math Problem(Integrate A Read In Definite Function And Calculate It By
1 Jun, 2008 - 01:18 AM
Post #4

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
You have two issues here:
  • For reading a custom function (for example from a file or user input), you need an expression parser, which is able to interpret and evaluate a string containing an expression, for example "x^2 - 5". C++ does not have such an expression parser built in. Some interpreted programming languages like Javascript and thinBasic do have a function eval("expression") built in, which is a powerful tool. If you can't use such a language for your project, you need to built an expression parser yourself, which is a lot of work. You can search on the internet for a "C++ expression parser".
  • Second issue is integrating the function. Have a look here. It is very easy to do a numeric integration. If you want to integrate a function 2*x^3 from x_start to x_end, with a stepsize dx, you just do:
    cpp

    double x_start = 0.0;
    double x_end = 2.0;
    double dx = 0.01; // step size (making this smaller will gives more accurate result
    double x = x_start + dx / 2.0; // start at x_start plus a half stepsize!
    double integral = 0.0;
    while (x < x_end)
    {
    double y = 2.0 * pow(x, 3);
    integral += y * dx;
    x += dx;
    }
    printf("Integral = %g \n", integral); // exact solution would be 8.0 for this function

    There are other (more complex) methods for integration too. You can have a look into that if the method above is not fast/accurate enough.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 10:59AM

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