Example: Zeros of a Univariate Function

In this example 3 zeros of the sin function are found.
import com.imsl.math.*;

public class ZeroFunctionEx1 {
    public static void main(String args[]) {
        
        ZeroFunction.Function fcn = new ZeroFunction.Function() {
            public double f(double x) {
                return Math.sin(x);
            }
        };
        
        ZeroFunction zf = new ZeroFunction();
        double guess[] = {5, 18, -6};
        double zeros[] = zf.computeZeros(fcn, guess);
        for (int k = 0;  k < zeros.length;  k++) {
            System.out.println(zeros[k]+" = "+(zeros[k]/Math.PI) + " pi");
        }
    }
}

Output

6.283185307179564 = 1.999999999999993 pi
18.84955592156295 = 6.0000000000077 pi
-6.283185307179641 = -2.0000000000000173 pi
Link to Java source.