ok im trying to make a user enter an equation so i can find the area under the curve using the limit with sigma notation and i was wondering how i could do something
how could i have a user enter an equation that the program could then access and use
i have the width equal to f(w)=(b-a)/n where a and b are the endpoints and n is the number of rectangles
for example the equation of h(x)= x^2 + 2
how could i access this equation
i would like to do it where i could do h(f)
i dont need to know how to do the limit
i have an idea for that
but im not sure how to do the equation part
thanks for the help
Your going to have to find either an open source/free mathmatical expression parser.
This will take the string you enter (e.g. exp(2)-1) and split it up into different sections. From there it will interpret each section and build an equation in memory (or on the fly).
This requires quite good knowledge of the different ways math functions can be expressed in text, how to manipulate text and convert variables and how to chain equations together.
On a scale of 1-10 (1 being beginner, 10 being guru) I'd rate the difficulty of developing you own somewhere around 7-8.
In case you want to write it yourself, which I recommend as a very good exercise, here's a short explanation. I tried to leave out all the low-level details such as string manipulation so as not to confuse you too much.
For example, you have the expression "x+(y/2)^(2+z)".
You need to decompose this into the smallest parts possible.
First, we'll need to define an operator precedence. We'll use the same as C (C doesn't have an exponentiation operator, but that doesn't matter):
A higher precedence means the operation is computed earlier.
3. ^
2. /, *
1. +, -
Parentheses alter the order of execution. whitespace is ignored.
So, we have our string and we also have a small language definition. All we need to do is parse.
I won't reproduce all the steps, as it can be lengthy. Basically, you'll need to detect four kinds of tokens: numerical constants, operators, parentheses, and variables. There are specific moments at which a token of a given kind can be found, so finding a kind of token out of place means the whole expression is invalid (e.g. "1+(+)" is invalid because an operator was found when a constant or a variable was expected).
You'll need to define your precedences somewhere, and do comparisons with them to determine the order of execution.
The final order of operations could be this:
&# means "the result of operation #"
1. /(y/2)
2. +(2,z)
3. ^(&1,&2)
4. +(x,&3)
You might choose to interpret (that is, actually performing the calculations rather than just translate the string for the machine) as you parse or you may prefer to parse everything first, put it in a queue and then begin your interpretation.
with a bit of reflection on what im trying to do i think i figured out a way to do it that is slightly user friendly
i think ill have them put in (starting with the highest power) the coefficent and then what power it goes to and then go on to the next highest power
(yes i know that this way will only work with one variable)
that way each seperate thing can be stored seperately
when i finish the code ill paste it on this thread
Another tool for doing expressions is ANTLR which has a nice GUI to test your grammar.
It's a bit of an overkill for this but it is very convenient to use, automatically generates lexer/parser code. We use it at work, and its free. Also a good tool to know professionally since it allows to create domain specific languages.