Here is my code for the Riemann Integrator:

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;
    }



}

It works for the most part, however, the math is wrong more often than not, and I don't know where I went wrong. For example, if I want to find the sum of the function of x^3+2x^2+3x, I get 2.416667 on my calculator, and on Wolfram Alpha. But, in my program, it tells me that I get 6. I think it may have something to do with the number of rectangles, because it always tells me I get one rectangle. Can anyone help?

有帮助吗?

解决方案

You need another loop, something along the lines of

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);

The idea is that you keep decreasing the rectancles' width until the sumOfArea with width = w is more-or-less the same (i.e. within epsilon) as the sumOfArea with width = 2w.

There are faster + more accurate integration algorithms out there e.g. Newton-Cotes, but I'm assuming you don't have a choice in the matter.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top