Article Image
read

The First and Second Derivatives and Local Minima

Whilst brushing up on linear algebra, I stumbled upon this webpage that provides a nice and short explanation on the first and second derivatives. To deepen my understanding I visualised the relationship between the first and second derivatives and local minimum / maximum.

The Meaning of the First and Second Derivatives

  • the function
  • its first derivative is the slope of the tangent line at point x
    • it tells whether the function is increasing or decreasing and how much it is increasing or decreasing
    • if the first derivative is 0, x is called a critical point of
  • its second derivative
    • it tells if the first derivative is increasing or decreasing. when the second derivative is positive, the curve is concave up, and vice versa.
    • when the second derivative is 0 then we do not know anything new about the behaviour of at that point

Critical Points and the Second Derivative Test

  • we can use the second derivative to find out when x is a local maximum or minimum
  • suppose that x is a critical point (first derivative = 0) and the second derivative is positive.
    • the second derivative tells us that the first derivative is increasing at that point and the graph is concave up.
    • the only way to visualise this is local minimum where the slope of the function is zero but the graph is concave up.
    • when the second derivative is negative with x being the critical point, it means that x is the local maximum.

Visualisation with Python

Sympy is a great python library when playing with mathematical symbols and complex formulas. Taking a derivative is easy with Sympy.

import numpy as np
from sympy import *
import matplotlib.pyplot as plt

%matplotlib inline

Suppose

x = Symbol('x')
y = x**3 - 9*x**2 + 15*x - 7
## the first derivative
yfirst = y.diff(x)
print(yfirst)

3x**2 - 18x + 15

## the second derivative
ysecond = yfirst.diff(x)
print(ysecond)

6*x - 18

## input x range
test_x = np.linspace(-1, 7, 50)
## corresponding y, first_derivative, second_derivative
test_y = [y.subs({x:v}) for v in test_x]
test_y_f = [yfirst.subs({x:v}) for v in test_x]
test_y_s = [ysecond.subs({x:v}) for v in test_x]
fig, ax = plt.subplots(figsize=(10, 10))
ax.plot(test_x, test_y, color='black', label='function_f')
ax.plot(test_x, test_y_f, color='red', label='first_order')
ax.plot(test_x, test_y_s, color='blue', label='second_order')
ax.plot(test_x, np.zeros(len(test_x)), 'g--', label='y=0')

circle1 = plt.Circle((1, 0), 1, color='red', fill=False)
circle2 = plt.Circle((5, -32), 1, color='r', fill=False)

ax.add_artist(circle1)
ax.add_artist(circle2)

plt.annotate('local maximum', xy=(1, 0), xytext=(1, 2.5), fontsize=15)
plt.annotate('local minimum', xy=(5, -32), xytext=(5, -30), fontsize=15)

ax.legend(fontsize=15)
plt.show()

png

As stated above, the function is at local maximum when the first derivative is at 0 and the second derivative is negative. And it’s at local minimum when the second derivative is positive.

Blog Logo

Junsik Whang


Published

Image

jsideas

a novice's journey into data science

Back to Overview