Back
+20 XP
Concept

Numerical Integration & The Trapezoidal Rule

When symbolic calculus is impossible, numerical integration approximates definite integrals $\int_a^b f(x)\,dx$ by summing trapezoidal slices: $\frac{\Delta x}{2} [f(a) + 2\sum_{i=1}^{n-1} f(x_i) + f(b)]$ 📐.

python
def trapezoid(f, a, b, n=1000):
    dx = (b - a) / n
    total = 0.5 * (f(a) + f(b))
    for i in range(1, n):
        total += f(a + i * dx)
    return total * dx

# Integrate x^2 from 0 to 3 (exact algebraic answer: 3^3 / 3 = 9.0)
area = trapezoid(lambda x: x ** 2, 0, 3, 1000)
print(f"Area under x^2: {area:.4f} 📐")

# 56. Numerical Integration & The Trapezoidal Rule When symbolic calculus is impossible, numerical integration approximates definite integrals $\int_a^b f(x)\,dx$ by summing trapezoidal slices: $\frac{\Delta x}{2} [f(a) + 2\sum_{i=1}^{n-1} f(x_i) + f(b)]$ 📐. ```python def trapezoid(f, a, b, n=1000): dx = (b - a) / n total = 0.5 * (f(a) + f(b)) for i in range(1, n): total += f(a + i * dx) return total * dx # Integrate x^2 from 0 to 3 (exact algebraic answer: 3^3 / 3 = 9.0) area = trapezoid(lambda x: x ** 2, 0, 3, 1000) print(f"Area under x^2: {area:.4f} 📐") ``` Increasing $n$ reduces approximation error at the rate of $O(\Delta x^2)$. For higher precision with fewer evaluations, Simpson's rule fits parabolic arcs across triplets of points 🚀. --- ### Question What geometric polygon does the Trapezoidal Rule use to approximate the area beneath a curve segment? 🧠 - [ ] a. A trapezoid connecting the x-axis baselines to the function values at adjacent boundaries 📐 - [ ] b. A circle whose radius equals the local derivative at the segment midpoint - [ ] c. An equilateral triangle anchored at the Cartesian origin - [ ] d. A regular pentagon fitted to minimize asymptotic residuals --- ### Challenge Implement `trapezoid(f, a, b, n)`. Calculate the integral of $f(x) = x^2$ from $0$ to $3$ with $n = 500$. Print `round(val, 2)`. 🎯 <details> <summary>Solution</summary> ```python def trapezoid(f, a, b, n=500): dx = (b - a) / n total = 0.5 * (f(a) + f(b)) for i in range(1, n): total += f(a + i * dx) return total * dx area = trapezoid(lambda x: x ** 2, 0, 3, 500) print(round(area, 2)) ``` </details>

Checkpoint

What geometric polygon does the Trapezoidal Rule use to approximate the area beneath a curve segment? 🧠

Practice

Implement `trapezoid(f, a, b, n)`. Calculate the integral of $f(x) = x^2$ from $0$ to $3$ with $n = 500$. Print `round(val, 2)`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks