TExcelTAD
Home › Calculus Essentials › Derivatives of Exponentials and Logs

Derivatives of Exponentials and Logs

The exponential exe^x is the one function that is its own derivative, and the natural log ln⁡x\ln x has the simple derivative 1/x1/x. These two facts explain why ee appears everywhere growth, decay and probability show up, and they give the famous shortcut for the sigmoid used in neural networks: σ′=σ(1−σ)\sigma' = \sigma(1 - \sigma).

The core formulas

Function Derivative With the chain rule
exe^x exe^x ddxeu=eu⋅u′\frac{d}{dx} e^{u} = e^{u} \cdot u'
axa^x (a>0a > 0) ax⋅ln⁡aa^x \cdot \ln a au⋅ln⁡a⋅u′a^{u} \cdot \ln a \cdot u'
ln⁡x\ln x (x>0x > 0) 1/x1/x u′/uu' / u
ln⁡∣x∣\ln \lvert x \rvert (x≠0x \ne 0) 1/x1/x u′/uu' / u
log⁡ax\log_a x 1xln⁡a\frac{1}{x \ln a} u′uln⁡a\frac{u'}{u \ln a}

Why exe^x? From the definition,

ex+h−exh=ex⋅eh−1h\frac{e^{x+h} - e^x}{h} = e^x \cdot \frac{e^h - 1}{h}

and ee is exactly the base for which eh−1h→1\frac{e^h - 1}{h} \to 1. For any other base aa that limit is ln⁡a\ln a, which is where the extra factor in ddxax\frac{d}{dx} a^x comes from. Since ln⁡\ln is the inverse of exp⁡\exp, differentiating eln⁡x=xe^{\ln x} = x with the chain rule gives eln⁡x⋅(ln⁡x)′=1e^{\ln x} \cdot (\ln x)' = 1, so (ln⁡x)′=1/x(\ln x)' = 1/x.

Numeric evidence for the special base

python · try it yourself
import math

print("   h        (2^h-1)/h    (e^h-1)/h    (3^h-1)/h")
for k in range(1, 7):
    h = 10.0**-k
    row = [(b**h - 1) / h for b in (2, math.e, 3)]
    print(f"{h:7.0e}   {row[0]:.7f}    {row[1]:.7f}    {row[2]:.7f}")
print(f"ln 2 = {math.log(2):.7f}, ln e = 1, ln 3 = {math.log(3):.7f}")
   h        (2^h-1)/h    (e^h-1)/h    (3^h-1)/h
  1e-01   0.7177346    1.0517092    1.1612317
  1e-02   0.6955550    1.0050167    1.1046692
  1e-03   0.6933875    1.0005002    1.0992160
  1e-04   0.6931712    1.0000500    1.0986726
  1e-05   0.6931496    1.0000050    1.0986183
  1e-06   0.6931474    1.0000005    1.0986129
ln 2 = 0.6931472, ln e = 1, ln 3 = 1.0986123

Base 2 gives a slope factor below 1, base 3 above 1, and e≈2.718e \approx 2.718 sits exactly where the factor is 1.

SymPy confirms

python · try it yourself
import sympy as sp

x = sp.symbols('x', positive=True)
for f in [sp.exp(3 * x), 2**x, sp.log(x**2 + 1), sp.log(x, 10), x * sp.log(x), sp.exp(-x**2)]:
    print(f"d/dx {str(f):<14} = {sp.diff(f, x)}")
d/dx exp(3*x)       = 3*exp(3*x)
d/dx 2**x           = 2**x*log(2)
d/dx log(x**2 + 1)  = 2*x/(x**2 + 1)
d/dx log(x)/log(10) = 1/(x*log(10))
d/dx x*log(x)       = log(x) + 1
d/dx exp(-x**2)     = -2*x*exp(-x**2)

SymPy writes log for the natural log ln⁡\ln, and log(x, 10) becomes log(x)/log(10).

Logarithmic differentiation

For awkward products and powers like xxx^x, take logs first:

y=xx  ⟹  ln⁡y=xln⁡xy′y=ln⁡x+1(differentiate both sides)y′=xx(ln⁡x+1)\begin{aligned} y &= x^x \implies \ln y = x \ln x \\ \frac{y'}{y} &= \ln x + 1 && \text{(differentiate both sides)} \\ y' &= x^x (\ln x + 1) \end{aligned}

Neither the power rule (exponent not constant) nor the axa^x rule (base not constant) applies directly, but logs turn the power into a product.

The sigmoid and its derivative

The logistic sigmoid squashes any number into (0,1)(0, 1):

σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

Differentiate with the chain rule (σ=(1+e−z)−1\sigma = (1 + e^{-z})^{-1}):

σ′(z)=−(1+e−z)−2⋅(−e−z)=e−z(1+e−z)2=[11+e−z]⋅[e−z1+e−z]=σ(z)⋅(1−σ(z))\begin{aligned} \sigma'(z) &= -(1 + e^{-z})^{-2} \cdot (-e^{-z}) \\ &= \frac{e^{-z}}{(1 + e^{-z})^2} \\ &= \left[\frac{1}{1 + e^{-z}}\right] \cdot \left[\frac{e^{-z}}{1 + e^{-z}}\right] \\ &= \sigma(z) \cdot (1 - \sigma(z)) \end{aligned}

The last step uses e−z1+e−z=1−11+e−z\frac{e^{-z}}{1 + e^{-z}} = 1 - \frac{1}{1 + e^{-z}}. This is why a framework only needs to remember the forward output a=σ(z)a = \sigma(z): the backward pass is a⋅(1−a)a \cdot (1 - a), with no extra exponential.

python · try it yourself
import sympy as sp

z = sp.symbols('z')
s = 1 / (1 + sp.exp(-z))
print("σ' − σ(1 − σ) simplifies to:", sp.simplify(sp.diff(s, z) - s * (1 - s)))
print("max of σ' (at z = 0):", sp.diff(s, z).subs(z, 0))

import math
sig = lambda t: 1 / (1 + math.exp(-t))
for t in [-4, -1, 0, 2]:
    a = sig(t)
    numeric = (sig(t + 1e-5) - sig(t - 1e-5)) / 2e-5
    print(f"z = {t:2}  a(1-a) = {a * (1 - a):.6f}   numeric = {numeric:.6f}")
σ' − σ(1 − σ) simplifies to: 0
max of σ' (at z = 0): 1/4
z = -4  a(1-a) = 0.017663   numeric = 0.017663
z = -1  a(1-a) = 0.196612   numeric = 0.196612
z =  0  a(1-a) = 0.250000   numeric = 0.250000
z =  2  a(1-a) = 0.104994   numeric = 0.104994

In software

  • Logistic regression and neural nets: the sigmoid shortcut is used in every backward pass through a sigmoid (logistic regression, neural networks). Paired with cross-entropy loss L=−[yln⁡a+(1−y)ln⁡(1−a)]L = -[y \ln a + (1 - y) \ln(1 - a)], the chain rule gives ∂L/∂z=a−y\partial L/\partial z = a - y: the 1/a1/a from the log cancels the σ′\sigma' neatly.
  • Log-likelihood: statisticians maximise ln⁡(likelihood)\ln(\text{likelihood}) instead of the likelihood itself, because ln⁡\ln turns products into sums and (ln⁡u)′=u′/u(\ln u)' = u'/u is easy to differentiate. Same maximiser, simpler maths, and no underflow.
  • Compound growth and decay: A(t)=A0ektA(t) = A_0 e^{kt} has A′=kAA' = kA. The rate is proportional to the amount, which models interest, population, cooling and radioactive decay. Exponential smoothing in games and UIs (x += (target − x)·(1 − e^(−k·dt))) is frame-rate independent for the same reason (damping).

Exercises

Exercise 1: Chain with an exponential

Differentiate f(x)=4e2x−5f(x) = 4e^{2x - 5}.

Answer

f′(x)=8e2x−5f'(x) = 8e^{2x - 5}

Solution
outer 4eu→4eu,inner u=2x−5→2f′=4e2x−5⋅2=8e2x−5\begin{aligned} &\text{outer } 4e^u \to 4e^u, \qquad \text{inner } u = 2x - 5 \to 2 \\ &f' = 4e^{2x - 5} \cdot 2 = 8e^{2x - 5} \end{aligned}

Exercise 2: Log of a product

Differentiate g(x)=ln⁡((x2+1)(x−3))g(x) = \ln((x^2 + 1)(x - 3)) for x>3x > 3.

Answer

g′(x)=2xx2+1+1x−3g'(x) = \dfrac{2x}{x^2 + 1} + \dfrac{1}{x - 3}

Solution
g=ln⁡(x2+1)+ln⁡(x−3)(log of a product = sum of logs)g′=2xx2+1+1x−3\begin{aligned} g &= \ln(x^2 + 1) + \ln(x - 3) && \text{(log of a product = sum of logs)} \\ g' &= \frac{2x}{x^2 + 1} + \frac{1}{x - 3} \end{aligned}

Exercise 3: Logarithmic differentiation

Differentiate y=xsin⁡xy = x^{\sin x} for x>0x > 0.

Answer

y′=xsin⁡x⋅(cos⁡x⋅ln⁡x+sin⁡xx)y' = x^{\sin x} \cdot \left(\cos x \cdot \ln x + \dfrac{\sin x}{x}\right)

Solution
ln⁡y=sin⁡x⋅ln⁡xy′y=cos⁡x⋅ln⁡x+sin⁡x⋅1xy′=xsin⁡x(cos⁡xln⁡x+sin⁡xx)\begin{aligned} \ln y &= \sin x \cdot \ln x \\ \frac{y'}{y} &= \cos x \cdot \ln x + \sin x \cdot \frac{1}{x} \\ y' &= x^{\sin x} \left(\cos x \ln x + \frac{\sin x}{x}\right) \end{aligned}

Exercise 4: Cross-entropy gradient

For one example with label y=1y = 1, L(z)=−ln⁡σ(z)L(z) = -\ln \sigma(z). Show that dLdz=σ(z)−1\frac{dL}{dz} = \sigma(z) - 1 and check it numerically at z=0.7z = 0.7.

Answer

dLdz=σ(z)−1≈−0.3318\frac{dL}{dz} = \sigma(z) - 1 \approx -0.3318 at z=0.7z = 0.7.

Solution
dLdz=−σ′(z)σ(z)=−σ(1−σ)σ=−(1−σ)=σ−1\frac{dL}{dz} = -\frac{\sigma'(z)}{\sigma(z)} = -\frac{\sigma(1 - \sigma)}{\sigma} = -(1 - \sigma) = \sigma - 1
python · try it yourself
import math

sig = lambda z: 1 / (1 + math.exp(-z))
L = lambda z: -math.log(sig(z))
z, h = 0.7, 1e-6
print(round(sig(z) - 1, 4), round((L(z + h) - L(z - h)) / (2 * h), 4))
-0.3318 -0.3318

Test yourself

What is ddx5x\frac{d}{dx} 5^x?

  1. x⋅5x−1x \cdot 5^{x-1}
  2. 5x5^x
  3. 5x⋅ln⁡55^x \cdot \ln 5
  4. 5x/ln⁡55^x / \ln 5

If a=σ(z)=0.8a = \sigma(z) = 0.8, what is σ′(z)\sigma'(z)?

  1. 0.16
  2. 0.8
  3. 0.2
  4. 0.64

What is ddxln⁡(3x)\frac{d}{dx} \ln(3x)?

  1. 3/x3/x
  2. 1/(3x)1/(3x)
  3. 1/x1/x
  4. ln⁡3/x\ln 3 / x