Thursday, March 5, 2015

Secant method in Java

Wikipedia

public Root secant(double starting, double ending) {

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

This is part of the RootApproximations project.

No comments: