0

I am trying to solve an given equation using Newton Tangent Method. The Tangent Method works by assuming the solution is somewhere in the a-b interval where a and b are given and that the function is continuous on the [a,b] interval. I already wrote the program and it's working fine but now I have to make a GUI for it and the equation must be read from a text file. My issues is that I do not know how to get the equation from the .txt file and set it as return for my function method. This should work for any given equation. Below is my code for the equation: x^3 -4 * x^2 + 5 * x^1 -12

Here is the code:

static double f(double x) { // the function from the .txt file
    //return Math.pow(x, 3) - 4* Math.pow(x,2) + 5 * Math.pow(x, 1) - 12;
    return x * x * x - 4 * x * x + 5 * x - 12;
}

static double df(double x) { // the function derivative
    return 3 * x * x - 8 * x + 5;
}

static String metTangent() {
    double b = 4, c, reduceIntervalBy, precision = 0.00000000001;
// reduceIntervalBy holds the value of how much should the interval be reduced by, starting from b to a, so from right to left
    DecimalFormat decimalformat = new DecimalFormat("#.00000000000000");

    do {
        c = b - f(b) / df(b);
        //System.out.println(c);
        reduceIntervalBy = b - c;          
        b = c;
    } while (reduceIntervalBy > precision );
    return "The solution is: " + decimalformat .format(c);    
}

Solved it. Thanks everyone for help :)

  • 1
    possible duplicate of [Is there an eval() function in Java?](http://stackoverflow.com/questions/2605032/is-there-an-eval-function-in-java) – Nir Alfasi Jan 16 '15 at 08:11
  • Got a problem now, when I call the method from inside the button I get a java.lang.NullPointerException and I don't know how to fix it. I don't think I'm sending any null values. – user3197521 Jan 17 '15 at 08:17

3 Answers3

3

you can use the below method to read the equation from a text file.

 static  String getEquation () throws Exception
{
    Scanner in = new Scanner(new FileReader("C:\\test1.txt"));
    StringBuffer br = new StringBuffer();

    while(in.hasNext())
    {
        br.append(in.next());
    }
    return br.toString();
}

Then to parse the equation and value to be evaluated to the below function.

static Object f(double x,String eq) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    engine.put("x",x);
    return engine.eval(eq);
}
robin
  • 1,893
  • 1
  • 18
  • 38
  • Got a problem, when I call the method from inside the button I get a java.lang.NullPointerException and I don't know how to fix it. I don't think I'm sending any null values. – user3197521 Jan 17 '15 at 04:00
2

To avoid having to write a parser, one can leverage an implementation of the Java Expression Language, e.g. https://uel.java.net/get-started.html. Alternatively, the Java Scripting API can do the same thing. Both is a little overkill for the small task but should quickly get you started.

Edit: See @robin's answer for an example using the scripting API.

Community
  • 1
  • 1
JimmyB
  • 12,101
  • 2
  • 28
  • 44
1

I suspect this task is MUCH harder than you imagine, these are the basic steps you need to look at implementing:

  1. Read the equation from a text file as a string
  2. Write a parser to make some sort of structure of java objects from this string that represent a path to evaluate the equation for a given value. (this is the hard part - try looking for sources on 'descent parser' and 'shunting yard algorithm .
  3. once you have this structure that can evaluate the equation for any x then the newton's method is easily implemented just as you do.

Good Luck

Elemental
  • 7,365
  • 2
  • 28
  • 33