Numerical differentiation and integration · Lecture 20
Numerical Integration
Areas, distances and probabilities from tables and from functions with no antiderivative: the trapezoidal rule, Simpson's 1/3 and 3/8 rules, their errors, Romberg integration and Gauss quadrature.
Md Mahbub E Noor · Assistant Professor CSE-3107 Numerical Methods · B.Sc. 3rd Year 1st Semester Department of CSE, University of Barishal
Why this lecture
The problem: distance from speedometer readings
A car accelerates from rest. Its speed is read every 10 seconds for one minute. How far did it travel?
Distance =∫060v(t)dt, the area under the speed curve
There is no formula for v(t): only the readings
The same need appears for ∫01e−x2dx (normal probabilities): a formula exists but no antiderivative
We need rules that turn a few function values into an accurate area, with an error estimate
t (s)
v (m/s)
0
0
10
12
20
22
30
30
40
36
50
40
60
42
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
2 / 22
Today's goals
Derive the trapezoidal, Simpson 1/3 and 3/8 rules from Newton's forward formula
Apply the composite rules to tables and functions, and present the working clearly
State and use their error terms: O(h2) and O(h4)
Improve accuracy with Romberg integration; understand Gauss quadrature
Choose a rule for a given problem, and recognise when rules fail
Last time
Differentiation from tables: sensitive to noise and round-off.
Today
Integration from tables: stable, accurate, and the basis of the ODE methods that follow.
Numerical differentiation and integration · you are here
An integral is the area under a curve.Cut the area into strips. One strip with a straight top is a trapezoid: easy area.Add the strips: that is the trapezoidal rule.Small gaps remain where the curve bends. More strips make them smaller.Simpson's rule uses curved tops (parabolas over two strips): far more accurate for the same work.
In plain words
An integral is an area
Cut it into thin strips whose areas are easy, and add them
Better-shaped strips (curved tops) need far fewer of them
New words today
quadrature
computing an integral numerically
trapezoidal rule
strips with straight tops
Simpson's rule
strips with parabolic tops
composite rule
the same rule repeated on many strips
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
4 / 22
The idea: integrate the interpolating polynomial
Integrate Newton's forward formula over n steps (x = x₀ + ph, dx = h dp)∫x0x0+nhydx=h∫0n(y0+pΔy0+2p(p−1)Δ2y0+⋯)dp
General quadrature formula (Newton–Cotes)=nh[y0+2nΔy0+12n(2n−3)Δ2y0+24n(n−2)2Δ3y0+⋯]
n=1 (straight line): 2h(y0+y1), the trapezoidal rule
Simpson 3/8 (n a multiple of 3): weights 1, 3, 3, 2, 3, 3, 2, …, 1 times 3h/8S3/8=83h[y0+3(y1+y2+y4+y5+⋯)+2(y3+y6+⋯)+yn]
Trapezoidal: halve h, error ÷4; exact for straight lines
Simpson: halve h, error ÷16; exact for cubics
Same number of function values, much better accuracy: Simpson is the default for smooth data
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
6 / 22
The method, step by step
Trapezoidal and Simpson's rules, step by step
Choose n (even for Simpson 1/3) and h=(b−a)/n
Tabulate xi=a+ih and yi=f(xi) for i=0,…,n
Trapezoidal: sum = ends + 2 × inside; multiply by h/2
Simpson: sum = ends + 4 × odd + 2 × even (inside); multiply by h/3
Check: repeat with n doubled; the change estimates the error
Pseudocode
input f, a, b, n # n even
h ← (b − a) / n
T ← (f(a) + f(b)) / 2
S ← f(a) + f(b)
for i = 1 … n−1:
y ← f(a + i·h)
T ← T + y
S ← S + (4 if i odd else 2)·y
return h·T, h·S / 3
One loop, n+1 function values, both rules at once.
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
7 / 22
Explore: panels under the curve
Shaded shapes: what each rule actually integrates (trapezoids or parabolas)
Slide n: watch the trapezoidal error fall by 4 per doubling, Simpson by 16
Simpson 3/8 needs n divisible by 3
The table compares all rules with the same points
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
8 / 22
Problem 1: ∫061+x2dx with h=1
Evaluate ∫061+x2dx by the trapezoidal rule and both Simpson rules with h=1. Compare with tan−16=1.405648.
Errors: trapezoidal 0.005, Simpson 1/3 0.039, Simpson 3/8 0.049. With h=1 the curve bends sharply near 0, so the parabolas fit badly: Simpson is not automatically better when h is too large.
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
9 / 22
Problem 2: the error falls as predicted
Compute ∫01e−x2dx=0.746824133 with n=2,4,8,16. How does the error change when n doubles?
n
Trapezoidal
error
ratio
Simpson
error
ratio
2
0.731370
1.55×10−2
—
0.74718043
3.56×10−4
—
4
0.742984
3.84×10−3
4.0
0.74685538
3.12×10−5
11.4
8
0.745866
9.59×10−4
4.0
0.74682612
1.99×10−6
15.7
16
0.746585
2.40×10−4
4.0
0.74682426
1.25×10−7
15.9
Ratios of 4 and 16 confirm the orders h2 and h4. With 17 function values Simpson already gives 7 correct digits.
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
10 / 22
Romberg integration: Richardson applied to the trapezoidal rule
Halve h repeatedly, then remove the h², h⁴, … error termsRk,0Rk,j=T(h/2k)=Rk,j−1+4j−1Rk,j−1−Rk−1,j−1
Column 1 is exactly Simpson's rule
R3,3 has 7 correct digits from only 9 function values
The same trick as Lecture 19, now in the stable direction
k
Rk,0 (trap)
Rk,1
Rk,2
Rk,3
0
0.683940
1
0.731370
0.747180
2
0.742984
0.746855
0.746834
3
0.745866
0.746826
0.746824
0.7468240
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
11 / 22
Gauss quadrature: choose the points as well as the weights
Two-point Gauss–Legendre on [−1, 1]: exact for every cubic∫−11f(t)dt≈f(−31)+f(31)
Map [a, b] to [−1, 1]∫abf(x)dx=2b−a∫−11f(2a+b+2b−at)dt
Newton–Cotes fixes equally spaced points; Gauss also chooses where to sample
n points are exact for polynomials of degree 2n−1 (Simpson: 3 points, degree 3; Gauss: 2 points, degree 3)
Needs f at irrational points: fine for formulas, impossible for tables
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
12 / 22
Classical vs numerical
Same area, two approaches
Calculus
Classical (antiderivative)
∫01xexdx: integrate by parts
=[xex−ex]01=1
Exact, if an antiderivative can be found
vs
Algorithm
Numerical (Simpson, n=4)
Five values of xex
S=1.000169 (error 1.7×10−4)
Same code for any integrand or table
When the classical approach cannot work
∫01e−x2dx and the speedometer table
e−x2 has no elementary antiderivative (Liouville's theorem): every normal-distribution table and every erf routine is computed numerically. The speed readings have no formula at all. Simpson: 0.7468554 with 5 values; distance 1620 m from 7 readings.
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
13 / 22
Trapezoidal, Simpson and Romberg in Python and C
One loop computes both composite rules
Romberg table built row by row
Applied to e−x2 on [0,1]
Lab: integrate the speedometer table
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
14 / 22
When quadrature goes wrong
Limitations shown failing
Aliasing: sin2(20x) on [0,π] with n=10 or 20 samples only its zeros: every rule says 0 (true π/2)
Singular derivatives: x near 0; Simpson's error falls only like h1.5, not h4
Simpson needs even n; 3/8 needs a multiple of 3
Jumps inside a panel break the polynomial assumption: split the integral at the jump
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
15 / 22
Practice
Practice problem
Problem 3"Evaluate ∫011+xdx using the trapezoidal rule, Simpson's 1/3 rule and Simpson's 3/8 rule with h=1/6. Hence obtain an approximate value of ln2."
Very high-order Newton–Cotes rules (negative weights, Runge-like trouble)
Integrals over many dimensions (use Monte Carlo)
Rule of thumb: tables → Simpson (trapezoidal for unequal or rough data); formulas → Gauss or an adaptive library routine; always compare two step sizes.
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
17 / 22
Comparative analysis: ∫01e−x2dx with about 5 function values
Rule
Function values
Exact for degree
Error order
Value
Error
Trapezoidal, n=4
5
1
h2
0.742984
3.8×10−3
Simpson 1/3, n=4
5
3
h4
0.746855
3.1×10−5
Gauss 2-point, 2 panels
4
3 per panel
h4
0.746803
2.1×10−5
Gauss 3-point, 1 panel
3
5
—
0.746815
9.5×10−6
Romberg R2,2
5
5
h6
0.746834
9.6×10−6
Same small budget of function values, accuracy differing by a factor of about 400. Gauss needs f at irrational points; the others work on tables.
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
18 / 22
Roadmap
End of Module 5: from areas to differential equations
Integration rules are accurate and stable. Their next job is to step differential equations forward in time:
What it does well (keep this)
Trapezoidal O(h2), Simpson O(h4)
Error estimate by halving h
Romberg and Gauss for high accuracy
Stable: errors in data are averaged, not amplified
Where exactly: evaluating classifiers. The ROC curve plots true-positive rate against false-positive rate; the area under it (AUC) is the standard score. sklearn.metrics.auc computes it with the trapezoidal rule on unequally spaced points.
Each segment: width × average height
0.1(0.25)+0.1(0.6)+0.2(0.8)+0.6(0.95)
AUC =0.025+0.06+0.16+0.57=0.815
FPR
0
0.1
0.2
0.4
1.0
TPR
0
0.5
0.7
0.9
1.0
Trapezoids work with any spacing, which is why they are used here rather than Simpson.
⚡ Energy from power readings
Where exactly: smart meters and battery management. Energy is the integral of power; a meter samples power and integrates.
hour
0
1
2
3
4
5
6
power (kW)
2.0
3.5
5.0
4.2
3.0
2.6
1.8
Trapezoidal: 20.2 kWh; Simpson: 20.33 kWh. A battery's state of charge is computed the same way from current readings (coulomb counting).
💊 Drug exposure (pharmacokinetics)
Where exactly: medicine and drug approval. The area under the blood-concentration curve (AUC, mg·h/L) measures total exposure to a drug; regulators require it to compare medicines.
Trapezoidal (the standard "linear trapezoidal method"): 2[21(0+2.4)+8.2+6.1+3.9]=38.8
Simpson: 32[0+4(8.2+3.9)+2(6.1)+2.4]=42.0
The difference (8%) shows the sampling is too coarse near the peak
time (h)
0
2
4
6
8
conc. (mg/L)
0
8.2
6.1
3.9
2.4
Clinical studies sample more densely around the expected peak for exactly this reason.
📊 Normal probabilities
Where exactly: statistics and ML (Gaussian models, p-values). P(0<Z<1)=∫012π1e−z2/2dz has no closed form; tables and scipy.stats.norm.cdf compute it numerically.
Simpson with n=4: 0.341355 (true 0.341345). With n=10 the error is 3×10−7.
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
20 / 22
Checkpoint
Checkpoint · think, then discuss with your neighbourSimpson's 1/3 rule with h=1 and y=1,3,9. What is the value?
A322
B8
C316
D13
A. 31[1+4(3)+9]=322≈7.333. (Trapezoidal gives 21[1+2(3)+9]=8.)
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
21 / 22
Summary
Key points
Newton–Cotes: integrate the interpolating polynomial; n=1,2,3 give trapezoidal, Simpson 1/3, 3/8
Trapezoidal error −12(b−a)h2f′′; Simpson −180(b−a)h4f(4) (exact for cubics)
Romberg: Richardson on the trapezoidal rule; Gauss: optimal points, n points exact to degree 2n−1
Check by halving h; beware singularities, aliasing, jumps and odd n
Opening problem: solved
Trapezoidal: 1610 m; Simpson: 1620 m. They differ by 0.6%, so the distance is about 1.6 km to within about 10 m.
Homework
Evaluate ∫0πsinxdx with n=6 by the trapezoidal and Simpson rules; compare with 2.
Derive Simpson's 1/3 rule from the general quadrature formula with n=2.
Integrate the speedometer table with Simpson's 3/8 rule (two blocks). Compare with 1610 m and 1620 m.
Build the Romberg table for ∫011+x24dx up to R2,2. Which famous number appears?
Next lecture
ODEs I: Taylor, Picard and Euler. When the rate of change depends on the unknown itself: stepping y′=f(x,y) forward.
Md Mahbub E Noor
Lecture 20: Numerical Integration
University of Barishal
22 / 22
Keys →Space next step · ← back A reveal the whole slide number + Enter jump to slide S slide list · F full screen · N notes [] previous / next lecture · L lab In a code box: Ctrl+Enter runs the code