Results 1 to 8 of 8
 05292013, 12:52 AM #1Senior Member
 Join Date
 Jan 2013
 Posts
 168
 Rep Power
 3
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;
If anyone has any ideas that would be great.
Thank you for your help!
 05292013, 01:02 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,523
 Rep Power
 7
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; 05292013 at 02:39 AM. Reason: YAT
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 05292013, 01:08 AM #3Senior Member
 Join Date
 Jan 2013
 Posts
 168
 Rep Power
 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?
 05292013, 02:48 AM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,523
 Rep Power
 7
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
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 05292013, 03:51 AM #5Senior Member
 Join Date
 Jan 2013
 Posts
 168
 Rep Power
 3
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.
 05292013, 04:02 AM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,523
 Rep Power
 7
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.
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 05292013, 04:04 AM #7Senior Member
 Join Date
 Jan 2013
 Posts
 168
 Rep Power
 3
Re: Graphing Function Ordered Pair Question
OK. Thank you. I will check that out.
 05292013, 08:18 AM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,945
 Blog Entries
 7
 Rep Power
 22
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; 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(); } } }
JosI have the stamina of a seal; I lie on the beach instead of running on it.
Similar Threads

Do not allow duplicates in an ordered link list
By wfsteadman in forum New To JavaReplies: 1Last Post: 02162013, 10:31 PM 
Ordered Vector Issue
By mmonish91 in forum New To JavaReplies: 8Last Post: 03072012, 08:44 AM 
Random Function Question.
By Dark in forum New To JavaReplies: 13Last Post: 04062011, 04:51 AM 
Ordered Array plz help....due tomorrow!!
By canyonist in forum New To JavaReplies: 6Last Post: 05232010, 09:57 AM 
viewing records ordered by date
By nitish.dhar in forum New To JavaReplies: 3Last Post: 07312008, 07:14 AM
Bookmarks