0
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?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
Alex Titov
  • 91
  • 1
  • 2
  • 9

2 Answers2

3

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");
Javier Martín
  • 2,537
  • 10
  • 15
  • Can i use UnivariateFunction form org.apache.commons.math3? If I want to use a sin(x), should I decompose it in a Taylor series? – Alex Titov Jul 05 '17 at 17:42
0

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 !

Alex Titov
  • 91
  • 1
  • 2
  • 9