2016-12-31 37 views
2

我正在尝试使用commons-math库进行一些数值分化任务。我使用DerivativeStructures构建了一个非常简单的函数,我认为它可以工作;显然我错了。公共数学差异化结果为0

public static void main(String[] args) { 
    DerivativeStructure x0 = new DerivativeStructure(2, 2, 2.0); 
    DerivativeStructure y0 = new DerivativeStructure(2, 2, 4.0); 
    DerivativeStructure xi = x0.pow(2); 
    DerivativeStructure yi = y0.pow(2); 
    DerivativeStructure f = xi.add(yi); 

    System.out.println(f.getValue()); 
    System.out.println(f.getPartialDerivative(1, 0)); // (?) 
    System.out.println(f.getPartialDerivative(0, 1)); // (?) 
} 

我试图获得第一和第二顺序的多变量函数f(x)的偏导数= X^2 + Y^2在点(2.0,4.0)。因此,我期望df/dx为4.0,df/dy为8.0,作为一阶偏好。 2.0为二次部分。然而,我得到了正确的f(x,y)值,我甚至没有从这个javadoc中得到任何想法。我在这里看到了一些关于stackoverflow的问题,其中有一些关于commons-math的不透明文档的评论,但不是关于多元函数的工作示例。单变量我可以解决,但不是这个...

任何提示将不胜感激!

回答

2

在你的代码中,你并没有真正指定2个独立变量x0,y0但只有1.对于DerivativeStructure x0,y0实际上被视为函数本身,这取决于变量的隐式向量p。对于每个独立变量,您必须给自变量向量赋予不同的索引。什么,你需要做的是:

DerivativeStructure x0 = new DerivativeStructure(2, 2, 0, 2.0); 
DerivativeStructure y0 = new DerivativeStructure(2, 2, 1, 4.0); 

当第三个参数(S)0和1表示在p矢量因此两个不同的独立变量2个不同的索引。如果在创建DerivativeStructure时忽略此参数,则在代码x0中假设为0,则y0不是独立的。

Further Reading