Advertisement

Mathematical Programs(Calculas)

Started by March 03, 2001 12:10 PM
2 comments, last by AMoronThatWantsToProgram 23 years, 11 months ago
I was wondering how to make a computer program take in a full function and then compute something with it. For example, Lets say I want it to find the approximate area under the graph using the trapozoidal rule. Now I can figure out how to make it run by hardcodeing the function, the lower and upper limits and and the number of times(sometimes "n"). What I want, though, is the program to ask you to input those things and then calculate the area. My main question is how would you store a function into memory? Thank you
Read the function in as a string. After that you''ll have to figure out some way to break it up into its smallest components so that it''s easy to calculate it for any value. One way to do this is just to do it straightforwardly, finding the innermost parenthesis and evaluating outward from there. The other way that I can think of (the one I use) is to first convert the function from infix to prefix notation. Then, breaking the function up becomes relatively easy. Prefix notation is a method of writing mathematical formulas such that the operator comes first, then the operand(s), instead of having the operator between the operands (infix notation) like we''re accustomed to. The nice thing about prefix is that it eliminates the need for parenthesis, so you can evaluate the function from beginning to end in one pass. A simple example of infix and prefix:

Infix: a+b
Prefix: +ab

And a slightly more complicated example:

Infix: (a+b)*((c+d)/(e-f))
Prefix: *+ab/+cd-ef

Once you get into prefix then it''s easy to break the function into small steps that look like assembly language:

add t0, a, b
add t1, c, d
sub t2, e, f
div t1, t1, t2
mul t0, t0, t1

In that example, t0, t1, and t2 are "temporary" variables used to store intermediate results. To convert to this from prefix you should only need a single function that accepts as an argument the position in the string at which to begin computations, and returns the index of the temporary variable in which it stores the result. The function should first read an operator, then however many operands that operator requires. For each operand, if the first character is itself an operator, the function calls itself recursively.

How you want to represent those assembly-type steps in memory is up to you, but once you have a function consisting entirely of simple instructions like that, it''s easy to compute its value for any combination of variables. You can also use functions like sin() and cos() just by considering them as unary operators.

-Ironblayde
 Aeon Software

Down with Tiberia!
"All your women are belong to me." - Nekrophidius
"Your superior intellect is no match for our puny weapons!"
Advertisement
A friend of mine created a Function Parser, and this might just be exactly what you are looking for. It lets you parse a function (which may include variables), and then compute them several times with different values for those variables. Take a look at http://www.crosswinds.net/~mindfly/

I hope I understood you right and it is what you are looking for...

Wuntvor

Edited by - Wuntvor on March 3, 2001 4:12:21 PM

Edited by - Wuntvor on March 3, 2001 4:12:57 PM
Well, an easy way to do it would be to pass a pointer to the function to the function can calculates the numeric approximation of the area under the graph. Assuming it is a simple y=f(x) function then you just need a pointer to a function taking one real number as a parameter and returning one real number. Along with the function pointer you would need the interval and resolution.

I assume that isn't what you mean and you want to type function into an edit box and have that function used. As Ironblayde describes you would need to create a respresentation of the function. The following is what your function might look like:

    float Area(FuncTree *Func, char *Var, float Start, float End, float Inc){   float prev, curr, area = 0.0;   if (CreateVar(Var))   {      SetVar(Var, Start);      curr = Evaluate(Func);      for (float i = Start + Inc; i <= End; i += Inc)      {         prev = curr;         SetVar(Var, i);         curr = Evaluate(func);         area += ((prev+curr)*Inc)/2.0;      }      if ((i - End) < Inc)      {         prev = curr;         SetVar(Var, End);         curr = Evaluate(func);         area += ((prev+curr)*Inc)/2.0;      }      DeleteVar(Var);   }   return(area);}    


As for how to get the expression tree I would suggest referring to a book on algorithms and data structures. One key point might be that variables are an implied unary operator of something like GetVar with an operand of a string. It has a higher precedence than any other operator. The reason that may be a key point is that they may only explain how to evaluate an expression with numeric constants. Things get a little more complicated with functions calling functions passing the return values of other functions as parameters. I would suggest a book on writing compilers and interpreters if you want to get into that. Just handling variables and the regular operators is a good start though.

Edited by - LilBudyWizer on March 3, 2001 3:53:21 PM
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement