简体   繁体   中英

How to use standard SimpsonIntegrator in import org.apache.commons.math3.analysis.integration.SimpsonIntegrator;

public double evalute(double distance){

    /**
     * equation (3.2)
     */
    this.from = 0;
    this.to = distance;
    this.n = 2;
    return - 10 * Math.log10(Math.exp(-IntSimpson(this.from, this.to, this.n)));
}

There is IntSimpson() function i designed manually, but I want to use standard library! How can i do it and where it can be found?

If you want to actually use the integrator object, you need to call the integrate method , which takes an instance of UnivariateFunction . If you are on Java 8, this is a single-method interface, so it is automatically a functional interface. Thus, you can pass a lambda or a method reference, as in:

final SimpsonIntegrator si = new SimpsonIntegrator();
final double result = si.integrate(50, x -> 2*x, 0, 10);
System.out.println(result + " should be 100");

Otherwise, you have to create an implementation of the interface yourself, either by having a class implement it, or by using an anonymous class:

final double result = si.integrate(50, new UnivariateFunction() {
        @Override public double value(double x) {
            return 2*x;
        }
    }, 0, 10);
System.out.println(result + " should be 100");

It works!

final SimpsonIntegrator si = new SimpsonIntegrator();
final double result1 = si.integrate(10, x -> 2*Math.pow(x, 1), 0.0, 10.0);
System.out.println(result1 + " should be 100.0");
final double result2 = si.integrate(1000, x -> Math.sin(x), 0.0, Math.PI);
System.out.println(result2 + " should be 2.0000...");

Thanks Javier Martín !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM