【问题标题】:How to use standard SimpsonIntegrator in import org.apache.commons.math3.analysis.integration.SimpsonIntegrator;如何在导入 org.apache.commons.math3.analysis.integration.SimpsonIntegrator 中使用标准 SimpsonIntegrator;
【发布时间】:2017-12-06 02:54:01
【问题描述】:
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)));
}

我手动设计了 IntSimpson() 函数,但我想使用标准库!我该怎么做?在哪里可以找到?

【问题讨论】:

    标签: java integration apache-commons numerical-integration numerical-computing


    【解决方案1】:

    如果要实际使用积分器对象,则需要调用integrate method,它采用UnivariateFunction 的实例。如果你在 Java 8 上,这是一个单方法接口,所以它自动是一个函数式接口。因此,您可以传递 lambda 或方法引用,如下所示:

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

    否则,您必须自己创建接口的实现,要么让类实现它,要么使用匿名类:

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

    【讨论】:

    • 我可以使用 org.apache.commons.math3 形式的 UnivariateFunction 吗?如果我想使用 sin(x),我应该将其分解为泰勒级数吗?
    【解决方案2】:

    有效!

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

    谢谢Javier Martín

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-11
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      相关资源
      最近更新 更多