Results

Visual Integrals for Programmers

Integrals — tiny rectangles accumulating under a curve to find total area

Plain English first

An integral adds up a huge number of tiny pieces.

Picture a hilly field. You want to know the total area. You can't measure it in one go — so you slice it into thousands of thin vertical strips, measure each strip's area (height × width), and add them all up.

The integral is what you get when the strips become infinitely thin.

The picture

f(x)
  |      ***
  |    **   **
  |   *       *
  |  *         *
  | *           *
  |*             *
  |________________ x
     a           b

  Sum of all the thin rectangles underneath the curve
  from x = a to x = b

Standard math notation

∫[a to b] f(x) dx

Read as: "the integral of f(x) from a to b"
dx means "a tiny slice of width dx"

Verbose Python with descriptive names

def compute_area_under_curve(
    function_to_integrate,
    left_boundary,
    right_boundary,
    number_of_rectangles
):
    """
    Estimate the area under a curve between two x values.

    Splits the interval into many thin rectangles.
    Each rectangle has:
      - width  = total_width / number_of_rectangles
      - height = function value at the left edge of that rectangle

    Adding all rectangle areas gives the approximate integral.
    More rectangles → more accurate answer.
    """
    total_width_of_interval = right_boundary - left_boundary

    # Width of each individual rectangle slice
    width_of_each_slice = total_width_of_interval / number_of_rectangles

    accumulated_area = 0

    for rectangle_index in range(number_of_rectangles):
        # x position at the left edge of this rectangle
        x_position = left_boundary + rectangle_index * width_of_each_slice

        # Height of the rectangle equals the function value at this x
        height_of_rectangle = function_to_integrate(x_position)

        # Area of this one rectangle
        area_of_this_rectangle = height_of_rectangle * width_of_each_slice

        accumulated_area += area_of_this_rectangle

    return accumulated_area


# Example: area under f(x) = x² from 0 to 3
# True answer: [x³/3] from 0 to 3 = 27/3 = 9
def square_function(x):
    return x * x

estimated_area = compute_area_under_curve(
    function_to_integrate=square_function,
    left_boundary=0,
    right_boundary=3,
    number_of_rectangles=10_000
)
print(estimated_area)  # very close to 9.0

What integrals measure in the real world

The key insight: integrals measure accumulation over time or space.

Situation Function f(x) Integral gives you
A car accelerating speed at time t total distance traveled
Water filling a tank flow rate at time t total volume added
Revenue over time dollars per day total revenue
Signal strength power at frequency f total energy

Common mistakes

  • Thinking more precision always matters. For most applications, 1000 rectangles is already very accurate.
  • Forgetting that the function can be negative — areas below the x-axis subtract from the total.
  • Confusing "area under the curve" with the shape's visual area — if f(x) dips below zero, the integral can be less than the visual area.

See also

Comments

No comments yet. Be the first!


Comments are moderated and will appear after approval.