0

I declared the private Complex variable 'root' in my class. I set the value of root to a Complex number inside the void method 'iterate'

I wish to print the new value of 'root' (line 60) but when I try, it prints as null. So I don't think it is calling the correct 'root'.

My code:

class Newton {

public static final int MAXITER = 20;
public static final double TOL = 1.0e-10;


private Polynomial f;        //Polynomial
private Polynomial fp;      //Derivative of the polynomial

private Complex root;
private int numIterations;
private int err;


//Basic constructor. Calculate and set fp in this method.

public Newton(Polynomial p) {
  this.f = p;
  this.fp = p.derivative();
}

// Newton-Rapshon method



public void iterate(Complex z0) {
      Complex[] z = new Complex[MAXITER];
      z[0] = z0;
        for(int i = 0 ; i<MAXITER ; i++){
        if(fp.evaluate(z[i]).abs() <= TOL){
          this.err = -1;
          return;
        }
      z[i+1] = z[i].add((f.evaluate(z[i])).divide(fp.evaluate(z[i])));
        if(f.evaluate(z[i]).abs() != TOL){
          this.err = -2;
          return;
      } if (f.evaluate(z[i]).abs() <= TOL){
          this.err = 0;
          this.root = z[i];
          this.numIterations = i;
          return;
      }
    }}

    // Tester function.
    public static void main(String[] args) {
        // Basic tester: find a root of f(z) = z^3-1 from the starting point
        // z_0 = 1+i.
        Complex[] coeff = new Complex[] { new Complex(-1.0,0.0), new Complex(),
                                          new Complex(), new Complex(1.0,0.0) };
        Polynomial p    = new Polynomial(coeff);
        Newton     n    = new Newton(p);

    Complex z0 = new Complex(1.0,1.0);
        n.iterate(z0);
        System.out.println(n.root);
    }}
dwara s
  • 1
  • 1
  • That code will not compile, it looks like you're missing a method signature after the constructor. Anyway, did you try debugging? – daniu Mar 19 '19 at 14:17
  • 2
    You have lots of code outside of any function. How would that even build? Please create a [mcve] to show us, that builds and exhibits your problem. – Some programmer dude Mar 19 '19 at 14:17
  • Your `for-loop` isn't inside any method. I would assume your code doesn't even compile – XtremeBaumer Mar 19 '19 at 14:17
  • 1
    You probably don't reach the line where `this.root` is set because '==' on `double` is higly dangerous. Prefer a method which accepts an error margin. – Arnaud Denoyelle Mar 19 '19 at 14:18
  • 1
    The above comments are valid, but the crux of your issue is that root, or rather n.root is private. It won’t be accessible in ‘static void main’. You either make it public, or provide a ‘public getter’ that returns the private root value. – Chris Adams Mar 19 '19 at 14:20
  • I edited the '==' to '<=' because that makes more sense! and I changed the last line to `System.out.println(n.getRoot());` and it seems to work now – dwara s Mar 19 '19 at 14:26
  • Check `err` before checking `root`, you are not handling errors at the moment. Only if `err == 0` you should check `root`. Consider using exceptions for error handling. – Karol Dowbecki Mar 19 '19 at 14:27
  • I suspect you return form `iterate` at this line : `if(f.evaluate(z[i]).abs() != TOL).` See [How to compare two double values in Java?](https://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java) – Arnaud Denoyelle Mar 19 '19 at 14:37

1 Answers1

0

You need to create a getter for root in your Newton class since it is a private field.

class Newton {

public static final int MAXITER = 20;
public static final double TOL = 1.0e-10;


private Polynomial f;        //Polynomial
private Polynomial fp;      //Derivative of the polynomial

private Complex root;
private int numIterations;
private int err;


//Basic constructor. Calculate and set fp in this method.

public Newton(Polynomial p) {
  this.f = p;
  this.fp = p.derivative();
}
public Complex getRoot(){
  return this.root;
}
}

You can then use the getter instead of trying to access n.root (i.e., using newtonObj.getRoot() instead of newtonObj.root):

// Tester function.
public static void main(String[] args) {
    // Basic tester: find a root of f(z) = z^3-1 from the starting point
    // z_0 = 1+i.
    Complex[] coeff = new Complex[] { new Complex(-1.0,0.0), new Complex(),
                                      new Complex(), new Complex(1.0,0.0) };
    Polynomial p    = new Polynomial(coeff);
    Newton     n    = new Newton(p);

Complex z0 = new Complex(1.0,1.0);
    n.iterate(z0);
    System.out.println(n.getRoot());
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80