Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Calculus Essentials

Calculus is the language of change. Chemistry is full of quantities that vary with position, time, temperature, or pressure, and two questions come up again and again: how fast does a quantity change, which is a derivative, and how much of it accumulates, which is an integral. Later in the course the same two operations act on the functions that describe waves and, eventually, quantum states, so it pays to have them at your fingertips now. This page collects the essentials with a picture for each of the two ideas that matter most: the derivative as a slope and the integral as an area. In between sits a short section on partial derivatives, the small extension needed when a function depends on more than one variable, such as a wave u(x,t)u(x,t) that depends on both position and time.

Differentiation

The derivative as a slope

The derivative of ff at a point is the slope of the line tangent to the curve there. We build it from the slope of a secant line through two nearby points and then let those points merge:

As the spacing hh shrinks, the secant line pivots into the tangent line. Drag the slider below for f(x)=x2f(x) = x^2 at x=1x = 1: the secant slope 2+h2 + h closes in on the true tangent slope 2.

The derivative is also the instantaneous rate of change: if ff is position, ff' is velocity; if ff is concentration, ff' is reaction rate. A function is differentiable only where it is smooth, so no corners, jumps, or vertical tangents.

The derivative as a step

Read the definition backwards and it becomes a recipe for moving along the curve. For a small step hh,

f(x+h)f(x)+f(x)h.f(x + h) \approx f(x) + f'(x)\,h .

Knowing the value and the slope at one position is enough to predict the value at a neighboring position: the derivative is the rate at which ff responds to a shift in xx. The green bar in the slider figure measures how far this prediction misses the true f(x+h)f(x+h). For x2x^2 the miss is exactly h2h^2, so halving the step quarters the error, and in the limit the prediction is perfect. That is what “linear approximation” means, and it is why a tangent line is the best straight-line stand-in for a curve near a point.

Higher derivatives predict farther: the Taylor series

The step recipe uses one derivative and works for small steps. Each further derivative extends the reach, and with all of them the prediction becomes exact for any smooth function:

The first two terms are the step recipe (the linear approximation). Adding the a2a^2 term corrects for the bending of the curve, and so on. The figure shows the partial sums for sinx\sin x about x=0x = 0: one derivative is good near the origin, seven derivatives track the curve for more than a full period.

Source
import numpy as np
import matplotlib.pyplot as plt
from math import factorial

