# Thread: Graphing Function Ordered Pair Question

1. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
8

## 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.

Java 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.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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.

Java Code:
```public double func_of_x(double x) {
return 3 * x *x + 2 * x + 3;
}```
Last edited by jim829; 05-29-2013 at 03:39 AM. Reason: YAT

3. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
8

## 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?

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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.

Java Code:
```double d;
// assume ^ is exponentiation
d = evaluate("x^2 + 5", 4);
System.out.println("d = " + d);// should print 21.0```
Regards,
Jim

5. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
8

## 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.

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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

7. Senior Member
Join Date
Jan 2013
Posts
168
Rep Power
8

## Re: Graphing Function Ordered Pair Question

OK. Thank you. I will check that out.

8. ## 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:

Java 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;

System.out.println(d);
} catch (NoSuchMethodException nsme) {
nsme.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}```
kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•