-1

I'm currently making an app for finding the roots of polynomials for android. At the moment I'm just dealing with quadratics however I can't spot my error. Whatever I've done it means my value for the root (only doing the first root right now), is always 0.0. Earlier I was trying to find my value for the discriminant and I kept getting the square of the 'b term' in the quadratic. My quadratic equation looks strange because of the way the application is set out, ever letter is shifted 1, so the closed form solution for quadratics in my app is -(c(+/-)(c^c-4*b*d)^(1/2))/(2*b). I would be grateful for any help, thanks! :) P.S. Please try to ignore my awful code, I'm just trying to get it working at the moment :)

public void onButtonClick(View v)
{
double rx, ry, rz, disc, disc2, a, b, c, d;
String sa, sb, sc, sd, sx, sy, sz;
EditText ta = (EditText)findViewById(R.id.a_c);
EditText tb = (EditText)findViewById(R.id.b_c);
EditText tc = (EditText)findViewById(R.id.c_c);
EditText td = (EditText)findViewById(R.id.d_c);
EditText tx = (EditText)findViewById(R.id.x);
EditText ty = (EditText)findViewById(R.id.y);
EditText tz = (EditText)findViewById(R.id.z);
sa = ta.getText().toString();
sb = tb.getText().toString();
sc = tc.getText().toString();
sd = td.getText().toString();
sx = tx.getText().toString();
sy = ty.getText().toString();
sz = tz.getText().toString();
if(sa.matches("")) {a = 0;} else {a = Double.parseDouble(sa);}
if(sb.matches("")) {b = 0;} else {b = Double.parseDouble(sb);}
if(sc.matches("")) {c = 0;} else {c = Double.parseDouble(sc);}
if(sd.matches("")) {d = 0;} else {d = Double.parseDouble(sa);}
if(sx.matches("")) {rx = 0;} else {rx = Double.parseDouble(sx);}
if(sy.matches("")) {ry = 0;} else {ry = Double.parseDouble(sy);}
if(sz.matches("")) {rz = 0;} else {rz = Double.parseDouble(sz);}
disc2 = c*c-4*b*d;
if(a == 0) {rx = (-c+Math.sqrt(disc2))/(2*b); tx.setText(Double.toString(rx));}
}
Gavin S. Yancey
  • 1,216
  • 1
  • 13
  • 34
Ryan Firth
  • 15
  • 2
  • `{d = 0;} else {d = Double.parseDouble(sa);}` shouldn't this be `(sd)`? – Mario Stoilov May 30 '14 at 14:05
  • I'm pretty sure its all over 2a? Unless I've gone completely mad hah, but thanks anyway:) Thanks for the other correction! Didn't notice that :) Just checked, it was the stupid mistake that you spotted that caused it! Thanks a lot, helps a lot having someone else check it. – Ryan Firth May 30 '14 at 14:21
  • New problem, the signs for the roots are wrong but that's an easy fix, its just strange hah:) – Ryan Firth May 30 '14 at 14:22
  • I'd recommend separating the quadratic calculation from all the I/O stuff. Write it as a Java class, test it thoroughly, then worry about getting data in and out of it. – duffymo May 30 '14 at 16:42

1 Answers1

0

Here's how I'd solve a quadratic equation. Sort this out first, then worry about getting data in and out:

/**
 * Quadratic root finder
 * @link http://stackoverflow.com/questions/23956437/quadratic-formula-in-java-for-android-cant-spot-my-mistake
 */
public class QuadraticRootFinder {

    private static final double TOLERANCE = 1.0e-8;

    public static void main(String[] args) {
        if (args.length > 0) {
            double a = Double.parseDouble(args[0]);
            double b = ((args.length > 1) ? Double.parseDouble(args[1]) : 0.0);
            double c = ((args.length > 2) ? Double.parseDouble(args[2]) : 0.0);
            Complex [] roots = QuadraticRootFinder.getRoots(a, b, c);
            for (Complex root : roots) {
                System.out.println(root);
            }
        } else {
            System.out.println("Usage: QuadraticRootFinder <a> <b> <c>");
        }
    }


    public static Complex [] getRoots(double a, double b, double c) {
        Complex [] roots = new Complex[2];
        if (Math.abs(a) <= TOLERANCE) {  // Linear equation; just one root
            if (Math.abs(b) > TOLERANCE) {
                roots = new Complex[1];
                roots[0] = new Complex(-c/b);
            } else {
                throw new IllegalArgumentException(String.format("No roots possible for (a,b,c) = (%f10.3, %f10.3, %f10.3", a, b, c));
            }
        } else {
            double discriminant = b*b-4.0*a*c;
            if (discriminant > 0.0) {  // Two real roots
                roots[0] = new Complex(-b/2.0/a-Math.sqrt(discriminant)/2.0/a, 0.0);
                roots[1] = new Complex(-b/2.0/a+Math.sqrt(discriminant)/2.0/a, 0.0);
            } else { // Two complex conjugate roots
                roots[0] = new Complex(-b/2.0/a, -Math.sqrt(-discriminant)/2.0/a);
                roots[1] = new Complex(-b/2.0/a, +Math.sqrt(-discriminant)/2.0/a);
            }
        }
        return roots;
    }

}

class Complex {
    private final double x;
    private final double y;

    Complex() {
        this(0.0, 0.0);
    }

    Complex(double x) {
        this(x, 0.0);
    }

    Complex(double x, double y) {
        this.x = x;
        this.y = y;
    }

    double getX() {
        return x;
    }

    double getY() {
        return y;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("(").append(x).append(",").append(y).append(")");
        return builder.toString();
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thanks for this, I got it working in the end (with cubics too:) ) but I'm pretty new to it all so reading through this will help me get out of bad practices. – Ryan Firth Jun 01 '14 at 11:25