Java - 辛普森的方法和错误

Java - Simpson's method and error

我正在为 Simpson 方法编写 Java 程序。基本程序按预期工作,尽管我无法使(绝对)错误部分工作。

我想我需要以不同的方式引用我的 while (absError < 0.000001) 循环。我究竟做错了什么?

第一次尝试

public static double function(double x, double s) {
    double sech = 1 / Math.cosh(x); // Hyperbolic cosecant
    double squared = Math.pow(sech, 2);
    return ((Math.pow(x, s)) * squared);
 }

 // Simpson's rule - Approximates the definite integral of f from a to b.
 public static double SimpsonsRule(double a, double b, double s, int n) {
    double dx, x, sum4x, sum2x;
    double absError = 1.0;
    double simpson = 0.0;
    double simpson2 = 0.0;

    dx = (b-a) / n;
    sum4x = 0.0;
    sum2x = 0.0;

    // 4/3 terms
    for (int i = 1; i < n; i += 2) {
        x = a + i * dx;
        sum4x += function(x,s);
    }

    // 2/3 terms
    for (int i = 2; i < n-1; i += 2) {
        x = a + i * dx;
        sum2x += function(x,s);
    }

    // Compute the integral approximation.
    simpson = function(a,s) + function(a,b);
    simpson = (dx / 3)*(simpson + 4 * sum4x + 2 * sum2x);
    while ( absError < 0.000001)
    {
        simpson2 = SimpsonsRule(a, b, s, n);
        absError = Math.abs(simpson2 - simpson) / 15;
        simpson = simpson2;
        n++;
    }
    System.out.println("Number of intervals is " + n + ".");
    return simpson2;
}

这行不通,因为我没有写

simpson2 = SimpsonsRule(a, b, s, n);

正确。

我尝试了第二种方法,但解决方案最终也失败了。

public static double function(double x, double s) {
    double sech = 1 / Math.cosh(x); // Hyperbolic cosecant
    double squared = Math.pow(sech, 2);
    return ((Math.pow(x, s)) * squared);
 }

 // Simpson's rule - Approximates the definite integral of f from a to b.
public static double SimpsonsRule(double a, double b, double s, int n) {
    double dx, x, sum4x, sum2x;
    double absError = 1.0;
    double simpson = 0.0;
    double simpson2 = 0.0;

    dx = (b-a) / n;
    sum4x = 0.0;
    sum2x = 0.0;

    // 4/3 terms
    for (int i = 1; i < n; i += 2) {
        x = a + i * dx;
        sum4x += function(x,s);
    }

    // 2/3 terms
    for (int i = 2; i < n-1; i += 2) {
        x = a + i * dx;
        sum2x += function(x,s);
    }

    // Compute the integral approximation.
    simpson = function(a,s) + function(a,b);
    simpson = (dx / 3)*(simpson + 4 * sum4x + 2 * sum2x);
    while ( absError < 0.000001)
    {
        n++;
        dx = (b-a) / n;
         // 4/3 terms
        for (int i = 1; i < n; i += 2) {
            x = a + i * dx;
            sum4x += function(x,s);
        }

        // 2/3 terms
        for (int i = 2; i < n-1; i += 2) {
            x = a + i * dx;
            sum2x += function(x,s);
        }
        simpson = function(a,s) + function(a,b);
        simpson2 = (dx / 3)*(simpson + 4 * sum4x + 2 * sum2x);
        absError = Math.abs(simpson2 - simpson) / 15;
        simpson = simpson2;
    }
    System.out.println("Number of intervals is " + n + ".");
    return simpson2;
}

我需要以不同的方式编写 while 循环。 while 循环中引用错误的方式有什么问题?

java 代码直到

    while ( absError < 0.000001)
{
    simpson2 = SimpsonsRule(a, b, s, n);
    absError = Math.abs(simpson2 - simpson) / 15;
    simpson = simpson2;
    n++;
}
System.out.println("Number of intervals is " + n + ".");
return simpson2;

工作正常并正确计算 Simpson 的方法。

看起来您的 Simpson 方法实现没有收敛。您可以做的最简单的事情是避免 while 中的无限循环 - 您必须添加另一个条件 - 最大迭代次数。

类似的东西:

int n = 0;
while (error < ACCURACY && n++ < MAX_ITERATIONS) {
    // while body
}

其中 ACCURACY 在您的情况下是 0.000001(或 1e-6)并且 MAX_ITERATIONS 是整数常量,例如 1000001e+6.

为什么你的算法不收敛 - 这是另一个问题 - 仔细查看你的公式 - 使用调试工具。祝你好运!

我修好了。感谢您的帮助。

 // Simpson's rule - Approximates the definite integral of f from a to b.
 public static double SimpsonsRule(double a, double b, double s, int n) {
    double simpson, dx, x, sum4x, sum2x;

    dx = (b-a) / n;
    sum4x = 0.0;
    sum2x = 0.0;

    // 4/3 terms
    for (int i = 1; i < n; i += 2) {
        x = a + i * dx;
        sum4x += function(x,s);
    }

    // 2/3 terms
    for (int i = 2; i < n-1; i += 2) {
        x = a + i * dx;
        sum2x += function(x,s);
    }

    // Compute the integral approximation.
    simpson = function(a,s) + function(a,b);
    simpson = (dx / 3)*(simpson + 4 * sum4x + 2 * sum2x);
    return simpson;
}

// Handles the error for for f(x) = t^s * sech(t)^2. The integration is
// done from 0 to 100.
// Stop Simspson's Method when the relative error is less than 1 * 10^-6
public static double SimpsonError(double a, double b, double s, int n)
{
    double futureVal;
    double absError = 1.0;
    double finalValueOfN;
    double numberOfIterations = 0.0;
    double currentVal = SimpsonsRule(a,b,s,n);

    while (absError / currentVal > 0.000001) {
        n = 2*n;
        futureVal = SimpsonsRule(a,b,s,n);
        absError = Math.abs(futureVal - currentVal) / 15;
        currentVal = futureVal;
    }

    // Find the number of iterations. N starts at 8 and doubles every iteration.
    finalValueOfN = n / 8;
    while (finalValueOfN % 2 == 0) {
        finalValueOfN = finalValueOfN / 2;
        numberOfIterations++;
    }
    System.out.println("The number of iterations is " + numberOfIterations + ".");
    return currentVal;
}