← Math
Math

Position, velocity, and integration

Why does the app say the ride averaged 2.6 km/h?

Estimate distance from timestamped speeds while keeping missing intervals explicit.

Outcome

Accumulate speed over real elapsed time, choose between the left and trapezoid rules, and separate moving time from elapsed time and dropouts.

Terminology (5)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Integration
Adding up a rate over time to get a total: speed over time gives distance.
Euler method Also left rule
Holding each sampled value until the next sample, then multiplying by the elapsed time.
Trapezoid rule
Using the average of the values at both ends of each interval.
Timestep
The time between samples or updates; larger steps make the approximation coarser.
Moving time
Here, the total duration of retained intervals whose average speed reaches the configured minimum; separate from elapsed time.

Why this matters

Distance from speed, battery charge from current, data used from bandwidth and position in an animation are all a rate added up over time. The arithmetic is short, and the mistakes come from the clock: assuming updates arrive on schedule, using one end of each interval, and integrating across time with no data.

This lesson assumes multiplication, averages and units of speed.

Accumulating a rate over time

Integration adds a rate over elapsed time: speed in metres per second multiplied by seconds contributes metres. With sampled speeds, each interval needs an assumption about the speed between its endpoints. Holding the first value gives a rectangle; averaging both endpoints gives a trapezoid.

The samples do not establish what happened during a long gap. Combining an integration rule with a dropout policy and a moving-time definition determines which distance and time enter the displayed average. Those choices remain part of the estimate even when the arithmetic is correct.

01Distance is speed times elapsed time

Distance is speed times elapsed time

Your cycling app records rides from a Bluetooth wheel-speed sensor. Riders report distances that are too short after the sensor drops out, a slight shortfall on every climb out of a junction, and an average speed that counts the minutes spent waiting at red lights.

The sensor is meant to report about once a second. One ride’s first readings were 4.0, 4.5 and 5.0 m/s at 0, 1 and 2 s, then nothing until 5.5 m/s at 5 s and 6.0 at 6 and 7 s. Adding the readings, as if each were one second, gives 25.0 m. Multiplying each speed by the time until the next reading gives 35.0 m: the estimate holds 5.0 m/s for three seconds. The final reading has no following interval, so it contributes no area.

Animations have the same bug when they move a fixed number of pixels per frame: on a 120 Hz display they run twice as fast as on a 60 Hz one.

Sensor events carry their own timestamps; in the browser, requestAnimationFrame passes a high-resolution time in milliseconds, so divide differences by 1000.

02Use both ends of each interval

Use both ends of each interval

Holding each speed until the next reading is the left rule, or Euler’s method. While the rider speeds up, the speed at the start of an interval is the lowest in it, so the left rule falls short. The trapezoid rule uses the average of both ends: 36.5 m for the same readings instead of 35.0. For the three-second interval alone, left gives 5 × 3 = 15 m and trapezoid gives (5 + 5.5)/2 × 3 = 15.75 m. Units are (m/s) × s = m. These are interpolation assumptions about what happened between samples.

On a long descent where the rider brakes steadily, how does the left rule compare with the trapezoid?

03Smaller steps, smaller error

Smaller steps, smaller error

Predict both lines.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// A rider speeding up evenly, 2 m/s every second, for 4 seconds: the true distance is 16 m.
const speed = (t: number) => 2 * t;

for (const dt of [1, 0.25]) {
  let left = 0;
  let trap = 0;

  for (let t = 0; t < 4; t += dt) {
    left += speed(t) * dt;
    trap += ((speed(t) + speed(t + dt)) / 2) * dt;
  }

  console.log(`dt=${dt} left ${left.toFixed(2)} trapezoid ${trap.toFixed(2)}`);
}

With readings every second the left rule is 4 m short of the true 16 m; every quarter second it is 1 m short. Its error shrinks in proportion to the step. The trapezoid is exact here because speed grows in a straight line, and close for smooth changes.

04Dropouts and moving time

Dropouts and moving time

A ride with a stop: skip gaps over 10 s; moving when interval-average speed is at least 1 m/s
MeasureTrapezoid over everythingWith dropouts and moving time
Distance34.70 m22.70 m
Time48.00 s elapsed7.00 s moving
Average speed2.60 km/h11.67 km/h

The full (time seconds, speed m/s) trace is (0,0), (1,2.4), (2,4), (3,5), (4,5), (5,0.5), (6,0), (46,0.6), (47,3), (48,5). This sensor goes quiet while stopped. Between 0 m/s at 6 s and 0.6 m/s at 46 s the trapezoid invents 12 m. Skipping intervals longer than a limit adds neither distance nor time for them, and an interval whose average speed is below the threshold adds distance but not moving time. This app defines its displayed rate as all retained distance divided by moving time, then × 3.6 to convert m/s to km/h. That is a product statistic: it can include subthreshold distance and is not the mean speed restricted to moving intervals. Threshold crossing inside an interval is not located by this model.

05What the samples cannot tell you

What the samples cannot tell you

06Fix the ride summary

Fix the ride summary

Repair the ride recorder’s interval estimates, then its dropout and moving-time policy. A longer silent interval must trigger a different decision from a short interval with evidence at both ends.

A cycling app’s ride summary

Integrate speed over time, use the trapezoid rule, then report moving time.

  1. easy · M-INTEGRATE-V4-1 Distance from timestamps
  2. medium · M-INTEGRATE-V4-2 Average both ends
  3. hard · M-INTEGRATE-V4-3 Moving time and dropouts
In the frontend The ride summary card in Next.js and Svelte Optional

The summary card shows moving time beside elapsed time and says when dropouts were left out, so a short distance does not look like a bug.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// The ride summary card: show moving time and the average over it, and say when the sensor dropped out.
type Summary = { distance: number; moving: number; elapsed: number; dropouts: number };

export function RideSummary({ s }: { s: Summary }) {
  const kmh = s.moving > 0 ? ((s.distance / s.moving) * 3.6).toFixed(1) : "n/a";

  return (
    <dl>
      <dt>Distance</dt>
      <dd>{(s.distance / 1000).toFixed(2)} km</dd>
      <dt>Moving time</dt>
      <dd>
        {Math.round(s.moving / 60)} min of {Math.round(s.elapsed / 60)}
      </dd>
      <dt>Average speed</dt>
      <dd>{kmh} km/h</dd>
      {s.dropouts > 0 && (
        <dd>Sensor dropped out {s.dropouts} times; those gaps are not counted.</dd>
      )}
    </dl>
  );
}

Check this idea later
An animation moves 2 px per frame. How far does it move in one second at 120 frames per second?
Readings 3 m/s at 10 s and 5 m/s at 12 s. What does the trapezoid add for that interval?
Sources

Values above were run in Node 22, Go 1.27, Rust 1.99 and Apple clang 17. The app, the thresholds and the readings are a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.