3.2. Differentiation and Critical Points#

3.2.1. Learning Goals:#

After working through these notes, you will be able to:

  1. Determine critical points for polynomial functions of one variable

  2. Determine critical points for exponential and logarithmic functions

  3. Determine critical points for composite functions

3.2.2. Coding concepts:#

The following coding concepts are used in this notebook:

  1. Variables

  2. Functions

  3. Plotting with matplotlib

3.2.3. Critical Points#

Critical points of a function are local maxima or minima in the function. For example, see the 1D function plotted below and the identification of the critical points.

Hide code cell source
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# setup plot 
fontsize=14
fig = plt.figure(figsize=(4,4), dpi= 120, facecolor='w', edgecolor='k')
plt.grid(which='major', axis='both', color='#808080', linestyle='--')
plt.xlabel("$x$",size=fontsize)
plt.ylabel("$f(x)$",size=fontsize)
x = np.arange(-0.5,2.65,0.1)
def f(x):
    return 2*x**3 - 6.5*x**2 + 5*x + 11
plt.plot(x,f(x),lw=2,c='k')
crit_x = np.roots([6,-13,5])
plt.scatter(crit_x[1],f(crit_x[1]),c="tab:red",s=30)
plt.annotate("Maximum", xy=(crit_x[1],f(crit_x[1])+0.25), color="tab:red",fontsize=fontsize)
plt.scatter(crit_x[0],f(crit_x[0]),c="tab:blue",s=30)
plt.annotate("Minimum", xy=(crit_x[0],f(crit_x[0])-0.75), color="tab:blue",fontsize=fontsize)
plt.tight_layout();
../_images/adb6242b033128e9df7e2dd0d0d2727eaab0b17d0fb52824f89152b1d874cddb.png

The determination of critical points of functions of one or more variables is fundamental to various aspects of physical chemistry. In these notes, I will present the basic approach for determining critical points of functions of known analytical form and then go through how to differentiate common functions.

The critical points of one dimensional functions are the local maxima and minima of this function. The \(x\) values of these points are determined by setting the derivative of the function to zero and solving for the \(x\) value(s). The dependent values are then determined by plugging in these \(x\) values.

Consider the example below.

3.2.3.1. Example: Determining Critical Points of a Polynomial#

Determine the critical points of the function

(3.17)#\[\begin{equation} f(x) = x^3 + 2x^2 - 4x \end{equation}\]

I start by plotting the function.

Hide code cell source
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# setup plot 
fontsize=14
fig = plt.figure(figsize=(4,4), dpi= 120, facecolor='w', edgecolor='k')
plt.grid(which='major', axis='both', color='#808080', linestyle='--')
plt.xlabel("$x$",size=fontsize)
plt.ylabel("$f(x)$",size=fontsize)
x = np.arange(-3.5,2.25,0.1)
def f(x):
    return x**3 + 2*x**2 -4*x
plt.plot(x,f(x),lw=2,c='k')
crit_x = np.roots([3,4,-4])
plt.scatter(crit_x[0],f(crit_x[0]),c="tab:red",s=30)
plt.annotate("Maximum", xy=(crit_x[0],f(crit_x[0])+0.25), color="tab:red",fontsize=fontsize)
plt.scatter(crit_x[1],f(crit_x[1]),c="tab:blue",s=30)
plt.annotate("Minimum", xy=(crit_x[1],f(crit_x[1])-0.75), color="tab:blue",fontsize=fontsize)
plt.tight_layout();
../_images/0529f4ebef87d4be7cc52de853940a8554fbfc05341dc7c36ea180095d1ba9b8.png

To determine the critical points, we start by determining the derivative of \(f\) with respect to \(x\)

(3.18)#\[\begin{equation} f'(x) = \frac{df}{dx} = 3x^2 + 4x - 4 \end{equation}\]

We determine the \(x\) values of the critical points (there will be two since the derivative is a quadratic and thus will have two roots), we determine the roots of the derivative equation. That is we set the derivative to zero and solve for \(x\):

(3.19)#\[\begin{align} 0 &= 3x^2 + 4x - 4 \end{align}\]

This can be solved in various ways: (1) the quadratic formula, (2) factoring, when possible, and/or (3) using a calculator to solve for the roots. I will use the numpy roots routine in the code snippet below.

print("Roots:", np.roots([3,4,-4]))
Roots: [-2.          0.66666667]

From the results above we see that the roots of the derivative equation are:

