Skip to content

Rectangle rule

Java numeric integration

Flowchart (ISO 5807)

StartInput a, b, nh = (b − a) / ns = 0i = 0, n − 1, 1x = a + ihs = s + x²hReturn sEndFigure 1 — Integral.rect

Source code

Java
class Integral {
    static double rect(double a, double b, int n) {
        double h = (b - a) / n;
        double s = 0;
        for (int i = 0; i < n; i++) {
            double x = a + i * h;
            s = s + x * x * h;
        }
        return s;
    }
}

The editor opens with this code — change it and the chart redraws.

Language
Java
Constructs
numeric integration
Topic
Algorithms
Formats
Word, Visio, draw.io, PDF, SVG

Related examples