Example: Zeros of a Univariate Function

In this example 3 zeros of the sin function are found.

import com.imsl.math.*;

public class ZerosFunctionEx1 {

    public static void main(String args[]) {

        ZerosFunction.Function fcn = new ZerosFunction.Function() {
            public double f(double x) {
                return Math.sin(x);
            }
        };

        ZerosFunction zf = new ZerosFunction();
        double guess[] = {5, 18, -6};
        zf.setGuess(guess);
        double zeros[] = zf.computeZeros(fcn);
        for (int k = 0; k < zeros.length; k++) {
            System.out.println(zeros[k] + " = " + (zeros[k] / Math.PI) + " pi");
        }
    }
}

Output

-2.953824405025999E-22 = -9.40231510170729E-23 pi
3.1415926535897936 = 1.0000000000000002 pi
6.283185307179586 = 2.0 pi
Link to Java source.