x = np.linspace(-2 * np.pi, 2 * np.pi, 600)
fig, ax = plt.subplots(figsize=(7.5, 4))
ax.plot(x, np.sin(x), 'k', lw=2.5, label=r'$\sin x$')
for order, color in zip([1, 3, 5, 7], ['#d1495b', '#edae49', '#66a182', '#2e4057']):
    poly = sum((-1)**(k // 2) * x**k / factorial(k) for k in range(1, order + 1, 2))
    label = '1 derivative' if order == 1 else f'{order} derivatives'
    ax.plot(x, poly, '--', color=color, lw=1.6, label=label)
ax.set_ylim(-2.5, 2.5)
ax.set_xlabel('x')
ax.set_xticks([-2 * np.pi, -np.pi, 0, np.pi, 2 * np.pi])
ax.set_xticklabels([r'$-2\pi$', r'$-\pi$', '0', r'$\pi$', r'$2\pi$'])
ax.axhline(0, color='gray', lw=0.6)
ax.legend(fontsize=8, loc='lower right', ncol=2)
ax.set_title('Fig.2 Taylor polynomials of sin x about 0: more derivatives reach farther')
plt.tight_layout()
plt.show()
<Figure size 750x400 with 1 Axes>

Three expansions are worth knowing by heart, all about x=0x = 0:

ex=1+x+x22!+x33!+,sinx=xx33!+x55!,cosx=1x22!+x44!e^{x} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \cdots, \qquad \sin x = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \cdots, \qquad \cos x = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \cdots

Truncating after the first non-trivial term gives the small-quantity approximations used throughout chemistry: ex1+xe^{x} \approx 1 + x, sinθθ\sin\theta \approx \theta, cosθ1θ2/2\cos\theta \approx 1 - \theta^2/2, and (1+x)n1+nx(1 + x)^n \approx 1 + nx. Keeping the x2x^2 term of any potential energy curve near its minimum is what turns a real vibrating bond into a harmonic oscillator.

Every derivative at one point is a piece of information about the function elsewhere, and the complete set determines the function everywhere. The sum has the shape of the exponential series, which is why one writes f(x+a)=ead/dxf(x)f(x + a) = e^{a\, d/dx} f(x): exponentiating the derivative translates a function by aa. Later in the course the operator d/dxd/dx reappears, dressed as id/dx-i\hbar\,d/dx, as momentum, and this formula is the deep reason momentum and translation belong together.

Reading the first and second derivatives

Most of what a derivative tells you can be read off a graph without any formula. The first derivative reports the direction of the curve:

The second derivative is the derivative of the slope, so it reports how the slope itself is changing, that is, how the curve bends:

The second and third bullets together are the second derivative test. The figure applies all of this to f(x)=x33xf(x) = x^3 - 3x: the first derivative 3(x21)3(x^2 - 1) vanishes at x=±1x = \pm 1, the second derivative 6x6x is negative at -1 (maximum) and positive at +1 (minimum), and it changes sign at x=0x = 0 (inflection).

Source
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2.3, 2.3, 500)
f, fp, fpp = x**3 - 3 * x, 3 * x**2 - 3, 6 * x
UP, DOWN = '#66a182', '#d1495b'

fig, axes = plt.subplots(3, 1, figsize=(7, 8), sharex=True)
ax = axes[0]
ax.plot(x, f, 'k', lw=2.5)
ax.fill_between(x, -6, 6, where=fp > 0, color=UP, alpha=0.12)
ax.fill_between(x, -6, 6, where=fp < 0, color=DOWN, alpha=0.12)
for xc in (-1, 1):
    ax.plot([xc - 0.4, xc + 0.4], [xc**3 - 3 * xc] * 2, color='#2e4057', lw=2)
    ax.plot(xc, xc**3 - 3 * xc, 'o', color='#2e4057', ms=7)
ax.plot(0, 0, 's', color='#edae49', ms=7)
ax.text(-1, 2.6, 'maximum', ha='center', fontsize=9)
ax.text(1, -3.3, 'minimum', ha='center', fontsize=9)
ax.text(0.15, 0.4, 'inflection', fontsize=9, color='#b07d1a')
ax.text(-2.1, 3.6, 'rising', color=UP, fontsize=9)
ax.text(-0.3, 3.6, 'falling', color=DOWN, fontsize=9)
ax.text(1.5, 3.6, 'rising', color=UP, fontsize=9)
ax.set_ylim(-5, 5)
ax.set_ylabel(r'$f = x^3 - 3x$')

ax = axes[1]
ax.plot(x, fp, color='#2e4057', lw=2.5)
ax.axhline(0, color='gray', lw=0.8)
ax.plot([-1, 1], [0, 0], 'o', color='#2e4057', ms=7)
ax.fill_between(x, 0, fp, where=fp > 0, color=UP, alpha=0.25)
ax.fill_between(x, 0, fp, where=fp < 0, color=DOWN, alpha=0.25)
ax.text(-1.75, 1.5, r"$f' > 0$: rising", color=UP, fontsize=9)
ax.text(-0.6, 1.3, r"$f' < 0$: falling", color=DOWN, fontsize=9)
ax.set_ylabel(r"$f' = 3x^2 - 3$")

ax = axes[2]
ax.plot(x, fpp, color='#b07d1a', lw=2.5)
ax.axhline(0, color='gray', lw=0.8)
ax.plot(0, 0, 's', color='#edae49', ms=7)
ax.fill_between(x, 0, fpp, where=fpp > 0, color=UP, alpha=0.25)
ax.fill_between(x, 0, fpp, where=fpp < 0, color=DOWN, alpha=0.25)
ax.text(0.8, -6, r"$f'' > 0$: bends up (bowl)", color=UP, fontsize=9)
ax.text(-2.2, 6, r"$f'' < 0$: bends down (dome)", color=DOWN, fontsize=9)
ax.set_ylabel(r"$f'' = 6x$")
ax.set_xlabel('x')
fig.suptitle("Fig.3 Reading a curve from its derivatives: f' gives direction, f'' gives bending", y=0.995, fontsize=10)
plt.tight_layout()
plt.show()
<Figure size 700x800 with 3 Axes>

Derivatives in motion: velocity and acceleration

When the variable is time, the two derivatives have names everyone knows. If x(t)x(t) is position, then x(t)x'(t) is the velocity, how fast and in which direction, and x(t)x''(t) is the acceleration, how the velocity is changing. Newton’s second law F=mxF = m\,x'' says that forces set the second derivative: the physics enters through the bending of the trajectory, not its slope. That is why equations of motion are second order and need two initial conditions, a position and a velocity.

The cleanest example is x(t)=costx(t) = \cos t. Its velocity is sint-\sin t and its acceleration is cost-\cos t, so

x=x.x'' = -x .

The acceleration is proportional to minus the displacement: the higher the curve climbs, the harder it bends back toward zero. A function whose curvature is proportional to minus itself oscillates, and cosine and sine are exactly the functions that do this. This one relation is the harmonic oscillator, and the same structure returns in space instead of time: sin(kx)\sin(kx) satisfies d2dx2sin(kx)=k2sin(kx)\dfrac{d^2}{dx^2}\sin(kx) = -k^2 \sin(kx), which is why sines and cosines solve the wave equation met later on this page. In quantum mechanics the curvature of a wavefunction measures its kinetic energy, so a wigglier wave means a faster particle.

Source
import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0, 4 * np.pi, 500)
fig, ax = plt.subplots(figsize=(7.5, 3.6))
ax.plot(t, np.cos(t), 'k', lw=2.5, label=r'position $x = \cos t$')
ax.plot(t, -np.sin(t), color='#2e4057', lw=1.8, label=r"velocity $x' = -\sin t$")
ax.plot(t, -np.cos(t), color='#d1495b', lw=1.8, ls='--', label=r"acceleration $x'' = -\cos t = -x$")
ax.axhline(0, color='gray', lw=0.6)
ax.set_xticks([0, np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi])
ax.set_xticklabels(['0', r'$\pi$', r'$2\pi$', r'$3\pi$', r'$4\pi$'])
ax.set_xlabel('t')
ax.set_ylim(-1.8, 1.8)
ax.legend(fontsize=8, loc='upper right', ncol=3)
ax.set_title('Fig.4 Cosine motion: the acceleration is the mirror image of the position')
plt.tight_layout()
plt.show()
<Figure size 750x360 with 1 Axes>

