- package lobos.never.die;
-
- interface numerical_function {
- public double f(double x);
- }
-
- interface function {
- public double f(double ... x);
- }
-
- public class Numerical {
-
- public static void main(String[] args) {
- numerical_function f = new numerical_function() {
- public double f(double x) { return Math.exp(x)-2; }
- };
-
- System.out.println(nderivative(f, 1));
- }
-
- public static double nderivative(numerical_function f, double x) {
-
- final double h = 1/67108864.;
- return (f.f(x+h) - f.f(x-h))/2/h;
-
- }
-
- public static double nintegrate(numerical_function f, double a, double b) {
- return 1.;
- }
-
- public static double newtonrhapson(numerical_function f, double x) {
- int iterations = 1000, is_zero_iterations = 20;
-
- double fprime;
-
- do {
- do {
- fprime = nderivative(f, x);
- x += 1/67108864.;
- } while (fprime == 0 && is_zero_iterations-- != 0);
-
- x = x - f.f(x)/fprime;
-
- } while (f.f(x) > 1/67108864. && iterations-- != 0);
-
- return x;
- }
- }