Finding the minimum of a function using derivatives
Finding the minimum of a function using derivatives
Estimate the derivative using numerical differentiation, move step by step in the opposite direction of the derivative, intuitively feel "the derivative guides the search direction".
After learning this case study, you will understand: the derivative tells you where the function value will rise, and moving in the opposite direction will cause it to decrease—this is the prototype of gradient descent.
Introduction from life
**Descending a mountain in dense fog with eyes covered
In dense fog where you cannot see your hand in front of your face, you need to walk from the mountaintop to the valley. You cannot see the entire mountain range, only the slope under your feet. Your strategy is simple: feel the steepest direction of the slope -> take a small step in the steepest downhill direction -> stop -> feel again -> take another small step.
"Feeling the slope" is differentiation, and "taking a small step" is updating the position in the opposite direction of the derivative. This method does not need to know the shape of the entire mountain, only the slope underfoot.
Intuitive understanding
For the function (f(x) = (x-3)^2 + 2), the minimum value is at (x=3), and the minimum value is (f(3) = 2).
Starting from (x=10), how to get to (x=3)? No need to solve the equation, just a strategy:
- If the derivative (f'(x) > 0) (the function is increasing), move left (decrease x)
- If the derivative (f'(x) < 0) (the function is decreasing), move right (increase x)
- The larger the absolute value of the derivative, the steeper the slope, and the further you can move.
Mathematical definition
Numerical Derivative (Central Difference)
[ f'(x) \approx \frac{f(x+h) - f(x-h)}{2h} ]
No need to derive the differentiation formula. The approximate value of the derivative is obtained by dividing the difference in function values of two very close points by the distance. (h) should be a very small number, such as (10^{-5}).
Simple Gradient Descent Algorithm
[ x_{t+1} = x_t - \eta \cdot f'(x_t) ]
Where (\eta) is the step size (learning rate), which controls how far to move in each step.
Python Hands-on Practice
Example
import
numpy
as
np
Objective function: f(x) = (x-3)^2 + 2, minimum is at x=3
def
f
(
x
)
:
return
(
x -
3
)
**
2
2
Numerical derivative—using central difference, without symbolic differentiation
def
numerical_derivative
(
f
,
x
,
h
=
1e-5
)
:
"Estimate the derivative of f at x"
return
(
f
(
x + h
)
- f
(
x - h
)
)
/
(
2
- h
)
Simple descent algorithm: move in the opposite direction of the derivative
x
=
10.0
Starting point, intentionally chosen far from the optimal point
step_size
=
0.1
The step size for each move
history
=
[
x
]
for
i
in
range
(
50
)
:
grad
=
numerical_derivative
(
f
,
x
)
x
=
x - step_size * grad
If the derivative is positive, move left; if negative, move right.
history.
append
(
x
)
(
"RUNOOB descent algorithm result:"
)
(
f
"Starting point x = {history[0]:.1f}"
)
(
f
"Endpoint x = {history[-1]:.4f} (True minimum point x=3)"
)
(
f
"Endpoint f(x) = {f(history[-1]):.4f} (True minimum f(3)=2)"
)
(
"\nRUNOOB first 10 steps movement trajectory:"
)
(
f
"{'Step':<6} {'x':<10} {'f(x)':<10} {'Derivative':<10} {'Direction'}"
)
for
i
,
xi
in
enumerate
(
history
[
:
10
]
)
:
grad
=
numerical_derivative
(
f
,
xi
)
direction
=
"To the left"
if
grad
0
else
"To the right"
(
f
"{i:<6} {xi:<10.4f} {f(xi):<10.4f} {grad:<10.4f} {direction}"
)
Original prompt (Chinese, left untranslated)
RUNOOB Descent Algorithm Result:
起点 x = 10.0
终点 x = 3.0000 (真实最小值点 x=3)
终点 f(x) = 2.0000 (真实最小值 f(3)=2)
RUNOOB first 10 steps movement trajectory:
步数 x f(x) 导数 方向
0 10.0000 51.0000 14.0000 往左
1 8.6000 33.3600 11.2000 往左
2 7.4800 22.0704 8.9600 往左
3 6.5840 14.8042 7.1680 往左
4 5.8672 10.1942 5.7344 往左
Application scenarios in AI
| Scenario | Connection to this case |
|---|---|
| Gradient Descent Training | The essence of neural network training: guiding parameter updates in the direction of the partial derivative of the loss function for each parameter |
| Numerical Gradient Check | When debugging backpropagation, using numerical gradients to verify the correctness of analytical gradients—this is the central difference method in this case |
| Hyperparameter Search | Parameter tuning methods like Bayesian Optimization are also searching for the minimum along the "derivative" direction on an unknown function surface |