Java - 辛普森的方法和错误

标签 java numerical-methods estimation

我正在为辛普森的方法编写 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
}

在您的情况下,ACCURACY0.000001(或 1e-6),MAX_ITERATIONS 是一个整数常量,例如 1000001e+6

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

关于Java - 辛普森的方法和错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31013143/

相关文章:

r - MATLAB 和 R 之间的执行时间差异很大

estimation - 如果您没有经验,如何评估编程任务

python - 并行求解微分方程,python

project-management - 如何估计使用新技术的时间?

java - Android 中如何一键打开相机?

java - 从文件中读取但我在android中没有看到?

java - GZIPOutputStream 与 BufferedOutputStream 的性能

java - Android Studio Facebook : onActivityResult in normal class

python - 查找从 (x,y) 坐标移动的距离