package Lecture19; class FuncA implements MathFunction { public double f(double x) { return x*x - 2; } } public class Bracket { public static boolean zbrac(MathFunction func, double[] x) { // Versión Java de zbrac, p.352, RN if (x[0] == x[1]) { System.out.println("Rango inicial de zbrac erróneo"); return false; } double f0= func.f(x[0]); double f1= func.f(x[1]); for (int j= 0; j < NTRY; j++) { if (f0*f1 < 0.0) return true; if (Math.abs(f0) < Math.abs(f1)) { x[0] += FACTOR*(x[0]-x[1]); f0= func.f(x[0]); } else { x[1] += FACTOR*(x[1]-x[0]); f1= func.f(x[1]); } } return true; } public static double FACTOR= 1.6; public static int NTRY= 50; public static void main(String[] args) { double[] bound= {5.0, 6.0}; // Suposición inicial (errónea) de la acotación boolean intervalFound= zbrac(new FuncA(), bound); System.out.println("¿Acotación encontrada? " + intervalFound); if (intervalFound) System.out.println("Límite inferior: " + bound[0] + " límite superior: " + bound[1]); System.exit(0); } }