Graphing Function Ordered Pair Question

I want the use to be able to enter a function such as f(x) = x^2 and then have a table with the values of points on that line but I am stuck.

How would I convert the equation so I could plug variables into it.

If I did this I would get a mismatch error.

Code:

`String equation = "x^2";`

int x = 1;

int solution = equation;

Ideally, this would plug in the x variable and solve, but Java can't do that.

If anyone has any ideas that would be great.

Thank you for your help!

Re: Graphing Function Ordered Pair Question

Unless you write a parser to analyze the expression you would have to code the method directly. Also, ^ is not exponentiation but bitwise exclusive or. There is no exponentiation operator in Java so you need to use Math.pow().

But to give an example.

Code:

`public double func_of_x(double x) {`

return 3 * x *x + 2 * x + 3;

}

Re: Graphing Function Ordered Pair Question

Is there a way to parse the equation though, so that way they could enter the equation and then I would be able to plug variables into it?

Re: Graphing Function Ordered Pair Question

Yes. Parsing an equation is not overly difficult from a computer science perspective. One way would be to convert it to reverse Polish notation and then evaluate it using stack operations. Most second or third year students need to learn to do it when they write compilers. To do it well requires an understanding of finite state machines and lexical analysis. There may be some packages available to facilitate this but I do not know of any. Perhaps others in this forum could provide a better answer. But syntactically, I believe what you want is something like this.

Code:

`double d;`

// assume ^ is exponentiation

d = evaluate("x^2 + 5", 4);

System.out.println("d = " + d);// should print 21.0

Regards,

Jim

Re: Graphing Function Ordered Pair Question

Thank you for your help. I'm only 14, so this may be a little difficult for me to grasp. If anyone else could help me with this parser that would be great. Again, thank you for you help.

Re: Graphing Function Ordered Pair Question

You might try ANTLR which is a parser generator. I just found out about this specific one myself and it is written for Java. You might also want to look on the web for "parsing algebraic expressions." There may be some prepackaged solutions that you can install. As I said earlier it is not exceptionally hard but it is challenging, especially for someone just starting out.

Regards.

Jim

Re: Graphing Function Ordered Pair Question

OK. Thank you. I will check that out.

Re: Graphing Function Ordered Pair Question

I wrote a blog article once on expression parsing (and evaluation); click on the link near the top right of this reply; alternatively you can let Javascript do the dirty work. Since Java 1.6. the JRE comes bundled with a Javascript interpreter. Feed you function/expression to that interpreter and get the result back; wait, I must have a small demo somewhere ...

Here it is:

Code:

`import javax.script.Invocable;`

import javax.script.ScriptEngine;

import javax.script.ScriptEngineManager;

public class ScriptDemo {

public static void main(String[] args) {

ScriptEngineManager manager = new ScriptEngineManager();

ScriptEngine engine = manager.getEngineByName("JavaScript");

try {

String expression = "function add(x, y) { return x+y; }";

engine.eval(expression);

engine.put("a", 41);

engine.put("b", 1);

Invocable invocable= (Invocable)engine;

double d= (Double)engine.eval("add(a, b)");

System.out.println(d);

System.out.println("add(1, 41)= "+invocable.invokeFunction("add", 1, 41));

System.out.println("add(a, b)= "+invocable.invokeFunction("add", "a", "b"));

System.out.println("add(a, b)= "+engine.eval("add(a, b)"));

} catch (NoSuchMethodException nsme) {

nsme.printStackTrace();

} catch(Exception e) {

e.printStackTrace();

}

}

}

kind regards,

Jos