PHY 411-506 Home Home  |  Course Outline  |  Lectures  |  Homework  |  Files

Lecture 6: January 29


Quadrature and Integration

Integration is the process by which one finds a solution to a differential equation. When the solution can be expressed a the definite integral of an explicitly known function, integration is said to reduce to Quadrature (which stems from the simple process of estimating the area under a curve by counting little squares). Most interesting examples of differential equations cannot be reduced to quadrature: in general, if you try to represent the solution in integral form, the integrand will depend not only on the integration variable, but also on the solution you are trying to find!

The extended trapezoidal approximation and Romberg integration are very useful to compute most one-dimensional integrals with well-behaved integrands. These algorithms are discussed in more detail in Chapter 4 of "Numerical Recipes":

Programming the Rectangular Approximation

The following program Integ.java is a translation of PROGRAM integ on page 346:
// Integ.java

import comphys.*;

public class Integ {

    // compute integral of f(x) form x = a to x = b
    // using rectangular approximation

    double f (double x) {
        return Math.cos(x);
    }

    double a;                   // lower limit
    double b;                   // upper limit
    int n;                      // number of intervals
    double dx;
    double delta;
    double sum;

    void initial () {
        a = Console.readDouble("lower limit a = ");
        b = Console.readDouble("upper limit b = ");
        n = 2;                  // initial number of intervals
        dx = (b - a) / n;
        delta = dx;             // delta = dx only for n = 2
        sum = f(a);             // -> (f(a) + f(b))/2 for trapezoid rule
        System.out.println();
        System.out.println("  n\trectangular approximation");
        System.out.println();
    }

    void sumf () {
        double x = a + dx;
        while (x < b) {
            sum += f(x);
            x += delta;
        }
    }

    void doubleIntervals () {   // can't use double which is a keyword!
        // double the number of intervals
        delta = dx;
        n *= 2;
        dx /= 2;
    }

    void output () {
        System.out.println(n + "\t" + Easy.format(sum * dx, 8));
    }
    
    public static void main (String[] args) {
        int doublings = 4;
        if (args.length > 0)
            doublings = Integer.parseInt(args[0]);
        Integ integ = new Integ();
        integ.initial();
        do {
            for (int i = 0; i < doublings; i++) {
                integ.sumf();
                integ.output();
                integ.doubleIntervals();
            }
        } while (Console.getYes("continue " + doublings + " more doublings"));
    }
}
Note that the True Basic program allows the calculation to continue for ever until the user hits a key. This can be done in Java using threads, but here we use a simpler solution: the Console.getYes() method asks whether you want to contiue the calculation; if you enter yes or hit the return key the do - while loop continues, but if you enter anything else the loop exits. The program also allows you to enter the number of automatic interval doublings on the command line.

If you are worried that Java might be too slow for your numerical analysis applications, try a Fortran version of PROGRAM integ translated as Integ.f on your computer system!

Multidimensional Quadrature

The trapezoid formula, improved by Romberg integration is a very efficient algorithm to perform one-dimensional quadratures of functions that are "sufficiently regular".

An algorithm, such as the trapezoid rule, which uses a regular lattice of points cannot easily be extended to multidimensional integrals for two reasons:


UB Physics Home Questions or comments: phygons@acsu.buffalo.edu