我的辛普森规则计算器是否正确(java)?

时间:2017-06-30 22:34:57

标签: java algorithm math calculator precision

我应该计算

enter image description here

使用Simpson的规则,有4个子间隔。

我当然不想手工完成,所以我试图用Java编写该算法。

辛普森规则的公式是

enter image description here

这是我的代码:

import java.util.Scanner;
import java.util.Locale;
public class Simpson {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in).useLocale(Locale.US);
        //e= 2.718281828459045 to copy paste
        System.out.println("Interval a: ");
        double aInt = input.nextDouble();
        System.out.println("Interval b: ");
        double bInt = input.nextDouble();
        System.out.println("How many sub intervals: ");
        double teilInt = input.nextDouble();
        double intervaldistance = (bInt-aInt)/teilInt;
        System.out.println("h = "+"("+bInt+"-"+aInt+") / "+teilInt+ " = "+intervaldistance);
        double total = 0;
        System.out.println("");
        double totalSum=0;

        for(double i=0; i<teilInt; i++) {
            bInt = aInt+intervaldistance;
            printInterval(aInt, bInt);
            total = prod1(aInt, bInt);
            total = total*prod2(aInt, bInt);
            aInt = bInt;
            System.out.println(total);
            totalSum=totalSum+total;
            total=0;
        }
        System.out.println("");
        System.out.println("Result: "+totalSum);
    }
    static double prod1(double a, double b) { // first product of simpson rule; (b-a) / 6
        double res1 = (b-a)/6;
        return res1;
    }
    static double prod2(double a, double b) { // second pproduct of simpson rule
        double res2 = Math.log(a)+4*Math.log((a+b)/2)+Math.log(b);
        return res2;
    }
    static void printInterval(double a, double b) {
        System.out.println("");
        System.out.println("["+a+"; "+b+"]"); 
    }
}

Output for 4 sub intervals:

[1.0; 1.4295704571147612]
0.08130646125926948

[1.4295704571147612; 1.8591409142295223]
0.21241421690076787

[1.8591409142295223; 2.2887113713442835]
0.31257532785558795

[2.2887113713442835; 2.7182818284590446]
0.39368288949073565

Result: 0.9999788955063609

现在,如果我将我的解决方案与其他在线计算器(http://www.emathhelp.net/calculators/calculus-2/simpsons-rule-calculator/?f=ln+%28x%29&a=1&b=e&n=4&steps=on)进行比较,则会有所不同......但我不明白为什么我的错误。

我的解决方案是0.9999788955063609,在线解决方案是0.999707944567103

也许我犯了一个错误?但我已经仔细检查了一切,无法找到。

2 个答案:

答案 0 :(得分:3)

您可能会多次执行b_n = a_ {n} +间隔来累积舍入误差。 相反,你可以使用归纳法,你说a_n = a_0 + n * interval,因为这只涉及引入一次舍入误差。

我将测试实际数字以确认并稍微充实答案,但在此期间,您可以观看有关handmade hero

错误累积的解释

<子> PS。作为奖励,您可以观看手工制作英雄的摘录!

更新:我查看了您的链接。虽然上面描述的问题确实适用,但精度差异很小(您将得到答案0.9999788955063612)。在您的情况下出现差异的原因是,在线计算器中使用的公式在符号方面略有不同,将区间[a,b]视为2h。换句话说,您的4个区间相当于计算中的8个区间。

如果您在该网页中放置8个矩形,您将获得与(更准确)数字相同的结果:

  

答案:0.999978895506362。

查看该网页here

上使用的符号的更好解释

答案 1 :(得分:3)

我将增量计算更改为顶部,以便您不会反复计算增量。您也没有为奇数和偶数因子应用正确的乘数,也没有为deltaX应用正确的公式,因为它必须是:((a-b)/ n)/ 3

double deltaX = ((bInt-aInt)/teilInt)/3;

for(int i=0; i<=teilInt; i++) { //changed to <= to include the last interval
    bInt = aInt+intervaldistance;
    printInterval(aInt, bInt);
    total = prod2(aInt, bInt, i+1, teilInt); //added the current interval and n. The interval is +1 to work well with the even and odds
    totalSum += total;
    aInt = bInt;
    System.out.println(total);
}   

System.out.println("");
System.out.println("Result: "+ (totalSum*deltaX)); //multiplication with deltaX is now here

为了考虑f(x)的正确因子,我将prod2改为:

static double prod2(double a, double b, int interval, double n) {
    int multiplier = 1;
    if (interval > 0 && interval <= n){
          //applying the right multiplier to f(x) given the current interval
          multiplier = (interval % 2 == 0) ? 4 : 2; 
    }

    return multiplier * Math.log(a);        
}

现在它产生了正确的结果:

enter image description here