Alex Titov
Alex Titov

Reputation: 101

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?

Upvotes: 0

Views: 626

Answers (2)

Alex Titov
Alex Titov

Reputation: 101

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 !

Upvotes: 0

Javier Martín
Javier Martín

Reputation: 2605

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");

Upvotes: 3

Related Questions