A6 · Lesson 9 · Lab: improper integrals in code
Integrating to infinity on a computer that can't go there
A computer can't integrate out to infinity, but it can integrate far enough that the rest doesn't matter, and know when it has. That's how a simulator computes the total energy of a decaying pulse, and how a numerical Laplace transform works. In this lab you'll write an integrator that keeps extending its range until the new pieces are negligible, a numerical Laplace transform checked against the table, and an energy calculation for a ringing pulse checked against its exact formula.
- The curve is . The shaded region runs from 0 to , and the readout compares its area with the full area to infinity, .
- Double again and again. The missing area, , is about : halving with each doubling.
- An integrand that falls like would leave a tail of , far faster.
Predict
An integrator adds pieces , each twice as long as the last, and stops when a piece adds less than . For , about how far out does it get?
About 30. A piece starting at adds about (times the piece's length, which grows only by doubling), so pieces fall below once passes about , at the end of the piece that starts at 15 and ends at 31. For the pieces shrink only as , so it would need to reach about : slowly decaying tails are the hard case.
Name it
Extending the range
To approximate :
- integrate a first piece, say , with Simpson's rule;
- keep adding pieces of doubling length;
- stop when a piece contributes less than the tolerance.
Doubling means a slowly decaying tail can still be reached in a reasonable number of steps. The stopping test assumes the pieces keep shrinking, which holds for integrands that decay steadily.
A numerical Laplace transform
. The factor makes the integrand negligible beyond , so integrating on is enough, as long as itself doesn't grow fast.
Checking against exact answers
A numerical integrator is trusted after it reproduces known results: , , . For a ringing pulse across , the energy is
from and the transforms of 1 and .
Worked example
How far must you integrate for its Laplace transform at to be accurate to ?
Show the solution
- Bound the tail. The integrand is at most , whose tail beyond is
- Solve for . Set :
- Check with Simpson's rule. Simpson's rule on with 4000 slices gives 0.119 999 999 99, against the exact .
Answer integrating out to is enough.
Worked example
A 10 V ringing pulse, , appears across 100 Ω. How much energy does it deliver?
Show the solution
- Use the damped-sine integral. With per second and :
- Check numerically. Integrating numerically for 30 time constants agrees to ten digits.
Answer the pulse delivers 0.244 mJ.
Check
A numerical Laplace transform at = 1 stops where falls below 1 × 10⁻⁶. How far does it integrate?
Check
How much of lies beyond = 100?
Check
Write to_inf(f, a, tol=1e-10): integrate f from a to infinity by adding Simpson's-rule pieces (200 slices each) of length 1, 2, 4, ..., stopping when a piece adds less than tol in size. Use the simpson in the starter.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write laplace(f, s, tol=1e-12), the numerical Laplace transform: integrate from 0 to with Simpson's rule and 4000 slices. Check it against the table.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A pulse volts, with = 1 ms and = 500 Hz, appears across 100 Ω. Set E_num to its energy by Simpson's rule over 30 time constants with 20 000 slices, and E_exact from the formula in the reading.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
By the integral test, the tail of after terms lies between and . Set S to the sum of the first N = 20 terms, lo and hi to S plus those two bounds, and true to . Check that true lies between lo and hi.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A slowly decaying tail can be moved to a finite range. With , . Set I to the right side by Simpson's rule with 200 slices, and exact to .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is much harder to compute numerically than , even though both are finite?
Both integrands decay, but at very different rates. The tail of beyond is , so stopping at = 30 already leaves less than . The tail of is about : to leave less than , the integration has to reach . Doubling the pieces makes that reachable in about 35 pieces, but each piece still costs work, and a stopping rule based on the size of the last piece can stop too early if the integrand flattens. In practice, slowly decaying tails are handled by a substitution such as , which turns the infinite range into a finite one, or by adding the tail's known asymptotic form.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.