Rules of differentiation

Example: a product with a composition inside

Differentiate f(x)=x2eαx2f(x) = x^2 e^{-\alpha x^2}. Attempt this before peeking at the solution. (Functions of exactly this shape describe vibrating molecules later in the course.)

Table of common derivatives

Function f(x)f(x)Derivative f(x)f'(x)Function f(x)f(x)Derivative f(x)f'(x)
cc0xnx^nnxn1nx^{n-1}
exe^{x}exe^{x}lnx\ln x1x\dfrac{1}{x}
sinx\sin xcosx\cos xcosx\cos xsinx-\sin x
tanx\tan xsec2x\sec^2 xeaxe^{ax}aeaxa\,e^{ax}
arcsinx\arcsin x11x2\dfrac{1}{\sqrt{1-x^2}}arctanx\arctan x11+x2\dfrac{1}{1+x^2}

A few limits underlie these results: limθ0sinθθ=1\displaystyle\lim_{\theta\to 0}\frac{\sin\theta}{\theta}=1, limθ0cosθ1θ=0\displaystyle\lim_{\theta\to 0}\frac{\cos\theta-1}{\theta}=0, and the definition limh0eh1h=1\displaystyle\lim_{h\to 0}\frac{e^{h}-1}{h}=1, which is exactly what makes exe^{x} its own derivative.

Functions of several variables

Partial derivatives

