-1

I am implementing the bisection method, and as a first step I need to evaluate the polynomial function, but I do not get the correct result.

The polynomial used is 3(x^2)+7(x)+1
with x=2, the result should be 27

public static double evaluaFx(int []coef, int grade, int x) { 
   //int x viene siendo los x0,xf.xmenter code here
   double Fx=0;
   //System.out.println("grade"+grade+"x"+x);
   //for (int i = grade; i >=0; i--) {
   //   System.out.println(coef[i]);
   //}
   for (int i = grade; i>=0; i--) {
      Fx= Math.pow((coef[i]*x), grade);
      // System.out.println(Fx+"mas"+"("+coef[i]+x+") a la grado"+grade);
   }
   return Fx;
}

What's wrong with above algorithm ?

T.Gounelle
  • 5,603
  • 1
  • 19
  • 31
Frank R
  • 1
  • 1

1 Answers1

1

Several things are wrong in your algorithm:

Formula for one grade (step i) should be:

 // for instance, if i=2, you want 3 * x^2 which translate to
 Fx = coef[i]*Math.pow(x, i);

Then in the for loop you don't add up successive grade computations, loosing at each step the previous result. You should do something like (note the += instead of =)

 Fx += coef[i]*Math.pow(x, i);
T.Gounelle
  • 5,603
  • 1
  • 19
  • 31