# Polynomial Bisection Help

• September 28th, 2013, 05:01 PM
Polynomial Bisection Help
Hello guys. I'm trying to make a bisection method in my program I keep getting an error that tells me that I can't reference that method because I can't access a non-static method bisection can't be referenced from a static context. That basically means that I can't access the method unless I make my arrays static but that changes the values from within as well. Its easier to explain if you try running it and changing the accessibility. Also, I don;t know if I'm even doing the Bisection method right or not. I've spent days on this program, is there something I can do to fix this without starting all over?

Code :

``` import java.util.Scanner;   public class Polynomial {   int [] coefficients = new int[3]; int [] powers = new int[3];     public static void main(String[] args) { int c1, c2, c3; Scanner input = new Scanner(System.in);   /*for(int i = 0; i<coefficents.length-1;i++) {         } */   Polynomial P1 = new Polynomial(4, 5, -3, 3, 1, 0); Polynomial P2 = new Polynomial(1, 1, -2, 3, 1, 0);   System.out.println("Here is the Polynomial 1: " + P1); System.out.println("Here is the Polynomial 2: " + P2); System.out.println("P1 eval = " + P1.evalAt(-1)); System.out.println("P2 eval = " + P2.evalAt(1));   System.out.println("Bisection " + bisection(P1, P2)); System.out.println("================================================");   P1.derivative(); System.out.println("Here is the Derivative for P1: " + P1); P2.derivative(); System.out.println("Here is the Derivative for P2: " + P2);           }   Polynomial(int a, int b, int c, int x, int y, int z) { coefficients[0] = a; coefficients[1] = b; coefficients[2] = c;   powers[0] = x; powers[1] = y; powers[2] = z; }   public static int evalAt(int x) { int sum = 0; for(int i = coefficients.length-1; i>=0;i--) { sum = coefficients[i] + x * sum; //System.out.print(sum + "\t"); } return sum; }   public void derivative() // Evaluates the Derivative { for(int i = 0; i<=2;i++) { coefficients[i] = powers[i] * coefficients[i]; powers[i]--; } }   public double bisection(Polynomial a, Polynomial b) { double tolerance = 0.000001; double m, ya, yb; double result = 0; int i = 0;   double A = a.evalAt(1); double B = b.evalAt(2);   while( B-A > tolerance) { m = (A+B)/2;   ya = m * m - coefficients[2]; yb = A * A - coefficients[2];   if(ya > 0 && ya < 0 || yb <0 && yb > 0 ) { B = m; } else { A = m; } System.out.println((A+B)/2); } result = (A+B)/2; return result;   }     public String toString() { String s = ""; for(int i=0;i<coefficients.length;i++) { if(powers[i]==0) { s = s + "+" + coefficients[i];   } else if(powers[i]>0) s = s + "+" + (coefficients[i]); else if(powers[i]<0) { s = s + "-" + (-coefficients[i]); } if(i == 1) s = s + "x^" + powers[i]; else if(i > 1) s = s + "x^" + powers[i]; //else if(i == coefficients.length-1)   else s = coefficients[i] + "x^" + powers[i]; } return s; } }```

Sample:

Code :

```Polynomial.java:31: error: non-static method bisection(Polynomial,Polynomial) cannot be referenced from a static context System.out.println("Bisection " + bisection(P1, P2)); ^ Polynomial.java:59: error: non-static variable coefficients cannot be referenced from a static context for(int i = coefficients.length-1; i>=0;i--) ^ Polynomial.java:61: error: non-static variable coefficients cannot be referenced from a static context sum = coefficients[i] + x * sum; ^ 3 errors```
• September 28th, 2013, 05:19 PM
Norm
Re: Polynomial Bisection Help
The static method main() can NOT access any non-static methods or variables in the class. non-static methods and variables are only accessible to methods in instances of the class.
Solutions:
1) Move the code that access the methods out of main to a method in the class. Have main() create an instance of the class and use the reference to the class to call the method.
2) Make everything static so no instance of the class is required.
• September 28th, 2013, 05:28 PM
Re: Polynomial Bisection Help
Quote:

