Inputting a Math Equation at Run Time?

So yesterday in Calc we learned about the trapezoid rule, the midpoint rule and Simon's rule. It's not necessary to "know" calculus to get them, they are simply formulas you use to find an approximation for a definite integral you cannot do out normally. However, by nature of the formula (http://en.wikipedia.org/wiki/Trapezoidal_rule as an example, scroll down to numerical implementation) it can be very tedious to do over and over for each value of delta-x.

I then thought about implementing a program that would simply do it all for me, where I could input the values that would change from problem to problem such as a, b, and n. The only problem, is the other thing that also changes is the value of f(x). Essentially, for each problem, I would have to re-write my problem with the correct value of f(x). Is there a way I can write a program so that it won't simply interpret an input such as 6sqrt(log(4x)) or a basic sin(x) as a string, but instead treat it as a function with a variable? Any help would be appreciated (new to C++).
You will want to pick an equation parsing library. Doing it yourself is quite tedious and difficult.
Will a given library allow me to store an equation with a variable?
I haven't looked deeply into libraries that exist to do this, so I only know of one that other people here have found and used:

http://muparser.beltoforion.de/

Check out the examples:
http://muparser.beltoforion.de/mup_version.html#idExample
<cmath> contains sin and cos. I think you could write a simple for loop to perform a (basic) numerical integration.
hyperfine brings up a good point - if you only have a small set of equations you will be working with, you can hard code them and never need to worry about using an external library.
The tricky part is I'd never know what equation I would need to solve for f(x) so a library that would give me the ability to parse any given f(x) is pretty invaluable. The link to the library you've posted seems interesting, I'll have to incorporate it and see if it gives me my desired result. I'll post code here later after I've taken a swing at it.
You can handle polynomials with integer exponents with out the need for <cmath>, and with <cmath> you can do polynomials of any exponent as well as any trig functions, logarithmic functions, the exponential function, etc. Those are the building blocks of more complicated functions, so you could probably make due with just <cmath>.
Last edited on
Topic archived. No new replies allowed.