0
我正在使用Apache Commons Math Library 3.4
。我已經實現了一個單變量函數名稱PFunction
。在我的班級中,我試圖解決2 * p * Math.log(p)/(p + 1) -w
,其中w
是-0.5
。無法使用Apache Commons Math庫解決單變量函數
不過,我得到的錯誤:
org.apache.commons.math3.exception.NoBracketingException: function values at endpoints do not have different signs, endpoints: [1, 1,000], values: [0.5, 14.302]
at org.apache.commons.math3.analysis.solvers.BrentSolver.doSolve(BrentSolver.java:122)
at org.apache.commons.math3.analysis.solvers.BaseAbstractUnivariateSolver.solve(BaseAbstractUnivariateSolver.java:195)
at org.apache.commons.math3.analysis.solvers.BaseAbstractUnivariateSolver.solve(BaseAbstractUnivariateSolver.java:200)
at Main.main(Main.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
我已經嘗試過其他的求解器,例如穆勒的方法,他們也顯示了同樣的錯誤。另外,我發現w的正值,例如0.5,不會給我一個錯誤。謝謝你的幫助。
import org.apache.commons.math3.analysis.UnivariateFunction;
public class PFunction implements UnivariateFunction {
private double w;
public PFunction(double w) {
this.w = w;
}
@Override
public double value(double p) {
return 2 * p * Math.log(p)/(p + 1) -w;
}
}
主要類:
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.solvers.*;
public class Main {
public static void main(String[] args) {
double p;
UnivariateFunction univariateFunction = new PFunction(-0.5);
BrentSolver brentSolver = new BrentSolver();
try {
p = brentSolver.solve(1000, univariateFunction, 1, 1000);
System.out.println("Brent solver, p: " + p);
} catch (Exception e) {
e.printStackTrace();
}
}
}