An Intuitive Introduction to Kalman Filtering · Module 3 of 11

Adding a motion model:
predict, then correct

Instead of assuming the sled stays where it was, predict where it is going, then let the reading correct the prediction. Along the way the filter learns a velocity it never measures.

Arrow keys or the buttons below to move. Press N for instructor notes.

Open MATLAB now, with m03_lab.m and m03_student.m. Green-edged slides send you there for a few minutes at a time.

How this module is laid out

Six parts, one idea

    The one new idea: predict where the sled will be, then let the reading correct the prediction. To predict, the filter keeps a velocity estimate it never measures.

    Open MATLAB now, with m03_lab.m and m03_student.m. Every few slides a green-edged slide sends you there for three to eight minutes: seven short labs to run and change, and four TODOs where you write the filter yourself.

    Step tables reveal one row per press of the right arrow. Click a part to jump to it.

    Where Module 2 left us

    The low-pass filter assumes the sled has not moved

    Module 2’s update was

    x^k=x^k−1+α(zk−x^k−1)

    Its “prediction” is the old estimate. At 16 m/s that is wrong by 3.2 m every step, so a smooth gain falls far behind and a gain that keeps up barely smooths.

    Best it could do: m, against about 2 m for the raw sensor.
    Module 2: a smooth gain lags the moving sled; the best gain keeps up but is noisy
    From Module 2: smooth and late (left), or on time and noisy (right).
    The plan

    Predict where the sled will be

    If we knew the sled’s velocity v, one reading later it should be at

    x^k−=x^k−1+vΔt

    The minus superscript marks a prediction: our best guess before the new reading arrives.

    The catch

    We have no velocity sensor. The only thing we measure is position, five times a second, with 2 m of noise.

    Which way to go

    Velocity is the rate of change of position, so getting it from position means differentiating. That turns out to be the hard direction, as the next two slides show.

    Module 3 · Part A of F

    Velocity from the readings

    To predict, we need the sled’s velocity. We only measure position. The obvious fix is to difference the readings; let us work it by hand and see why it fails.

    • Difference real readings into velocities and accelerations, row by row.
    • Measure how the noise grows, and why faster sampling makes it worse.
    • Write the noise formula yourself (TODO 1).
    First try

    Difference the readings

    Velocity ≈ change in position / change in time:

    vdiff=zk−zk−1Δt

    Each reading carries ±2 m of noise. Dividing a difference of two noisy readings by a small Δt blows that noise up.

    Drag the sample rate. Intuition says more readings should help. Here they make it worse: the faster you sample, the smaller Δt, and the noisier each difference.

    Part A · By hand

    Differencing six readings, 9.0 to 10.0 s

    The sled is pushing at 2 m/s², so its true velocity climbs from 8 to 10 m/s. Predict first: what should the second difference (an acceleration) come out as?

    timereading(zk − zk−1) / Δtvdifftrue v(vk − vk−1) / Δtadiff

    Press the right arrow to reveal one row at a time.

    The differenced velocities jump between −15 and +25 m/s around a truth of 9 m/s. The accelerations, meant to be 2 m/s², swing by hundreds.

    Part A · Hands-on in MATLAB

    Lab A: difference the readings yourself

    m03_lab.msection “Lab A”about 4 minutes

    Do this

    1. In m03_lab.m, run the Setup section once, then the section Lab A. Click inside the section and press Ctrl+Enter (Run Section).
    2. Compare the table with the slide. The last two lines give the velocity noise over the whole run and the formula √2 σ / Δt.
    3. Set sigma = 0.5 and run again. Then put it back to 2 and set every = 5 (one reading per second).

    Then answer

    How noisy is the differenced velocity in each case?

    About m/s with σ = 2 m at 5 Hz (formula ), m/s with σ = 0.5 m (formula ), and m/s using one reading per second (formula ). One run is a small sample, so the measured numbers sit a little below the formula.

    Fewer readings gave less noise. Is that a good trade?

    Only partly. Each difference now spans a whole second, so it describes the average velocity over that second and lags behind a sled that is accelerating. Differencing has to choose between noise and lag, like Module 2’s filter.

    Click a question to check your answer.

    Why differencing fails

    Differentiating amplifies noise

    Two independent readings, each with variance σ². Their difference has variance 2σ² (variances add, from Module 1). Dividing by Δt divides the variance by Δt²:

    σv=2σΔt=2×20.2≈ m/s

    For acceleration, the second difference zk − 2zk−1 + zk−2 has variance (1 + 4 + 1)σ², so σa = √6 σ/Δt² ≈ m/s².

    The sled never goes faster than 22 m/s or accelerates harder than 2.5 m/s². The noise swamps both.

    Differenced velocity and second-difference acceleration compared with the truth: both buried in noise
    Simulation: measured velocity noise m/s.
    Part A · Your turn to write it

    TODO 1: how much noise does differencing create?

    m03_student.mblock “TODO 1”about 6 minutes

    Write it

    1. Open m03_student.m and press Run (F5) once as it is. It runs with placeholders; wait for the plot window.
    2. Find the block TODO 1. Set sigma_vdiff_theory and sigma_adiff_theory to the one-sigma noise of the first and second differences, using sigma_pos and dt.
    3. Press Run again.

    How you know it works

    • The Command Window prints TODO 1 PASS. TODOs 2 to 4 still say FAIL; they come later.
    • The report line “Differenced velocity: theory … measured …” shows your formula next to the measured spread of 100 noise draws.
    • “Step table 1” in the Command Window is the table you just worked by hand.
    Part A · Recap

    What we just saw

    What we just saw

    • Differencing turns ±2 m of position noise into ±14 m/s of velocity noise and ±120 m/s² of acceleration noise.
    • The noise is √2σ/Δt: sampling faster makes each difference worse.
    • A difference of two readings uses two readings and ignores the rest. We need something that uses all of them.

    Going further in MATLAB (optional)

    1. Read Step table 1 in the Command Window, then open Kinematics › Acceleration. Why does the plot need a y-axis of ±400?
    Part A · Check your understanding

    Before moving on

    1. Two readings 0.2 s apart say 10.0 m and 10.6 m. What velocity does differencing give, and how uncertain is it if each reading has σ = 2 m?

    (10.6 − 10.0)/0.2 = 3 m/s, give or take √2 × 2 / 0.2 ≈ 14 m/s. The answer is almost meaningless.

    2. You double the sensor’s sample rate. What happens to the noise of a differenced velocity?

    It doubles: σv = √2σ/Δt, and Δt halves. Faster sampling makes differencing worse, though it gives a filter more readings to work with.

    3. While parked you average all 24 differenced velocities. Why does that help far less than averaging 25 readings did in Module 1?

    The sum telescopes: (z2 − z1) + (z3 − z2) + … = z25 − z1. The average uses only the first and last readings, so its noise is √2 × 2 / (24 × 0.2) ≈ 0.6 m/s instead of shrinking with every reading.

    Module 3 · Part B of F

    Predict, then correct

    Instead of computing velocity from two readings, keep a velocity estimate and let every reading nudge it. Here is one cycle of that loop, worked with real numbers, then ten more.

    • The predict, compare, correct loop, and its two gains.
    • One cycle in full, then five while parked and five as the sled pushes off.
    • Write the predict step and the position correction (TODOs 2 and 3).
    The new idea

    Estimate velocity inside the filter

    Instead of computing velocity from two readings, keep a velocity estimate alongside the position estimate, and let every reading nudge both.

    1. Predict: move the estimate forward using the estimated velocity.
    2. Compare: the residual is how far the reading lands from the prediction.
    3. Correct: use the residual to fix position and velocity, each with its own gain.

    This is the alpha-beta filter (also called the g-h filter). Its predict-then-correct loop is the skeleton of every Kalman filter.

    Predict x̂⁻ = x̂ + v̂ Δt Compare r = z − x̂⁻ Correct x̂ += αr, v̂ += (β/Δt) r reading z once per reading, every 0.2 s
    Step 1 in math

    Predict

    Assume the sled keeps its current velocity for one step. This is the constant-velocity model:

    x^k−=x^k−1+v^k−1Δtv^k−=v^k−1

    Then measure the surprise, the residual:

    rk=zk−x^k−

    The model is a deliberate simplification. The sled does accelerate; we will see exactly what that costs.

    Step 2 in math

    Correct, with two gains

    x^k=x^k−+αrkv^k=v^k−+βΔtrk

    Both lines are the course’s update, prediction + gain × (measurement − prediction), used twice from one residual.

    • α answers the old question: how far toward the reading should the position move?
    • β is new. r/Δt turns a position surprise into a speed surprise; β is how much of it to believe.
    If the reading keeps landing ahead of the prediction, we must be going too slowly, so speed up. If it keeps landing behind, slow down.
    One step, by hand

    Drag the gains

    Black dashed: the prediction along the old velocity. Red: the reading. Green: the corrected position, and the new velocity it will predict with next time (green dashed).

    Part B · By hand

    One cycle in full, at 6.0 s

    Reading 31 of the course run, with the hand gains α = 0.5 and β = 0.1 (so β/Δt = 0.5). The filter has been running since the first reading; the sled pushed off at 5.0 s.

    steparithmeticresult

    Press the right arrow to reveal one step at a time.

    Part B · By hand

    Five cycles while parked

    Starting from the first reading with velocity 0. Each row is one cycle: predict, residual, correct both. The true velocity is 0 throughout.

    kreadingprediction x̂ + v̂Δtresidualposition x̂⁻ + 0.5 rvelocity v̂ + 0.5 rtrue v

    Press the right arrow to reveal one row at a time.

    Part B · By hand

    Five cycles as the sled pushes off

    The same filter carries on from the cycle you just did. The sled is accelerating at 2 m/s², so its true velocity climbs 0.4 m/s per reading.

    kreadingprediction x̂ + v̂Δtresidualposition x̂⁻ + 0.5 rvelocity v̂ + 0.5 rtrue v

    Press the right arrow to reveal one row at a time.

    Part B · Hands-on in MATLAB

    Lab B: one cycle in full, then five

    m03_lab.msection “Lab B”about 5 minutes

    Do this

    1. Run the section Lab B. It runs the filter over every reading, prints cycle 31 in full and then the five cycles from there.
    2. Check the printout against the two slides you just worked.
    3. Set k_first = 2 (the parked sled) and run again.

    Then answer

    While parked, which way does the velocity estimate move after each residual?

    The same way as the residual: a positive residual raises it by half the residual in m/s, a negative one lowers it. After reading 6 it is m/s.

    Why does the parked velocity never settle at exactly 0?

    Every reading carries fresh noise, and every residual moves the velocity. A fixed β never stops listening, the way a fixed α never stopped in Module 2.

    Click a question to check your answer.

    Watch it work

    Predict, then correct, as the sled pushes off

    Animation: each step predicts along the estimated velocity, then corrects toward the reading, while the velocity estimate climbs

    Top: prediction (square) along the estimated velocity, then correction toward the reading (circle). Bottom: the velocity estimate climbing as the residuals keep landing ahead.

    Part B · Your turn to write it

    TODO 2: the predict step

    m03_student.mblock “TODO 2”about 5 minutes

    Write it

    1. In m03_student.m, find the block TODO 2.
    2. Replace the placeholder so that predictPos(x, v, dt) returns where the sled should be one reading later if it keeps moving at velocity v. One line.
    3. Press Run (F5).

    How you know it works

    • The Command Window prints TODO 2 PASS.
    • Nothing else changes yet: the velocity is still never corrected, so it stays 0 and the prediction is the old position. That changes with TODO 4.
    Part B · Your turn to write it

    TODO 3: correct the position

    m03_student.mblock “TODO 3”about 5 minutes

    Write it

    1. Find the block TODO 3.
    2. Replace the placeholder so that correctPos(x_pred, r, alpha) moves the prediction a fraction alpha of the residual r. One line.
    3. Press Run.

    How you know it works

    • The Command Window prints TODO 3 PASS.
    • With the velocity stuck at 0, your filter is now exactly Module 2’s low-pass filter: open Errors › Position error and compare the green line with Module 2’s.
    • In Step table 3 the velocity column is still all zeros. TODO 4 fixes that in Part C.
    Part B · Recap

    What we just saw

    What we just saw

    • Each reading runs one cycle: predict along the estimated velocity, compare, then correct position by αr and velocity by (β/Δt)r.
    • While parked, the residuals are pure noise and the velocity estimate jitters around zero.
    • As the sled pushes off, the readings start landing ahead of the predictions and the velocity estimate rises.

    Going further in MATLAB (optional)

    1. Open Step by step › Parked and press Next step twelve times. Each press shows the prediction, the reading and the correction.
    2. Compare Step table 3 with your hand tables, then open Step by step › The push.
    Part B · Check your understanding

    Before moving on

    1. Estimate 50 m at 10 m/s, Δt = 0.2 s, reading 51 m, α = 0.5, β = 0.1. What are the new position and velocity?

    Prediction 52 m. Residual 51 − 52 = −1 m. Position 52 + 0.5 × (−1) = 51.5 m. Velocity 10 + (0.1 / 0.2) × (−1) = 9.5 m/s. The reading fell short, so the filter slows down.

    2. With α = 1 and β = 0, what does the filter do?

    The position jumps onto every reading and the velocity never changes: it reproduces the raw sensor, whatever the prediction.

    3. The residual is +3 m. What does the filter conclude, before any gain is applied?

    That the sled is 3 m further on than predicted, which, spread over 0.2 s, suggests it is 15 m/s faster than estimated. The gains decide how much of that to believe.

    Module 3 · Part C of F

    The velocity nobody measured

    No sensor reports velocity, yet the filter tracks it. Remove the noise and you can watch, row by row, how the residuals build the velocity estimate.

    • Perfect readings at the start of the push: every residual positive, the velocity ratcheting up.
    • With β = 0 the filter is exactly Module 2 again.
    • Write the velocity correction (TODO 4).
    Part C · Predict first

    Where can a velocity come from?

    Imagine perfect, noise-free readings. The filter has been parked with velocity 0 when the sled starts accelerating at 5.0 s. Nothing measures velocity. What happens to the velocity estimate?

    1. It stays at 0: there is no velocity measurement to change it.
    2. It jumps to the right value at the first reading after 5.0 s.
    3. It climbs a little with every reading, because every reading lands ahead of the prediction.
    4. It climbs, then overshoots wildly.

    Commit to an answer before you go on. The next slides step through what really happens.

    Part C · By hand

    Perfect readings: the residuals build the velocity

    No noise at all: the readings are the true positions. Each residual is the model’s own mistake, and each one adds 0.5 × r to the velocity.

    timereadingpredictionresidualvelocity v̂ + 0.5 rtrue v

    Press the right arrow to reveal one row at a time.

    The surprise

    A velocity we never measured

    Not one velocity reading was taken, yet the filter tracks the sled’s speed through the whole run.

    • Differencing the readings: m/s RMS error.
    • Alpha-beta velocity: m/s, about × better.

    The difference: differencing uses two readings and throws the rest away; the filter’s velocity is shaped by every residual so far, with recent ones counting most.

    Velocity over the whole run: differenced readings scattered widely, alpha-beta estimate close to the truth
    Part C · A special case

    β = 0 is Module 2 again

    With β = 0 the velocity stays at its starting value 0, the prediction is the old estimate, and the update is x̂ + α(z − x̂): Module 2’s low-pass filter, number for number.

    Part C · Hands-on in MATLAB

    Lab C: where does the velocity come from?

    m03_lab.msection “Lab C”about 4 minutes

    Do this

    1. Run the section Lab C. The readings are perfect; the table is the one you just stepped through.
    2. Set beta = 0 and run again. Then try beta = 0.3.
    3. Each time, read the last line: the velocity estimate at 9.0 s (true 8.0 m/s) and the position gap.

    Then answer

    At 9.0 s, what are the velocity estimate and the position gap for each β?

    β = 0.1: m/s and m behind. β = 0: m/s and m behind (Module 2’s lag). β = 0.3: m/s and m behind.

    Why does the velocity estimate never quite reach the true velocity during the push?

    While the sled accelerates, a residual only appears once the estimate has already fallen behind. The two settle into a steady gap where the residuals add exactly the 0.4 m/s per reading that the sled gains.

    Click a question to check your answer.

    Part C · Your turn to write it

    TODO 4: correct the velocity

    m03_student.mblock “TODO 4”about 6 minutes

    Write it

    1. Find the block TODO 4 in m03_student.m.
    2. Replace the placeholder so that correctVel(v_pred, r, beta, dt) nudges the predicted velocity by beta * r / dt. One line.
    3. Press Run.

    How you know it works

    • The Command Window prints TODO 4 PASS. If TODOs 1 to 3 pass too, your result is saved to the scoreboard.
    • Step tables 3 and 4 now match your hand tables, velocity column included.
    • Kinematics › Velocity shows a velocity estimate that follows the truth, next to the hopeless differenced velocity.
    Part C · Recap

    What we just saw

    What we just saw

    • A velocity estimate can be built from position readings alone: each residual says the sled is further (or less far) than predicted.
    • During an acceleration every residual has the same sign, so the velocity ratchets up, ending a steady gap behind.
    • With β = 0 the filter is exactly Module 2’s low-pass filter. The velocity correction is the only new ingredient.

    Going further in MATLAB (optional)

    1. Set beta_hand = 0 in section 3 of the script and run again: the velocity column of Step table 3 stays 0 and the positions are Module 2’s. Set it back to 0.1.
    Part C · Check your understanding

    Before moving on

    1. β = 0.1, Δt = 0.2 s, and the residual is +0.5 m. By how much does the velocity estimate change?

    (0.1 / 0.2) × 0.5 = +0.25 m/s.

    2. The readings are perfect and the sled coasts at a steady 16 m/s. The filter has settled. What are the residuals, and what does the velocity estimate do?

    The residuals are zero: the constant-velocity prediction is exactly right. The velocity estimate stays at 16 m/s.

    3. Why does one noisy reading move the velocity estimate only a little?

    Because β is small. A residual of 2 m on its own would suggest a 10 m/s speed error; with β = 0.1 the filter believes half of that per reading, and later readings pull it back if it was noise.

    Module 3 · Part D of F

    Choosing the two gains

    The model removes the lag of Module 2 at constant speed, but not while the sled accelerates. Two gains now share the trade-off between noise and lag.

    • Smooth and on time at constant speed.
    • Three gain pairs side by side, and the lag while accelerating, worked and derived.
    • Find the best pair by trying it yourself.
    Lag, revisited

    Smooth and on time

    At constant speed, the constant-velocity model predicts the next position exactly. The residuals are pure noise with zero average, so nothing drags the estimate behind: no lag.

    The filter can therefore use a smooth position gain (α = ) without paying for it in lag.

    This run: Module 2 at α = 0.30 m, Module 2 at α = 0.78 m, alpha-beta m.

    Position error over the run: Module 2 with a smooth gain lags many meters, Module 2 with a fast gain is noisy, alpha-beta is small and centered
    Part D · Side by side

    The same run at three gain pairs

    Velocity estimates over the first 20 s, then the cycles from 6.0 s for each pair: position error and velocity estimate.

    timetrue verror, 0.2 / 0.02v̂, 0.2 / 0.02error, 0.5 / 0.1v̂, 0.5 / 0.1error, 0.8 / 0.4v̂, 0.8 / 0.4
    The cost of the model

    But it lags while accelerating

    During a constant acceleration a, the constant-velocity prediction is always a little short. The estimate settles a fixed distance behind:

    lag=(1−α)aΔt2β
    • At the best gains, the m/s² push gives m (measured m); the 2.5 m/s² brake m.
    • Lag now depends on acceleration, not speed, and it shrinks as β grows.
    Lag during the push versus velocity gain: measured points follow the formula; lag during the coast is zero
    Part D · By hand

    Perfect readings: how far behind during the push?

    Noise-free readings, α = 0.5, during the +2 m/s² push from 5 to 13 s. Two velocity gains. The dashed lines are the formula (1 − α) a Δt² / β.

    timetrue vgap, β = 0.1velocity gapgap, β = 0.05velocity gap

    Press the right arrow to reveal one row at a time.

    Part D · Hands-on in MATLAB

    Lab D1: the lag while accelerating

    m03_lab.msection “Lab D1”about 4 minutes

    Do this

    1. Run the section Lab D1: perfect readings, α = 0.5, β = 0.1.
    2. Set beta = 0.2 and run again, then beta = 0.05.
    3. Each time, compare the settled gap with the formula on the last line.

    Then answer

    Where does the gap settle during the push for each β?

    β = 0.1: m (formula ). β = 0.2: m (formula ). β = 0.05: m (formula ).

    In the figure, what does the gap do during the coasts?

    It returns to zero. At constant speed the constant-velocity model is exact, so the filter catches up completely. Lag now follows acceleration, not speed.

    Click a question to check your answer.

    Listening to the filter

    The residuals lean when the model is wrong

    If the model were right, the residuals would be pure sensor noise: centered on zero.

    During each push the readings keep landing a little ahead of the predictions, and during the brake a little behind. The running mean of the residuals tracks the acceleration we left out of the model.

    A filter’s residuals are its own report on how well its model fits. Module 5 turns this into a formal health check, and Module 8 builds on it.
    Residuals over the run with their running mean, which shifts during each acceleration phase
    Choosing the gains

    Two gains, one trade-off

    Every pair (α, β) run on noise draws:

    • Best for position: α = , β = , giving m and m/s.
    • Best for velocity alone: α = , β = , giving m/s but m.

    Small β: velocity is smooth but slow to follow a push (more lag). Large β: it follows quickly but jitters with every reading.

    Maps of position and velocity error over the two gains with the best points marked
    Watch the trade-off

    Sweeping the velocity gain

    Animation: as the velocity gain grows, the velocity estimate goes from sluggish to jittery while the error curves trace valleys

    α held at . Left: the velocity estimate. Right: where that β sits on the error curves. The animation ends at the best β.

    Part D · Hands-on in MATLAB

    Lab D2: find the best gains in five tries

    m03_lab.msection “Lab D2”about 5 minutes

    Do this

    1. Run the section Lab D2 with α = 0.5, β = 0.1 and note both errors.
    2. Pick a new pair, run again, and keep going for five tries. Aim for the smallest position error.
    3. Compare your best pair with the sweep on the previous slides.

    Then answer

    What is the best you can do?

    The hand gains give m and m/s (raw sensor m). The sweep’s best pair, α = , β = , gives m and m/s on this run.

    Is the best pair for position also best for velocity?

    No. Velocity likes a smaller β (smoother) and a smaller α; position likes a little more of both. The gain maps two slides back show the two valleys in different places.

    Click a question to check your answer.

    Part D · Recap

    What we just saw

    What we just saw

    • At constant speed the prediction is exact, so a smooth α costs no lag.
    • While accelerating, the estimate settles (1 − α) a Δt² / β behind: lag now follows acceleration, and grows as β shrinks.
    • Small β: smooth velocity, slow to follow. Large β: quick but jittery. The best pair depends on what you care about.

    Going further in MATLAB (optional)

    1. Open Choosing the gains › Position error map. Is the valley narrow in α, in β, or both?
    2. In the Playground, set β to 0.01 and watch the velocity during the brake (32 to 40 s). Then try 0.5.
    3. Open Choosing the gains › Theory check. Why do the green squares sit at zero?
    Part D · Check your understanding

    Before moving on

    1. The sled coasts at a steady 20 m/s. Roughly how far behind does the alpha-beta estimate sit once it settles? What about Module 2’s low-pass filter at α = 0.3?

    Alpha-beta: about zero, because the constant-velocity prediction is exact at constant speed. Low-pass: vΔt(1 − α)/α = 20 × 0.2 × 0.7 / 0.3 ≈ 9.3 m.

    2. With α = 0.3 and β = 0.075, how far behind is the estimate during the 2.5 m/s² brake?

    (1 − 0.3) × 2.5 × 0.2² / 0.075 = 0.93 m (ahead of the truth, since the sled is slowing).

    3. Why did we need the true trajectory to find the best gains?

    The sweep scored each pair against the truth. A real system never has the truth, so it needs another way to set the gains: Modules 4 and 5.

    Module 3 · Part E of F

    A bad start

    Switch the filter on while the sled is already moving and it starts with the wrong velocity. With fixed gains, how long does it take to recover?

    • A late start with velocity 0, stepped through cycle by cycle.
    • Try other starting velocities yourself.
    • Why fixed gains are the wrong tool at start-up.
    Part E · Predict first

    Switched on at 13 s

    The filter is switched on at 13.0 s, when the sled is already coasting at 16 m/s. It starts at the first reading with a velocity estimate of 0, and the hand gains α = 0.5, β = 0.1. How long until its velocity is within 3 m/s of the truth?

    1. One reading.
    2. About half a second (2 or 3 readings).
    3. About one and a half seconds (8 or 9 readings).
    4. More than five seconds.

    Commit to an answer before you go on. The next slides step through what really happens.

    Part E · By hand

    Cycle by cycle after a late start

    Starting at the 13.0 s reading with velocity 0. The sled covers 3.2 m between readings, so the first predictions fall far short.

    cycletimereadingpredictionresidualvelocity v̂ + 0.5 rtrue v

    Press the right arrow to reveal one row at a time.

    Part E · Hands-on in MATLAB

    Lab E: a bad start

    m03_lab.msection “Lab E”about 4 minutes

    Do this

    1. Run the section Lab E: switched on at 13 s with v_start = 0.
    2. Set v_start = 16 (the true speed) and run again, then v_start = 30.
    3. Each time, read the last line: when the estimate gets within 3 m/s and stays there for a second.

    Then answer

    How long does each start take?

    v_start = 0: cycle ( s). v_start = 16: cycle ( s, it starts right). v_start = 30: cycle ( s).

    Would a larger β help?

    At start-up, yes: it catches up faster. But the same β would then make the velocity jittery for the rest of the run. Fixed gains cannot be right both at the start and later.

    Click a question to check your answer.

    Part E · Over many noise draws

    Fixed gains recover slowly

    The same late start at the best gains, averaged over noise draws:

    • Started with velocity 0: m/s off after one second, about s to recover.
    • Started with the true velocity: about m/s from the start, the filter’s usual noise.
    At start-up the filter knows nothing about the speed, so it should trust the readings a lot. Once it has seen a few, it should trust them less. A fixed gain does neither.
    Velocity error after a late start: a filter started with velocity zero takes seconds to recover
    Part E · Recap

    What we just saw

    What we just saw

    • A wrong starting velocity makes every early prediction miss by the same amount, so the residuals all lean one way.
    • With fixed gains the filter recovers at a fixed pace: about one and a half seconds here.
    • The right gain at start-up is large and should shrink as the filter learns. Fixed gains cannot do that.

    Going further in MATLAB (optional)

    1. Open Inside the filter › Late start, then Step by step › Late start at 13 s. How many readings until the velocity is within 3 m/s?
    Part E · Check your understanding

    Before moving on

    1. Started with velocity 0 at the 13.0 s reading (64.87 m). The next reading is 68.05 m. With α = 0.5 and β = 0.1, what are the new position and velocity?

    Prediction 64.87 m. Residual 3.18 m. Position 64.87 + 0.5 × 3.18 = 66.46 m. Velocity 0 + 0.5 × 3.18 = 1.59 m/s.

    2. Why do the early residuals keep landing ahead of the predictions?

    The predictions use the too-small velocity estimate, while the sled covers 3.2 m per reading.

    3. What would a smarter filter do at start-up?

    Use large gains at first, when it knows nothing, and smaller ones as it learns, like the 1/k gain of Module 2’s running average. Modules 4 and 5 make that precise.

    Module 3 · Part F of F

    Run it, score it, and what still goes wrong

    You have written the filter one line at a time. Run the whole thing, put it on the scoreboard, and look at what fixed gains still cannot do.

    • The finished script, tab by tab.
    • Two new bars: velocity joins the scoreboard.
    • Three weaknesses, and the idea that fixes all of them.
    Part F · Hands-on in MATLAB

    Run the finished script

    m03_student.mall four TODOs doneabout 10 minutes

    Do this

    1. Press Run (F5). All four TODOs should print PASS, and the last line should say the scoreboard entries were saved.
    2. Read the report: the best gains, their errors, Module 2 on the same draws, and the lag check.
    3. Open each group of tabs in turn, using the table on the right.
    4. Finish in the Playground: drag both gains and watch the velocity estimate.
    KinematicsPosition, velocity, acceleration
    ErrorsPosition error vs Module 2; velocity error
    Choosing the gainsError maps; theory check
    Inside the filterPredict and correct; residuals; late start
    Step by stepParked; the push; a late start
    Playground, ScoreboardDrag both gains; position and velocity charts

    A TODO still says FAIL? Go back to its green slide: Part A for TODO 1, Part B for TODOs 2 and 3, Part C for TODO 4.

    Results

    What you should see

    At the best gains, on the same readings as Modules 1 and 2:

    • Position RMS error: m.
    • Velocity RMS error: m/s.

    What error remains follows the acceleration plot below it: pushes pull it negative, the brake pulls it positive, coasts sit on zero.

    Alpha-beta position error with its running mean, above the true acceleration
    The scoreboard

    Position m → m, and a velocity estimate

    Position scoreboard: raw sensor, low-pass, alpha-beta
    Position: about % better than Module 2.
    Velocity scoreboard: differencing versus alpha-beta
    Velocity, a new chart: differencing vs the filter.
    What still goes wrong

    Who chooses the gains?

    • We tuned against the truth. The best gains came from a sweep scored against the true trajectory, which a real system never has.
    • The gains never change. Switched on at 13 s with no idea of the speed ( m/s), the filter takes about s to recover. Early on it should trust readings more; later, less.
    • It does not know how good it is. The filter gives a number with no error bar.
    Next: Module 4 gives every estimate an uncertainty and a rule for combining two of them. Module 5 lets the uncertainties set the gain at every step.
    Velocity error after a late start: a filter started with velocity zero takes seconds to recover