Thursday, March 5, 2015

Newton-Raphson method in Java

Wikipedia

public Root newtonRaphson(double starting) {

    double x0 = starting;
    double x1;
    int loops = 0;
    while (true) {
        loops++;
        x1 = newton(x0);
        if (Math.abs(x1 - x0) < e ) {
            return new Root(x1, loops);
        }
        x0 = x1;
    }
}
private double newton(double x0) {
    return (x0 - (function.f(x0) / function.fd(x0)));
}


This is part of the RootApproximations project.

No comments: