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

A5 · Lesson 47 · Lab: integrating data

Energy, charge and RMS from a list of samples

A power meter doesn't know the formula for the power it's measuring; it has a list of samples. Charge from a current log, energy from a power log and RMS from a voltage record are all integrals of data. In this lab you'll write the trapezoid and Simpson rules for sampled data, handle samples that aren't evenly spaced, let the computer decide how many slices it needs, and measure an RMS from one period of a distorted wave.

  • A clipped sine wave, v=max⁡(−Ac,min⁡(Ac,sin⁡θ)), over one period. Clipping flattens its tops at ±Ac.
  • Lower the clip level. The readout is its RMS, computed by integrating v2 exactly; the sine's own RMS is 0.707.
  • How much does clipping at 0.9 lower the RMS? At 0.5?

Predict

A current is logged at uneven times: 0, 0.1, 0.3 and 0.6 s. Can the trapezoid rule still find the charge?

Name it

Rules for samples

For samples y0,…,yn spaced h apart:

  • trapezoid: h(12y0+y1+⋯+yn−1+12yn);
  • Simpson (n even): h3(y0+4y1+2y2+⋯+4yn−1+yn).

For uneven spacing, add the trapezoids one at a time: ∑yk+yk+12(xk+1−xk).

Letting the computer choose n

When you can evaluate the function anywhere, start with a few slices and double n until two successive trapezoid results Tn and T2n agree to within a tolerance. Their difference estimates the error, and 4T2n−Tn3 (Richardson's correction) is usually far more accurate than either.

RMS from samples

Vrms=1T∫0Tv2dt: square the samples, integrate over a whole number of periods, divide by the time, take the root. For a clipped sine wave of peak 1 clipped at Ac, the exact mean square is 2π(θc2−Ac1−Ac22+Ac2(π2−θc)) with θc=sin−1⁡Ac, a handy check.

Worked example

A current is logged as 0 A at 0 s, 2 A at 0.1 s, 3 A at 0.3 s and 1 A at 0.6 s. How much charge passed?

Show the solution
  1. Widths. The slices are 0.1 s, 0.2 s and 0.3 s wide.
  2. Add the trapezoids with their own widths. q=0+22(0.1)+2+32(0.2)+3+12(0.3)=0.1+0.5+0.6=1.2C

Answer 1.2 C passed.

Worked example

Integrate e−x2 on [0,1] by doubling n until two trapezoid results agree to 10−6.

Show the solution
  1. Double n. T1=0.683940T2=0.731370T4=0.742984T8=0.745866 and so on; the differences shrink by about 4 each time.
  2. Stop when two agree. They first agree to 10−6 at n=512 against 256, with T512=0.746824.
  3. Compare with Richardson's correction. From T8 and T4 alone it already gives 0.746 826: a tiny fraction of the work for the same accuracy.

Answer the results first agree to 10−6 at n=512, giving 0.746 824.

Check

A current reads 0 A, then 2 A after 0.2 s, then 3 A after a further 0.5 s. What charge passed?

Write units the way you normally would: 1.5 C, 1500 mC and 0.0015 kC are the same answer.

Check

A unit sine wave is clipped at ± 0.7. What is its RMS?

Check

Write trap(y, h) and simpson(y, h) for equally spaced samples (Simpson needs an even number of slices, so an odd number of samples). Test them on samples of sin⁡ over [0,π], whose integral is 2.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write trap_xy(x, y) for samples at uneven times. Set Q to the charge in the starter's current log number k = 1.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write auto_trap(f, a, b, tol): start with 1 slice, double the number of slices until two successive trapezoid results differ by less than tol, and return (estimate, n), where the estimate is the Richardson value 4T2n−Tn3 and n the larger slice count. Set r = auto_trap(lambda x: math.exp(-x*x), 0, 1, tol).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Sample one period of a unit sine clipped at ±Ac = 0.7 with 2001 points, and set rms to its RMS using Simpson's rule on v2. Set exact from the mean-square 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

Write romberg(f, a, b, m): build a triangle of estimates. Row i starts with the trapezoid rule on 2i slices; each next entry is Ri,j=Ri,j−1+Ri,j−1−Ri−1,j−14j−1. Return the last entry of row m−1. Set r = romberg(lambda x: math.exp(-x*x), 0, 1, 5).

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 the trapezoid rule the default for logged data, even though Simpson's rule is more accurate for smooth functions?

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.