Most quantities in chemistry depend on more than one variable. The pressure of an ideal gas depends on both volume and temperature, P(V,T)=nRT/VP(V,T) = nRT/V, and the displacement of a plucked guitar string depends on where you look and when, u(x,t)u(x,t). Graphically, a function of two variables f(x,y)f(x,y) is a surface over the xyxy plane, and a surface has no single slope: it can be steep along xx and nearly flat along yy.

The fix is to vary one variable at a time. The partial derivative of ff with respect to xx is the ordinary derivative along xx with every other variable frozen:

Nothing new is needed to compute one: treat the other variables as constants and apply the rules of the previous section. Geometrically, slicing the surface with the plane y=y0y = y_0 leaves an ordinary curve, and f/x\partial f/\partial x is the slope of that curve.

Source
import numpy as np
import matplotlib.pyplot as plt

f = lambda x, y: 4 - x**2 - y**2 / 4
fx = lambda x, y: -2 * x
fy = lambda x, y: -y / 2
x0, y0 = 1.0, 1.0

X, Y = np.meshgrid(np.linspace(-2, 2, 60), np.linspace(-3, 3, 60))
fig = plt.figure(figsize=(7.5, 5.5))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, f(X, Y), color='lightgray', alpha=0.35, linewidth=0)

xs = np.linspace(-2, 2, 100)
ys = np.linspace(-3, 3, 100)
ax.plot(xs, np.full_like(xs, y0), f(xs, y0), color='#d1495b', lw=2.5,
        label=r'slice $y = 1$: slope is $\partial f/\partial x$')
ax.plot(np.full_like(ys, x0), ys, f(x0, ys), color='#66a182', lw=2.5,
        label=r'slice $x = 1$: slope is $\partial f/\partial y$')

tx = np.array([x0 - 0.8, x0 + 0.8])
ax.plot(tx, np.full_like(tx, y0), f(x0, y0) + fx(x0, y0) * (tx - x0), '--', color='#d1495b', lw=1.5)
ty = np.array([y0 - 1.2, y0 + 1.2])
ax.plot(np.full_like(ty, x0), ty, f(x0, y0) + fy(x0, y0) * (ty - y0), '--', color='#66a182', lw=1.5)
ax.scatter([x0], [y0], [f(x0, y0)], color='k', s=40)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('f(x, y)')
ax.view_init(elev=25, azim=-60)
ax.legend(loc='upper left', fontsize=8)
ax.set_title('Fig.6 Partial derivatives are slopes of slices through the surface')
plt.tight_layout()
plt.show()
<Figure size 750x550 with 1 Axes>

For the hill f(x,y)=4x2y2/4f(x,y) = 4 - x^2 - y^2/4 in the figure, f/x=2x\partial f/\partial x = -2x and f/y=y/2\partial f/\partial y = -y/2. At the marked point (1,1)(1, 1) the surface drops four times faster along xx than along yy, which is why the red tangent is steeper than the green one.

Second and mixed partial derivatives

Differentiating twice along the same variable gives 2fx2\dfrac{\partial^2 f}{\partial x^2}, written fxxf_{xx}; differentiating once along each gives the mixed partial 2fyx\dfrac{\partial^2 f}{\partial y\,\partial x}, written fxyf_{xy}. For every smooth function the order does not matter:

2fyx=2fxy.\frac{\partial^2 f}{\partial y\,\partial x} = \frac{\partial^2 f}{\partial x\,\partial y}.

For the example above, fxy=2x+cos(xy)xysin(xy)f_{xy} = 2x + \cos(xy) - xy\sin(xy) whichever variable you differentiate first. This innocent-looking equality is the origin of the Maxwell relations of thermodynamics.

The chain rule on a sliding shape

The most important partial-derivative calculation in this course involves a function of the single combination xvtx - vt. Take any smooth profile ff and a constant vv, and define

u(x,t)=f(xvt).u(x,t) = f(x - vt).

At t=0t = 0 the graph of uu is just the graph of ff; at a later time the same shape has moved a distance vtvt to the right. So uu describes a shape sliding at speed vv without changing form. Its partial derivatives follow from the chain rule with the inner function s=xvts = x - vt, for which s/x=1\partial s/\partial x = 1 and s/t=v\partial s/\partial t = -v:

