Derivatives

Step 8 of 18 in Foundations · Concept · Easy

A derivative is the slope: how much the output changes for a tiny change in input. Learning algorithms use it to know which way to nudge parameters.

The lesson

The derivative f′(x) is the slope of f at x. Nudge x by a tiny h and the output moves by about f′(x)·h. A positive derivative means raising x raises f.

The rules you will use constantly: (xⁿ)′ = n·xⁿ⁻¹, (eˣ)′ = eˣ, (sin x)′ = cos x, and a sum differentiates term by term.

Worked example: f(x) = x² at x = 3
  1. By nudging: (f(3.001) − f(3)) / 0.001 = (9.006001 − 9) / 0.001 = 6.001
  2. By rule: f′(x) = 2x, so f′(3) = 6
  3. The two agree better as the nudge shrinks.

On a computer you estimate the slope by nudging. The one-sided version is (f(x + h) − f(x)) / h. The central difference steps both ways, (f(x + h) − f(x − h)) / 2h, which cancels the first error term: it is off by about h², the one-sided version by about h.

Common mistake. A smaller h is not always better. Floats keep about 16 digits. At h = 1e-15, f(x + h) and f(x − h) agree in nearly all of them, and the subtraction leaves mostly rounding noise. Around 1e-5 works well for the central difference.

In learning, the derivative of the loss with respect to a parameter says which way to nudge that parameter.

Check yourself

  1. f(x) = x³. What is f′(2)?

    Show the answer

    12. f′(x) = 3x², so f′(2) = 3·4 = 12.

  2. At some point f′(x) = −4. To make f smaller you should:

    • decrease x
    • leave x alone
    • increase x
    Show the answer

    increase x. A negative slope means f falls as x rises. Step against the derivative: here, increase x.

  3. f(x) = 3x² + 2x. What is f′(1)?

    Show the answer

    8. f′(x) = 6x + 2, so f′(1) = 8.

Build it

  1. Numerical derivative

    derivative(f, x, h) with the central difference (f(x+h) − f(x−h)) / 2h. Test on x², sin x, eˣ.

    Done when: Matches the analytic derivative to 1e-6 and you can show what happens when h is too small.

    Runs in your browser: write the code on the map and the tests mark it.

Resources

Earn this star

On the map, your answers are marked and you log what you built. Finish the step and its star lights.

Open this step on the map