Home > Python > Numerical Integration

Ch. 12.1 Trapezoidal Method

Chapters Index
Program 1

Trapezoidal Method

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Integration by Trapezoidal method

Theoretical Background & Computational Mechanics

The trapezoidal rule is a numerical method for approximating the definite integral of a function. It is particularly useful when the function is not easily integrable analytically. The trapezoidal rule approximates the area under a curve by dividing it into trapezoids rather than rectangles.

Definition of the Integral: We want to approximate the definite integral of a function \( f(x) \) over the interval \([a, b]\):

\[ I = \int_a^b f(x) \, dx \]

Divide the Interval: Divide the interval \([a, b]\) into \( n \) equal subintervals of width \( h \):

\[ h = \frac{b - a}{n} \]

The points dividing the intervals are:

\[ x_0 = a, x_1 = a + h, x_2 = a + 2h, \ldots, x_n = b \]

Trapezoidal Approximation: The area under the curve on each subinterval \([x_i, x_{i+1}]\) can be approximated by the area of the trapezoid formed by the line segments joining the points \((x_i, f(x_i))\) and \((x_{i+1}, f(x_{i+1}))\).

The area \( A_i \) of each trapezoid is given by:

\[ A_i = \frac{1}{2} \left( f(x_i) + f(x_{i+1}) \right) h \]

Sum the Areas: The total area \( I \) can be approximated by summing the areas of all trapezoids:

\[ I \approx \sum_{i=0}^{n-1} A_i = \sum_{i=0}^{n-1} \frac{1}{2} \left( f(x_i) + f(x_{i+1}) \right) h \]

Simplify the Summation: Factor out \( \frac{h}{2} \):

\[ I \approx \frac{h}{2} \left( f(x_0) + 2 \sum_{i=1}^{n-1} f(x_i) + f(x_n) \right) \]

This leads to the final form of the trapezoidal rule:

\[ I \approx \frac{b - a}{2n} \left( f(a) + 2 \sum_{i=1}^{n-1} f(a + ih) + f(b) \right) \]

Program 2

Trapezoidal Method

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Integration by Trapezoidal method
Theoretical Principles & Computational Physics Guide

Trapezoidal Method: Numerical Quadrature & Definite Integrals

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Continuous Quadrature: Evaluates definite integrals $\int_a^b f(x)dx$ when analytical anti-derivatives do not exist in closed form.
Discretized Summation: Approximates area under curves using interpolating polynomials (Trapezoidal $\mathcal{O}(h^2)$, Simpson $\mathcal{O}(h^4)$).
Physical Observables: Essential for finding total work, probability normalization $\int |\psi|^2 dx = 1$, and center of mass coordinates.

2. Mathematical Formulation & Algorithmic Pipeline

Composite numerical integration quadrature formulas:

$$ \text{Trapezoidal: } I \approx \frac{h}{2}\left[f_0 + 2\sum_{i=1}^{N-1} f_i + f_N\right], \quad \text{Simpson: } I \approx \frac{h}{3}\left[f_0 + 4\sum_{\text{odd}} f_i + 2\sum_{\text{even}} f_i + f_N\right] $$

3. Numerical Mechanics & Implementation Logic

  • Interval Subdivision: Divide $[a, b]$ into $N$ equal slices of width $h = (b - a)/N$.
  • Weighted Summation: Evaluate integrand at nodal points and apply quadrature weight coefficients.
  • Convergence Verification: Double $N$ and verify that error scales consistent with the method's asymptotic order.

4. Relevant Computational Practice Exercises

Exercise 1
Evaluate $\int_0^{\pi/2} \sin(x)dx$ using Trapezoidal Method and calculate absolute error against the exact value 1.0.
Exercise 2
Perform an error study by doubling strip count $N$ from $10$ to $1000$ and plot error vs $h$ on a log-log scale.
Exercise 3
Apply the integration method to calculate the period of a large-amplitude nonlinear pendulum $T = 4\sqrt{L/g}\int_0^{\pi/2}\frac{d\phi}{\sqrt{1 - k^2\sin^2\phi}}$.
Standard Literature & References:
  • Mark Newman, "Computational Physics", University of Michigan (2013).
  • William H. Press et al., "Numerical Recipes", Cambridge University Press (2007).