Java 黎曼积分和问题

Java 黎曼积分和问题,java,integral,calculus,Java,Integral,Calculus,以下是我的Riemann积分器代码: 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

以下是我的Riemann积分器代码:

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



}
公共类黎曼积分器{
公共静态void main(字符串[]args)
{
double lower=double.parseDouble(args[args.length-2]);
double higher=double.parseDouble(args[args.length-1]);
double[]系数=新的double[args.length-3];
如果(args[0]。等于(“多边形”))
{
for(int i=1;i对于(inti=1;i您需要另一个循环,沿着

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的面积与宽度=2w的面积大致相同(即在ε范围内)

有更快更精确的积分算法,例如,但我假设你在这件事上没有选择