如何用apache常用数学计算积分3.3

时间:2014-05-27 08:20:19

标签: java integral apache-commons-math

我想用apache commonmath3.3 lib计算一些实验的积分。

我看到了this question.

SimpsonIntegrator simpson = new SimpsonIntegrator();
TrapezoidIntegrator trapezoid = new TrapezoidIntegrator();
double[] vector = new double[2];
vector[0] = 0;
vector[1] = 1;

PolynomialFunction f = new PolynomialFunction(vector);
UnivariateFunction uf = (UnivariateFunction)new PolynomialFunction(vector);
System.out.println("To String " + uf.toString());
System.out.println("Degree: " + f.degree());

double i = simpson.integrate(10, uf, -Float.MAX_VALUE, Float.MAX_VALUE);
double j = trapezoid.integrate(10, uf, 0, 1);
System.out.println("Simpson integral : " + i);        
System.out.println("Trapezoid integral : " + j);   

我认为我们必须为整数的间隔创建一个双数组。但是当我想创建大于2的数组时,我会给出这样的信息:

Caused by:org.apache.commons.math3.exception.TooManyEvaluationsException: illegal state: maximal        count (10) exceeded: evaluations 05-27 08:11:33.461: E/AndroidRuntime(1349):    at     org.apache.commons.math3.analysis.integration.BaseAbstractUnivariateIntegrator.computeObjec tiveValue(BaseAbstractUnivariateIntegrator.java:218) 

我错过了什么吗?

如何为积分声明函数?
例如,我如何声明" x ^ 2 + 3"积分功能?

1 个答案:

答案 0 :(得分:1)

在更复杂的多项式函数的情况下,您需要增加积分器允许的最大计算次数。

尝试这样的事情:

SimpsonIntegrator simpson = new SimpsonIntegrator();
TrapezoidIntegrator trapezoid = new TrapezoidIntegrator();
double[] vector = new double[3];
vector[0] = 0;
vector[1] = 1;
vector[2] = 2;

PolynomialFunction f = new PolynomialFunction(vector);
UnivariateFunction uf = (UnivariateFunction)new PolynomialFunction(vector);

double i = simpson.integrate(10, uf, -Float.MAX_VALUE, Float.MAX_VALUE);
double j = trapezoid.integrate(10000, uf, 0, 1);
System.out.println("Simpson integral : " + i);        
System.out.println("Trapezoid integral : " + j);