我在使用 apache commons 数学库时遇到问题。
我只想创建像 f(x) = 4x^2 + 2x 这样的函数,并且我想计算这个函数的导数
--> f'(x) = 8x + 2
我读过关于差异化的文章(http://commons.apache.org/proper/commons-math/userguide/analysis.html,第 4.7 节)。
有一个例子我不太明白:
int params = 1;
int order = 3;
double xRealValue = 2.5;
DerivativeStructure x = new DerivativeStructure(params, order, 0, xRealValue);
DerivativeStructure y = f(x); //COMPILE ERROR
System.out.println("y = " + y.getValue();
System.out.println("y' = " + y.getPartialDerivative(1);
System.out.println("y'' = " + y.getPartialDerivative(2);
System.out.println("y''' = " + y.getPartialDerivative(3);
在第 5 行中,当然会发生编译错误。功能f(x)
被调用但未定义。我做错了什么?
有没有人对 apache commons 数学库的微分/推导有任何经验,或者有人知道另一个可以帮助我的库/框架吗?
Thanks
在该示例下面的段落中,作者描述了创建的方法DerivativeStructure
s。这不是魔法。在您引用的示例中,应该有人编写该函数f
。嗯,那不是很清楚。
用户可以通过多种方式创建 UnivariateDifferentiableFunction 接口的实现。第一种方法是简单地使用 DerivativeStructure 中的适当方法直接编写它来计算加法、减法、正弦、余弦...这通常非常简单,无需记住微分规则:用户代码仅代表函数本身,差异将在后台自动计算。第二种方法是编写经典的 UnivariateFunction 并将其传递给 UnivariateFunctionDifferentiator 接口的现有实现,以检索同一函数的微分版本。第一种方法更适合用户已经控制所有底层代码的小功能。第二种方法更适合使用 DerivativeStructure API 编写起来很麻烦的大型函数,或者用户无法控制完整底层代码的函数(例如调用外部库的函数)。
使用第一个想法。
// Function of 1 variable, keep track of 3 derivatives with respect to that variable,
// use 2.5 as the current value. Basically, the identity function.
DerivativeStructure x = new DerivativeStructure(1, 3, 0, 2.5);
// Basically, x --> x^2.
DerivativeStructure x2 = x.pow(2);
//Linear combination: y = 4x^2 + 2x
DerivativeStructure y = new DerivativeStructure(4.0, x2, 2.0, x);
System.out.println("y = " + y.getValue());
System.out.println("y' = " + y.getPartialDerivative(1));
System.out.println("y'' = " + y.getPartialDerivative(2));
System.out.println("y''' = " + y.getPartialDerivative(3));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)