【问题标题】:Riemann Integrator Sum Issue黎曼积分求和问题
【发布时间】:2013-04-18 02:15:37
【问题描述】:

这是我的黎曼积分器代码:

public class RiemannIntegrator {

    public static void main (String [] args)
    {
        double lower = Double.parseDouble(args[args.length -2]);
        double higher = Double.parseDouble(args[args.length -1]);

        double[] coefficients = new double[args.length - 3];

        if (args[0].equals("poly"))
        {
            for (int i = 1; i < args.length - 2; i++)
            {
                coefficients[i-1] = Double.parseDouble(args[i]);
            }

            System.out.println(integral("poly", coefficients, lower, higher));
        }
    }

    private static double integral(String s, double[] function, double lowBound, double highBound)
    {

        double area = 0; // Area of the rectangle
        double sumOfArea = 0; // Sum of the area of the rectangles
        double width = highBound - lowBound; 

            if (s.equals("poly"))
            {
                for (int i = 1; i <= ((highBound - lowBound) / width); i++) // Represents # of rectangles
                {
                    System.out.println("Rectangles:" + i);
                    for (int j = 0; j < function.length; j++) // Goes through all the coefficients
                    {   
                        area = width * function[j] * Math.pow ( (double)( (i * width + lowBound + (i -1.0) * width + highBound) / 2.0 ),function.length- 1- j);  
                        /*Above code computes area of each rectangle */

                        sumOfArea += area;
                    }
                }
            }
            width = width / 2.0;
            System.out.println("polynomial, function (of any length), lower boundary, higher boundary.");
            function.toString();
            System.out.println("Lower Bound:" + lowBound + ", Higher Bound: " + highBound + ".");
            System.out.println("The integral is:");
            return sumOfArea;
    }



}

它在大多数情况下都有效,但是,数学经常出错,我不知道我哪里出错了。例如,如果我想求 x^3+2x^2+3x 的函数之和,我在计算器上得到 2.416667,在 Wolfram Alpha 上得到。但是,在我的程序中,它告诉我得到 6。我认为这可能与矩形的数量有关,因为它总是告诉我得到一个矩形。有人可以帮忙吗?

【问题讨论】:

    标签: java integral calculus


    【解决方案1】:

    你需要另一个循环,类似于

    double width = highBound - lowBound;
    double epsilon = .001; // This determines how accurate you want the solution to be, closer to zero = more accurate
    double prevValue = -1.0;
    double curValue = -1.0; // initialize curValue to a negative value with greater magnitude than epsilon - this ensures that the while loop evaluates to true on the first pass
    do {
        ... // this is where your for loop goes
        prevValue = curValue;
        curValue = sumOfArea;
        width /= 2.0;
    } while(Math.abs(prevValue - curValue) > epsilon);
    

    这个想法是你不断减小矩形的宽度,直到宽度 = w 的 sumOfArea 与宽度 = 2w 的 sumOfArea 或多或少相同(即在 epsilon 内)。

    有更快+更准确的集成算法,例如Newton-Cotes,但我假设你在这件事上别无选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-24
      • 2020-08-27
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多