Concept 01: Rates of Change & Derivatives
▶ Interactive Demo: Rate of Change & Derivative Visualizer
Open the interactive demo below to shrink the time step dt and watch the average speed converge into the exact instantaneous tangent slope.
1. The Real-World Problem: How Fast Are We Moving?
A robot’s wheel encoder does not measure velocity directly. It only reports how many rotations (or meters) the wheel has turned.
Every 20 milliseconds (dt = 0.02s), the robot’s control loop reads the position:
- At
t = 1.00s: Positionx₁ = 2.00 meters. - At
t = 1.02s: Positionx₂ = 2.08 meters.
To find the robot’s speed, we calculate the rate of change:
Speed = (Distance Traveled) / (Time Taken) = (2.08 - 2.00) / 0.02 = 4.0 meters per second
2. Solving It in Code (Java & WPILib)
First-Principles Java: Numerical Derivative
// Sensor position readings (meters) at two timestamps
double x1 = 3.00, t1 = 1.00;
double x2 = 3.42, t2 = 1.05;
// Finite difference derivative: v = dx / dt
double dt = t2 - t1; // 0.05 seconds
double velocity = (x2 - x1) / dt; // 0.42 / 0.05 = 8.40 m/s
System.out.printf("Instantaneous Velocity: %.2f m/s%n", velocity);
3. Bridge to Machine Learning: The Loss Slope
In machine learning:
- When training a model, we measure how much the prediction error changes when a weight
wis tweaked slightly:Slope = dLoss / dw - If the derivative is positive, increasing
wincreases error (bad!). If the derivative is negative, increasingwdecreases error (good!).
4. Review Checkpoints
Checkpoint 1
An encoder reports x = 5.0m at t = 2.0s, and x = 5.15m at t = 2.05s.
What is the average velocity over this interval?
Solution:
v = Δx / Δt = (5.15 - 5.0) / (2.05 - 2.0) = 0.15 / 0.05 = 3.0 m/s.
Checkpoint 2
If a robot’s position curve is flat (horizontal line, x(t) = 3.0m constant), what is its velocity derivative dx/dt?
Solution:
Because position is not changing (dx = 0), the slope is zero: v = dx/dt = 0 m/s (The robot is stationary).