ENع
→Derivatives
الدرس 22 · الجبر الخطي والتفاضل والتكامل

Derivatives: the slope at a point

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة9 خطوات5 أسئلةAdvanced
عن هذا الدرس

A derivative tells you how fast a function changes at one exact point: the slope of its curve there. Follow one worked example, f(x) = x² at x = 3. Watch a secant line turn into the tangent as the step h shrinks, see the slopes 7, 6.1 and 6.01 close in on the limit 6, estimate it in Python (and see floats break when h is too small), then use the derivative to roll downhill with gradient descent, the core move of machine learning.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 22 · تفاعلي
01 / 09

How steep is a curve?

A derivative measures how fast a function's output changes as its input changes, at one exact point. A straight line has one slope everywhere: rise ÷ run. The curve f(x) = x² gets steeper as x grows. How steep is it at x = 3?

>>> def f(x): return x**2
...
>>> f(3)
9
>>> [f(x) for x in range(5)]
[0, 1, 4, 9, 16]
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. How steep is a curve?

    A derivative measures how fast a function's output changes as its input changes, at one exact point. A straight line has one slope everywhere: rise ÷ run. The curve f(x) = x² gets steeper as x grows. How steep is it at x = 3?

    >>> def f(x): return x**2
    ...
    >>> f(3)
    9
    >>> [f(x) for x in range(5)]
    [0, 1, 4, 9, 16]
  2. Slope between two points

    Take a second point Q a step h to the right of P. With h = 1, Q is (4, 16). The straight line through P and Q is called a secant. Its slope is rise ÷ run: (16 − 9) ÷ 1 = 7.

    >>> def f(x): return x**2
    ...
    >>> (f(4) - f(3)) / 1        # rise ÷ run
    7.0
  3. Shrink the step

    Now shrink h. With h = 0.1, Q is (3.1, 9.61) and the slope is 0.61 ÷ 0.1 = 6.1. With h = 0.01 it is 6.01. As Q slides toward P, the secant turns and its slope closes in on 6.

    >>> def f(x): return x**2
    ...
    >>> for h in [1, 0.1, 0.01]:
    ...     print(h, (f(3 + h) - f(3)) / h)
    ...
    1 7.0
    0.1 6.100000000000012
    0.01 6.009999999999849
  4. Algebra shows the target

    Expand: (3 + h)² − 9 = 6h + h². Divide by h, and the secant's slope is exactly 6 + h for every h that is not 0. So as h shrinks toward 0, the slope gets as close to 6 as we like.

    >>> from fractions import Fraction
    >>> h = Fraction(1, 100)
    >>> ((3 + h)**2 - 9) / h       # exact, no rounding
    Fraction(601, 100)
    >>> 6 + h
    Fraction(601, 100)
  5. The limit is the derivative

    That target is called a limit. The derivative is f′(x) = lim (f(x + h) − f(x)) ÷ h as h → 0. Here f′(3) = 6. The line through P with slope 6 is the tangent: it touches the curve at P.

    >>> def f(x): return x**2
    ...
    >>> for k in range(1, 6):
    ...     h = 10**-k
    ...     print(h, round((f(3 + h) - f(3)) / h, 6))
    ...
    0.1 6.1
    0.01 6.01
    0.001 6.001
    0.0001 6.0001
    1e-05 6.00001
  6. A slope for every x

    At any x the same algebra gives (x + h)² − x² = 2xh + h², so f′(x) = 2x. Slide P along the curve: the tangent's slope is 2, 4, then 6 at x = 1, 2, 3. In general, xⁿ has derivative n·xⁿ⁻¹.

    >>> [2*x for x in (1, 2, 3)]      # f'(x) = 2x
    [2, 4, 6]
    >>> def g(x): return x**3
    ...
    >>> round((g(2 + 1e-6) - g(2)) / 1e-6, 3)   # 3·2² = 12
    12.0
  7. Estimate it in code

    A program can estimate a derivative with a small h: slope(x, h) returns (f(x + h) − f(x)) ÷ h. At x = 3 with h = 0.001 it gives 6.000999999999479. That is 6 + h, give or take a tiny rounding error.

    >>> def f(x): return x**2
    ...
    >>> def slope(x, h):
    ...     return (f(x+h)-f(x))/h
    ...
    >>> slope(3, 0.001)
    6.000999999999479
  8. Too small breaks it

    A tinier h does not always help. A Python float keeps about 16 significant digits (the digits that carry real information). The same slope() with h = 10⁻⁸ gives 5.99999996, with 10⁻¹⁵ 5.33, and with 10⁻¹⁶, 3 + h rounds to 3: slope 0.0.

    >>> def f(x): return x**2
    ...
    >>> def slope(x, h):
    ...     return (f(x+h)-f(x))/h
    ...
    >>> for h in [1e-8, 1e-15, 1e-16]:
    ...     print(h, slope(3, h))
    ...
    1e-08 5.999999963535174
    1e-15 5.329070518200751
    1e-16 0.0
    >>> 3 + 1e-16 == 3
    True
  9. Rolling downhill to learn

    Machine learning uses derivatives to shrink an error. Gradient descent steps against the slope, with a step size (learning rate) of 0.25: x ← x − 0.25 · f′(x). From x = 3: 3 − 0.25 × 6 = 1.5, then 0.75, then 0.375.

    >>> x = 3
    >>> for step in range(3):
    ...     x = x - 0.25 * 2*x       # f'(x) = 2x
    ...     print(x)
    ...
    1.5
    0.75
    0.375

اختبار يتاح بعد الخطوة الأخيرة.