ux=f(s)1=f(s),ut=f(s)(v)=vf(s).\frac{\partial u}{\partial x} = f'(s)\cdot 1 = f'(s), \qquad \frac{\partial u}{\partial t} = f'(s)\cdot(-v) = -v\,f'(s).

Differentiate once more and every factor of vv appears again:

2ux2=f(s),2ut2=v2f(s).\frac{\partial^2 u}{\partial x^2} = f''(s), \qquad \frac{\partial^2 u}{\partial t^2} = v^2 f''(s).

Eliminating ff'' between the two lines gives a relation that holds for every sliding shape, whatever ff is:

2ux2=1v22ut2.\frac{\partial^2 u}{\partial x^2} = \frac{1}{v^2}\,\frac{\partial^2 u}{\partial t^2}.

This is the classical wave equation. Here it emerged from calculus alone; in the waves lecture the same equation gets its physical meaning, and the shorthand uxxu_{xx} and uttu_{tt} used there is the subscript notation introduced above. The same steps with f(x+vt)f(x + vt) give a shape sliding to the left and the identical relation, since vv enters only as v2v^2.

Integration

The integral as accumulated area

A definite integral measures the net area between a curve and the xx axis. We approximate that area with a Riemann sum of nn rectangles and let nn\to\infty:

The plot shows the rectangles filling the area under f(x)=x2f(x)=x^2 on [0,2][0,2] as nn grows from coarse to fine. The exact area is 02x2dx=8/32.667\int_0^2 x^2\,dx = 8/3 \approx 2.667.

Source
import numpy as np
import matplotlib.pyplot as plt

f = lambda x: x**2
a, b = 0, 2
xs = np.linspace(a, b, 400)

fig, axes = plt.subplots(1, 3, figsize=(11, 3.6), sharey=True)
for ax, n in zip(axes, [4, 8, 32]):
    edges = np.linspace(a, b, n + 1)
    mids = 0.5 * (edges[:-1] + edges[1:])   # midpoint rule
    dx = (b - a) / n
    approx = np.sum(f(mids) * dx)
    ax.bar(mids, f(mids), width=dx, align='center',
           color='#66a182', edgecolor='white', alpha=0.7)
    ax.plot(xs, f(xs), 'k', lw=2)
    ax.set_title(f'n = {n},  sum = {approx:.3f}', fontsize=10)
    ax.set_xlabel('x')
axes[0].set_ylabel('f(x)')
plt.suptitle('Fig.7 Riemann sums approaching the exact area 8/3 = 2.667 as n grows', y=1.04)
plt.tight_layout()
plt.show()
<Figure size 1100x360 with 3 Axes>

We can watch that convergence numerically:

import numpy as np

f = lambda x: x**2
a, b = 0.0, 2.0
for n in [4, 16, 64, 256, 1024]:
    x = np.linspace(a, b, n + 1)
    mids = 0.5 * (x[:-1] + x[1:])
    approx = np.sum(f(mids) * (b - a) / n)
    print(f"n = {n:5d}   Riemann sum = {approx:.5f}")

exact = 8 / 3
print(f"\nexact value  = {exact:.5f}")
n =     4   Riemann sum = 2.62500
n =    16   Riemann sum = 2.66406
n =    64   Riemann sum = 2.66650
n =   256   Riemann sum = 2.66666
n =  1024   Riemann sum = 2.66667

exact value  = 2.66667

Antiderivatives and the fundamental theorem

An antiderivative FF of ff satisfies F(x)=f(x)F'(x)=f(x). Because the derivative of a constant is zero, antiderivatives come as a family F(x)+CF(x)+C. The fundamental theorem of calculus ties the two halves of the subject together:

This is why we rarely compute Riemann sums by hand: finding an antiderivative turns an infinite sum into a single subtraction.

Table of indefinite integrals

