0

I would like to know if it is possible to make this algorithm iterative instead of recursive, and if its posible, could someone help me?.

public static double adaptiveQuadrature(double a, double b) {
    double h = b - a;
    double c = (a + b) / 2.0;
    double d = (a + c) / 2.0;
    double e = (b + c) / 2.0;
    double Q1 = h / 6 * (f(a) + 4 * f(c) + f(b));
    double Q2 = h / 12 * (f(a) + 4 * f(d) + 2 * f(c) + 4 * f(e) + f(b));
    if (Math.abs(Q2 - Q1) <= EPSILON)
         return Q2 + (Q2 - Q1) / 15;
    else 
         return adaptiveQuadrature(a, c) + adaptiveQuadrature(c, b);
}

static double f(double x) {
    return Math.exp( - x * x / 2) / Math.sqrt(2 * Math.PI);
}

Thank you so much for your help!

1 Answers1

0

I don't think so. The size of the steps is depending on function evaluations made at the endpoints of the initial interval, then at the endpoints of subintervals. The pattern is not progressive and you can't organize a single loop that will "guess" the step ahead of time.

Of course you can derecursivate by means of an explicit stack, but that won't essentially change the nature of the process.