【问题标题】:How to solve 2D integral?如何求解二维积分?
【发布时间】:2012-04-24 14:09:41
【问题描述】:

我一直在尝试为双积分实现梯形规则。我尝试了很多方法,但我无法让它正常工作。

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;
}

我了解单变量积分的方法,但我不知道如何对二维积分执行此方法,例如:x + (y*y)。 有人可以简单解释一下吗?

【问题讨论】:

  • 您是在问如何应用这种近似技术来求解体积 f(x,y) = x + y^2(或其他一些 f(x,y))上的积分?跨度>
  • 我只是做了一些简单的 f(x,y) 函数的例子,因为我不明白它的原理。

标签: java numerical-methods numerical


【解决方案1】:

有很多方法可以做到这一点。

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

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

这样,您基本上可以拥有任意数量的维度。虽然它的扩展性很差。对于较大的问题,可能需要使用monte carlo integration

【讨论】:

  • 这个方法很好。如果您发布代码,我们可以看看是否有人发现问题。
  • 这是我的尝试,与您的建议类似,但是当我签入 Mathematica 时,它并没有加起来
  • static double f(double x, double y) { return x * (y+y); } // trapezoid rule static double trapezoid(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 &lt; N; k++) { sum = sum + h * f(a + h*k, a); for(int j = 1; j &lt; N; j++) { sum1 = sum1 + h * f(a + h*k, a + j*k); } } return sum + sum1; }
  • 我觉得你应该写出来。 (也可以将其添加到问题中以获得良好的格式)。我认为问题在于您的金额。它应该是double sum = 0.5 * h * (sum1(at x=a) + sum1(at x=b));double sum1 = 0.5 * h * (f(current x,a) + f(current x,b));
  • 也应该是sum = sum + h*sum1(at current x) - 你返回的是两个一维积分的总和,而不是你要求的。
【解决方案2】:

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

// 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;
}

希望这会有所帮助。如果您对我的代码有任何疑问,请随时提出(警告:代码未经测试)。

【讨论】:

  • thx null0pointer,如果我有问题,我会实现你的代码并回复你
  • 你的代码完美运行,我不得不说一个非常聪明的方法,避免混乱的内部总和
【解决方案3】:

考虑使用DataMelt Java program 中的jhplot.F2D 类。您可以集成和可视化 2D 函数,执行以下操作:

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

【讨论】:

    猜你喜欢
    • 2011-01-23
    • 2022-11-29
    • 2019-11-22
    • 2022-11-14
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    相关资源
    最近更新 更多