Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何求解二维积分?_Java_Numerical Methods_Numerical - Fatal编程技术网

Java 如何求解二维积分?

Java 如何求解二维积分?,java,numerical-methods,numerical,Java,Numerical Methods,Numerical,我一直在尝试实现二重积分的梯形规则。我已经尝试了很多方法,但我不能让它正常工作 static double f(double x) { return Math.exp(- x * x / 2); } // trapezoid rule static double trapezoid(double a, double b, int N) { double h = (b - a) / N; double sum = 0.5 * h * (f(a) + f(b));

我一直在尝试实现二重积分的梯形规则。我已经尝试了很多方法,但我不能让它正常工作

static double f(double x) {
    return Math.exp(- x * x / 2);
}

// trapezoid rule
static double trapezoid(double a, double b, int N) {
    double h = (b - a) / N;
    double sum = 0.5 *  h * (f(a) + f(b));
    for (int k = 1; k < N; k++)
        sum = sum + h * f(a + h*k);
    return sum;
}
静态双f(双x){
返回数学表达式(-x*x/2);
}
//梯形法则
静态双梯形(双a、双b、内N){
双h=(b-a)/N;
双和=0.5*h*(f(a)+f(b));
对于(int k=1;k
我了解单变量积分的方法,但我不知道如何处理二维积分,比如:x+(y*y)。 有人能简单地解释一下吗?

有很多方法

如果你已经知道1d,你可以这样做:

  • 编写一个函数g(x),计算固定
    x
    f(x,y)上的1d积分
  • 然后积分g(x)上的一维积分
  • 成功:)

  • 这样你基本上可以拥有你喜欢的任意多个维度。虽然它的规模很小。对于较大的问题,可能需要使用。

    如果您打算使用梯形规则,那么您可以这样做:

    // The example function you provided.
    public double f(double x, double y) {
        return x + y * y;
    }
    
    /**
     * Finds the volume under the surface described by the function f(x, y) for a <= x <= b, c <= y <= d.
     * Using xSegs number of segments across the x axis and ySegs number of segments across the y axis. 
     * @param a The lower bound of x.
     * @param b The upper bound of x.
     * @param c The lower bound of y.
     * @param d The upper bound of y.
     * @param xSegs The number of segments in the x axis.
     * @param ySegs The number of segments in the y axis.
     * @return The volume under f(x, y).
     */
    public double trapezoidRule(double a, double b, double c, double d, int xSegs, int ySegs) {
        double xSegSize = (b - a) / xSegs; // length of an x segment.
        double ySegSize = (d - c) / ySegs; // length of a y segment.
        double volume = 0; // volume under the surface.
    
        for (int i = 0; i < xSegs; i++) {
            for (int j = 0; j < ySegs; j++) {
                double height = f(a + (xSegSize * i), c + (ySegSize * j));
                height += f(a + (xSegSize * (i + 1)), c + (ySegSize * j));
                height += f(a + (xSegSize * (i + 1)), c + (ySegSize * (j + 1)));
                height += f(a + (xSegSize * i), c + (ySegSize * (j + 1)));
                height /= 4;
    
                // height is the average value of the corners of the current segment.
                // We can use the average value since a box of this height has the same volume as the original segment shape.
    
                // Add the volume of the box to the volume.
                volume += xSegSize * ySegSize * height;
            }
        }
    
        return volume;
    }
    
    //您提供的示例函数。
    公共双f(双x,双y){
    返回x+y*y;
    }
    /**
    
    *查找a的函数f(x,y)描述的曲面下的体积考虑使用中的类
    jhplot.F2D
    。您可以集成和可视化二维函数,例如:

    f1=F2D("x*y",-1,1,-1,1) # define in a range
    print f1.integral()
    

    你是在问如何应用这种近似技术来求解体积f(x,y)=x+y^2(或其他f(x,y))上的积分吗?我只是编了一个简单的f(x,y)函数的例子,因为我不理解它的原理。这个方法很好。如果你发布你的代码,我们可以看看是否有人发现了问题。这是我的尝试,simimar对你的建议表示赞同,但当我在Mathematica中检查时,它不符合
    static double f(double x,double y y){return x*(y+y);}//梯形规则static double梯形(double a,double b,int N){double h=(b-a)/N;double sum=0.5*h*(f(a,a)+f(b,b));double sum1=0.5*h*(f(a,a)+f(b,b));for(int k=1;k
    我认为你应该把它写出来。(也可以把它添加到问题中,以获得更好的格式)。我认为问题在于你的求和。它应该是
    双和=0.5*h*(sum1(x=a)+sum1(x=b));
    以及
    双和=0.5*h*(f(当前x,a)+f(当前x,b))
    它也应该是
    sum=sum+h*sum1(当前x)
    -您返回的是两个1d积分的和,而不是您要求的。thx null0pointer,如果我有一些问题,我将执行您的代码并返回给您您。您的代码工作得很好,我必须说一个非常聪明的方法,避免混乱的内部和