Originally Posted by Norm
The static method main() can NOT access any non-static methods or variables in the class. non-static methods and variables are only accessible to methods in instances of the class.
Solutions:
1) Move the code that access the methods out of main to a method in the class. Have main() create an instance of the class and use the reference to the class to call the method.
2) Make everything static so no instance of the class is required.

Yeah, youre right. I had to make my bisection( ) method an instance of my polynomial class. I stored the evalAt ( ) object results into a variable in which I was able to use as an argument to pass through my bisection method. No changes affected on my array. Since I was able to pass everything correctly, I can focus more on getting the overall Bisection method correctly.

Here is my fully functional program: Do keep in mind that the toString ( ) shows +- or --. Ill work on that later.

Code :

``` import java.util.Scanner;   public class Polynomial {   int [] coefficients = new int[3]; int [] powers = new int[3];     public static void main(String[] args) { int c1, c2, c3; Scanner input = new Scanner(System.in);   /*for(int i = 0; i<coefficents.length-1;i++) {         } */   Polynomial P1 = new Polynomial(4, 5, -3, 3, 1, 0); Polynomial P2 = new Polynomial(1, 1, -2, 3, 1, 0);   System.out.println("Here is the Polynomial 1: " + P1); System.out.println("Here is the Polynomial 2: " + P2); int c = P1.evalAt(5); int d = P2.evalAt(0); System.out.println("P1 eval = " + c); System.out.println("P2 eval = " + d);   System.out.println("Bisection " + P1.bisection(c, d)); System.out.println("================================================");   P1.derivative(); System.out.println("Here is the Derivative for P1: " + P1); P2.derivative(); System.out.println("Here is the Derivative for P2: " + P2);           }   Polynomial(int a, int b, int c, int x, int y, int z) { coefficients[0] = a; coefficients[1] = b; coefficients[2] = c;   powers[0] = x; powers[1] = y; powers[2] = z; }   public int evalAt(int x) { int sum = 0; for(int i = coefficients.length-1; i>=0;i--) { sum = coefficients[i] + x * sum; //System.out.print(sum + "\t"); } return sum; }   public void derivative() // Evaluates the Derivative { for(int i = 0; i<=2;i++) { coefficients[i] = powers[i] * coefficients[i]; powers[i]--; } }   public double bisection(double a, double b) { double tolerance = 0.000001; double m, ya, yb; double result = 0; int i = 0;   double A = a; double B = b;   while( B-A > tolerance) { m = (A+B)/2;   ya = m * m - coefficients[2]; yb = A * A - coefficients[2];   if(ya > 0 && ya < 0 || yb <0 && yb > 0 ) { B = m; } else { A = m; } System.out.println((A+B)/2); } result = (A+B)/2; return result;   }     public String toString() { String s = ""; for(int i=0;i<coefficients.length;i++) { if(powers[i]==0) { s = s + "+" + coefficients[i];   } else if(powers[i]>0) s = s + "+" + (coefficients[i]); else if(powers[i]<0) { s = s + "-" + (-coefficients[i]); } if(i == 1) s = s + "x^" + powers[i]; else if(i > 1) s = s + "x^" + powers[i]; //else if(i == coefficients.length-1)   else s = coefficients[i] + "x^" + powers[i]; } return s; } }```

Code :

``` ----jGRASP exec: java Polynomial   Here is the Polynomial 1: 4x^3+5x^1+-3x^0 Here is the Polynomial 2: 1x^3+1x^1+-2x^0 P1 eval = -46 P2 eval = 1 -10.75 -4.875 -1.9375 -0.46875 0.265625 0.6328125 0.81640625 0.908203125 0.9541015625 0.97705078125 0.988525390625 0.9942626953125 0.99713134765625 0.998565673828125 0.9992828369140625 0.9996414184570312 0.9998207092285156 0.9999103546142578 0.9999551773071289 0.9999775886535645 0.9999887943267822 0.9999943971633911 0.9999971985816956 0.9999985992908478 0.9999992996454239 0.999999649822712 Bisection 0.999999649822712 ================================================ Here is the Derivative for P1: 12x^2+5x^0-0x^-1 Here is the Derivative for P2: 3x^2+1x^0-0x^-1```