Integration

Your Progress in this Chapter

0%

0 / 1 units completed

Chapter Overview

Trapezoidal Method

Section 7.1 of 56 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], we partition the interval into n sub-intervals, each of width h = \cfrac{b-a}{n}. The composite trapezoidal rule is defined as:

abf(x),dxh2[f(x0)+2i=1n1f(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.9953967383788942

Error Analysis

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

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

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

MethodAccuracy OrderWeighting Logic
Left RiemannO(h)Uses f(x_{ i-1})
TrapezoidalO(h^2)Average of endpoints
Simpson'sO(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.000000000000283