(3.20)#\[\begin{align} x_{critical} = -2, \frac{2}{3} \end{align}\]

In order to find the f(x) values at these critical points we plug the above \(x\) values into \(f(x)\) (careful not to plug into \(f'(x)\) and also be careful of negative signs):

(3.21)#\[\begin{align} f(-2) &= (-2)^3 + 2*(-2)^2 -4\cdot(-2) \\ &= -8 + 8 + 8 \\ &= 8 \end{align}\]
(3.22)#\[\begin{align} f(\frac{2}{3}) &= (\frac{2}{3})^3 + 2*(\frac{2}{3})^2 -4\cdot(\frac{2}{3}) \\ &= -1.48 \end{align}\]

So, we would report the critical points as \((-2,8)\) and \((\frac{2}{3},-1.48)\).

print(np.roots([3,4,-4]),f(np.roots([3,4,-4])))
[-2.          0.66666667] [ 8.         -1.48148148]

3.2.4. Derivatives of common functions#

3.2.4.1. Polynomials#

Derivatives of polynomials show up regulary. These can be determined by the basic rule:

(3.23)#\[\begin{equation} \frac{d}{dx} x^n = nx^{n-1} \end{equation}\]

Or, more generally, if we have a polynomial function with a constant, \(A\), in front of \(x\) we have

(3.24)#\[\begin{equation} \frac{d}{dx} A\cdot x^n = A\cdot n\cdot x^{n-1} \end{equation}\]

3.2.4.2. Logarithmic functions:#

(3.25)#\[\begin{equation} \frac{d}{dx} \ln(x) = \frac{1}{x} \end{equation}\]

3.2.4.3. Exponential Functions#

(3.26)#\[\begin{equation} \frac{d}{dx}e^x = e^x \end{equation}\]

3.2.5. Rules for Differentiation of Composite Functions#

3.2.5.1. Addition of Functions#

Differentiation of a composite function that is a sum of two functions is just the sum of the derivative of those two functions. This can be written as

(3.27)#\[\begin{equation} \frac{d}{dx}\left(f(x) + g(x)\right) = \frac{df}{dx} + \frac{dg}{dx} \end{equation}\]

This rule comes up all the time when differentiating polynomials. Example:

(3.28)#\[\begin{align} \frac{d}{dx}\left(x^2 + 2x + 1\right) &= \frac{d}{dx}(x^2) + \frac{d}{dx}(2x) + \frac{d}{dx}(1) \\ &= 2x + 2 \end{align}\]

3.2.5.2. Product Rule#

The derivative of a composite function that is a product of two functions is not as straightforward as the derivative of a function that is a sum of two functions. The product rule for differentiation can be written as

(3.29)#\[\begin{equation} \frac{d}{dx}\left( f(x)\cdot g(x)\right) = g(x)\frac{df}{dx} + f(x)\frac{dg}{dx} \end{equation}\]

This rule also comes up regularly. Here is an example of its use:

(3.30)#\[\begin{align} \frac{d}{dx}\left(x\cdot e^x\right) &= e^x\frac{dx}{dx} + x\frac{de^x}{dx} \\ &= e^x + xe^x \\ &= (x+1)e^x \end{align}\]

3.2.5.3. The Chain Rule#

A composite function that cannot be written as a simple product or sum of two other functions, such as the log of a polynomial, must be differentiated using the chain rule. The chain rule can be thought of in various ways but I write it here as

(3.31)#\[\begin{equation} \frac{d}{dx}\left( f(g(x)) \right) = \frac{df}{dx}|_{g(x)}\frac{dg}{dx} \end{equation}\]

Here is an example

(3.32)#\[\begin{align} \frac{d}{dx}\left(\ln(2x^2+3x+1)\right) &= \frac{d\ln(x)}{dx}|_{2x^2+3x+1}\frac{d(2x^2+3x+1)}{dx} \\ &= \frac{1}{2x^2+3x+1}\left(4x+3\right) \\ &= \frac{4x+3}{2x^2+3x+1} \end{align}\]
import numpy as np
np.roots([6,-2])
array([0.33333333])
3*(1/3)**2-2*1/3+1
0.6666666666666667
(3.33)#\[\begin{align} \frac{d}{dx}\left(\ln( 3x^2-2x + e^{4x^2})\right) &= \frac{1}{3x^2-2x + e^{4x^2}}\left( 6x-2+8xe^{4x^2}\right) \\ \frac{6x-2+8xe^{4x^2}}{3x^2-2x + e^{4x^2}} \end{align}\]