0

I am creating a project to compute roots for bisection method in JSwing. There are errors when obtaining function from the user, put it in a method and returning it.

I have tried various way that I know of but it wasnt successful such as converting the string to double, initialised the obtained function before returning it and a few more.

public double f(double x) {
    double func =Double.parseDouble(textFunction.getText());
    return func;
}

public void bisection_method(double x1,double x2) {
    double error = Double.valueOf(textError.getText());
    int n = 0;
    if(f(x1) * f(x2)>0.0)
        JOptionPane.showMessageDialog(null, "Function has same signs at ends of interval");

    while(Math.abs(x1 - x2) >= error) {
        double m = (x1 + x2) / 2.0;

        if(f(m) * f(x1) < 0.0) {
            x2 = m;
            n++;
        } else {
            x1 = m;
            n++;
        }
        System.out.println(m);
    }
}

I expect the roots of equation to show but nothing happens.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • Can you please provide more explanation? What errors are you getting? How are you reading the values? Please read [How to ask](https://stackoverflow.com/help/how-to-ask) and then edit your question accordingly. – Prashant Nov 07 '19 at 08:06
  • [How to take f(x) as user input and make it work like formula using java code?](https://stackoverflow.com/questions/52245741/how-to-take-fx-as-user-input-and-make-it-work-like-formula-using-java-code) – Abra Nov 07 '19 at 08:12
  • Please [edit] your question and post a [mcve]. If you are getting exceptions when you run your code, then also post the error message and stack trace. – Abra Nov 07 '19 at 08:14

0 Answers0