0

I'd like to build myself a small app to get my homework done faster. I want the user to be able to enter any function like f(x)=x2-5x+3 or f(x)=x3/(2x-2) and then return a list of y-values for specific x-values. For f(x)=x2 it would look something like this:

  • x=-2 y=4
  • x=-1 y=1
  • x=0 y=0
  • x=1 y=1
  • x=2 y=4
  • x=3 y=9
  • x=4 y=16

And so on. Is there any function in the math class of java that takes in a mathematical function and an x-value and outputs the according y-value or do I need to have some kind of parser of my own?

Edit

exp4j seems to be the right thing for me. This library is very small, fast and easy to use.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    You can build an expression parser in ANTLR: http://stackoverflow.com/questions/4589951/parsing-an-arithmetic-expression-and-building-a-tree-from-it-in-java or use the JVM's scripting facilities, as is shown here: http://stackoverflow.com/questions/4681959/algebra-equation-parser-for-java – miku Feb 16 '13 at 12:27
  • You can run this kind of thing through WolframAlpha, e.g. [`evaluate f(x)=x^2-5x+3z for x=4`](http://www.wolframalpha.com/input/?i=evaluate+f%28x%29%3Dx^2-5x%2B3z+for+x%3D4), but building your own parser is a lot more fun. ANTLR is a good place to start. – Barend Feb 16 '13 at 12:30

0 Answers0