Trapezoidal Method

Integration

Your Progress in this Chapter

0%

3 sections

Trapezoidal Method

Section 7.1 of 76 min2 code examples

Trapezoidal Integration in SepalSolver

The Trapezoidal Rule is a numerical method used to approximate the definiintegral of a function. It works by approximating the region under the graph of the function :math: f(x) as a trapezoid and calculating its area.

Mathematical Definition

To compute the integral over the interval [a,b][a, b], we partition the interval into nn sub-intervals, each of width h=b−anh = \cfrac{b-a}{n}. The composite trapezoidal rule is defined as:

∫abf(x),dx≈h2[f(x0)+2∑i=1n−1f(xi)+f(xn)]\int_{a}^{b}f(x) , dx \approx \cfrac{h}{2} \left[f(x_0) + 2 \sum_{i=1}^{n-1} f(x_i) + f(x_n) \right]

SepalSolver Implementation: Manual Approach Writing the algorithm manually allows for a deeper understanding of how the weighting of the endpoints :math:x_0 and :math:x_n differs from the interior points.

Example 1C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Code is ready to run
OutputFrom the book
The approximate integral is: 
1.9993

Error Analysis

The error in the trapezoidal rule, often denoted as EtE_t, is proportional to the square of the step size hh. Specifically, for a function that is twice continuously differentiable, the error is:

Et=−(b−a)12h2f′′(ξ)E_t = -\frac{(b-a)}{12}h^2 f''(\xi)

where ξ\xi is some number in the interval [a,b][a, b]. This indicates that the method is :math: O(h^2), meaning that halving the step size hh will approximately reduce the error by a factor of four.

MethodAccuracy OrderWeighting Logic
Left RiemannO(h)O(h)Uses f(xi−1)f(x_{ i-1})
TrapezoidalO(h2)O(h^2)Average of endpoints
Simpson'sO(h4)O(h^4)Parabolic fit (1-4-1)

Example 2C#

1
2
3
4
5
6
Code is ready to run
OutputFrom the book
The integral of sin(x) from 0 to pi is approximately 2.0000
Takes a free account.