编程语言
首页 > 编程语言> > Java – Simpson的方法和错误

Java – Simpson的方法和错误

作者:互联网

我正在为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的方法实现没有收敛.你可以做的最简单的事情是避免无限循环 – 你必须添加另一个条件 – 最大迭代次数.

像这样的东西:

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

其中ACCURACY为0.000001(或1e-6),MAX_ITERATIONS为整数常量,例如100000或1e 6.

为什么你的算法没有收敛 – 这是另一个问题 – 仔细研究你的公式 – 使用调试工具.祝好运!

标签:java,numerical-methods,estimation
来源: https://codeday.me/bug/20190623/1273615.html