Function f(x)f(x)Antiderivative F(x)F(x)Function f(x)f(x)Antiderivative F(x)F(x)
xn (n1)x^n\ (n\neq -1)xn+1n+1+C\dfrac{x^{n+1}}{n+1} + C1x\dfrac{1}{x}lnx+C\ln\lvert x \rvert + C
exe^{x}ex+Ce^{x} + Ceaxe^{ax}1aeax+C\dfrac{1}{a}e^{ax} + C
sinx\sin xcosx+C-\cos x + Ccosx\cos xsinx+C\sin x + C
1x2+a2\dfrac{1}{x^2 + a^2}1aarctan ⁣(xa)+C\dfrac{1}{a}\arctan\!\left(\dfrac{x}{a}\right) + C1a2x2\dfrac{1}{\sqrt{a^2-x^2}}arcsin ⁣(xa)+C\arcsin\!\left(\dfrac{x}{a}\right) + C

Useful properties: reversing the limits flips the sign, aafdx=0\int_a^a f\,dx = 0, and integrals are linear, (cf±g)dx=cfdx±gdx\int (cf \pm g)\,dx = c\int f\,dx \pm \int g\,dx. For symmetric limits, an even function gives aafdx=20afdx\int_{-a}^{a} f\,dx = 2\int_0^a f\,dx while an odd function gives aafdx=0\int_{-a}^{a} f\,dx = 0, a shortcut that kills many quantum-mechanical integrals on sight.

Techniques of integration

Integration by parts is the reverse of the product rule, and it is the standard tool for integrals like xexdx\int x\,e^{-x}\,dx or xsinxdx\int x\sin x\,dx that appear constantly in expectation-value calculations.

For products of sines and cosines, the power-reduction and product-to-sum identities do the work:

sin2x=12(1cos2x),cos2x=12(1+cos2x),\sin^2 x = \tfrac{1}{2}(1-\cos 2x), \qquad \cos^2 x = \tfrac{1}{2}(1+\cos 2x),
sinAsinB=12[cos(AB)cos(A+B)],cosAcosB=12[cos(AB)+cos(A+B)].\sin A\sin B = \tfrac{1}{2}\big[\cos(A-B)-\cos(A+B)\big], \qquad \cos A\cos B = \tfrac{1}{2}\big[\cos(A-B)+\cos(A+B)\big].

These are exactly the integrals that enforce orthogonality of particle-in-a-box wavefunctions.

Problems

Problem 1: Differentiate a Gaussian

Differentiate f(x)=eαx2f(x) = e^{-\alpha x^2} with respect to xx.

Problem 2: Product and chain together

Differentiate g(x)=xsin(kx)g(x) = x\sin(kx).

Problem 3: A normalization integral

Evaluate 0Lsin2 ⁣(πxL)dx\displaystyle\int_0^{L} \sin^2\!\left(\frac{\pi x}{L}\right)dx.

Problem 4: Integration by parts

Evaluate 0xexdx\displaystyle\int_0^{\infty} x\,e^{-x}\,dx.

Problem 5: Symmetry shortcut

Without computing an antiderivative, evaluate aaxex2dx\displaystyle\int_{-a}^{a} x\,e^{-x^2}\,dx.

Problem 6: Mixed partial derivatives

For f(x,y)=x3y2+exyf(x,y) = x^3 y^2 + e^{xy} find f/x\partial f/\partial x and f/y\partial f/\partial y, then verify that the two mixed second derivatives agree.

Problem 7: Verify a Riemann sum numerically

Modify the Riemann-sum code above to estimate 0πsinxdx\int_0^{\pi}\sin x\,dx and compare with the exact value 2.

Problem 8: Chain rule on a plane wave

Differentiate ψ(x)=eikx\psi(x) = e^{ikx} with respect to xx, and again to find ψ(x)\psi''(x).

Problem 9: A Gamma-function integral

Evaluate 0x2exdx\displaystyle\int_0^{\infty} x^2\,e^{-x}\,dx using integration by parts twice.

Problem 10: A standing shape

Show that u(x,t)=sin(kx)cos(ωt)u(x,t) = \sin(kx)\cos(\omega t) satisfies 2ux2=k2ω22ut2\dfrac{\partial^2 u}{\partial x^2} = \dfrac{k^2}{\omega^2}\,\dfrac{\partial^2 u}{\partial t^2}. Compare with the relation obeyed by the sliding shape f(xvt)f(x - vt) and read off the speed vv.