A69 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 11+x2. The shaded region runs from 0 to L, and the readout compares its area with the full area to infinity, π/2.
  • Double L again and again. The missing area, π2−tan−1⁡L, is about 1/L: halving with each doubling.
  • An integrand that falls like e−x would leave a tail of e−L, far faster.

Predict

An integrator adds pieces [a,a+1],[a+1,a+3],[a+3,a+7],…, each twice as long as the last, and stops when a piece adds less than 10−10. For ∫0∞e−xdx, about how far out does it get?

Name it

Extending the range

To approximate ∫a∞f:

  1. integrate a first piece, say [a,a+1], with Simpson's rule;
  2. keep adding pieces of doubling length;
  3. 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

F(s)=∫0∞e−stf(t)dt. The factor e−st makes the integrand negligible beyond T=ln⁡(1/tol)s, so integrating on [0,T] is enough, as long as f itself doesn't grow fast.

Checking against exact answers

A numerical integrator is trusted after it reproduces known results: ∫0∞dx1+x2=π2, ℒ{eat}=1s−a, ℒ{sin⁡ωt}=ωs2+ω2. For a ringing pulse v=V0e−t/τsin⁡ωt across R, the energy is

∫0∞v2Rdt=V022R(1a−aa2+4ω2),a=2τ

from sin2⁡=12(1−cos⁡2ωt) and the transforms of 1 and cos⁡.

Worked example

How far must you integrate e−stsin⁡3t for its Laplace transform at s=4 to be accurate to 10−12?

Show the solution
  1. Bound the tail. The integrand is at most e−4t, whose tail beyond T is ∫T∞e−4tdt=e−4T4
  2. Solve for T. Set e−4T=10−12: T=12ln⁡104=6.9
  3. Check with Simpson's rule. Simpson's rule on [0,6.9] with 4000 slices gives 0.119 999 999 99, against the exact 325=0.12.

Answer integrating out to T=6.9 is enough.

Worked example

A 10 V ringing pulse, v=10e−t/1mssin⁡(2π⋅1000t), appears across 100 Ω. How much energy does it deliver?

Show the solution
  1. Use the damped-sine integral. With a=2/τ=2000 per second and ω=2π×1000: E=100200(12000−200020002+4ω2)=0.5(0.0005−0.0000123)=0.244mJ
  2. 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 s = 1 stops where e−st falls below 1 × 10⁻⁶. How far does it integrate?

Check

How much of ∫0∞dx1+x2 lies beyond x = 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 e−stf(t) from 0 to T=ln⁡(1/tol)/s 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 v=10e−t/τsin⁡(2πft) volts, with τ = 1 ms and f = 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 ∑1n2 after N terms lies between 1N+1 and 1N. Set S to the sum of the first N = 20 terms, lo and hi to S plus those two bounds, and true to π2/6. 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 x=1/u, ∫1∞dx1+x2=∫01du1+u2. Set I to the right side by Simpson's rule with 200 slices, and exact to π/4.

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 ∫0∞dx1+x2 much harder to compute numerically than ∫0∞e−xdx, even though both are finite?

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.