package Lecture19a; class FuncB implements MathFunction2 { public double fn(double x) { return x*x - 2; } public double fd(double x) { return 2*x; } } public class Newton { public static double newt(MathFunction2 func, double a, double b, double epsilon) { double guess= 0.5*(a + b); for (int j= 0; j < JMAX; j++) { double fval= func.fn(guess); double fder= func.fd(guess); double dx= fval/fder; guess -= dx; System.out.println(guess); if ((a - guess)*(guess - b) < 0.0) { System.out.println("Error: fuera de la acotación"); return ERR_VAL; } if (Math.abs(dx) < epsilon) return guess; } System.out.println("Superado el número máximo de iteraciones"); return guess; } public static final int JMAX= 50; public static final double ERR_VAL= -10E10; public static void main(String[] args) { double root= Newton.newt(new FuncB(), -0.0, 8.0, 10E-14); System.out.println("Root B: " + root); System.out.println(); } }