package Lecture19; class FuncA implements MathFunction { public double f(double x) { return x*x - 2; } } public class RootFinder1 { public static double bisect(MathFunction func, double x1, double x2, double epsilon) { double m; for (m= (x1+x2)/2.0; Math.abs(x1-x2) > epsilon; m= (x1+x2)/2.0) if (func.f(x1)*func.f(m) <= 0.0) // Si la función atraviesa el cero en el sub izquierdo x2= m; // Usar subintervalo izquierdo else x1= m; // Usar subintervalo derecho return m; } public static void main(String[] args) { // MathFunction ff= new FuncA(); // Esta alternativa funciona (puede usar también FuncA ff=...) // double root= RootFinder.bisect(ff, -8.0, 8.0, 0.0001); double root= RootFinder1.bisect(new FuncA(), -8.0, 8.0, 0.0001); System.out.println("Raíz: " + root); System.exit(0); } }