Graphing with Calculus and Computers
Computers plot faster than any person, but they only show what you ask them to show. Calculus tells you which window to look at, and the computer handles the algebra that is too messy by hand: critical points of high-degree polynomials, roots that have no formula, and whole families of curves that depend on a parameter. This lesson combines the two.
Why a default plot can lie
A plotter samples at a few hundred points and joins them with straight lines. Features narrower than the spacing, or too small for the vertical scale, vanish. Consider
On it looks like one smooth bowl. But has three zeros: and . So there is a small local maximum at 0 between two minima. Calculus finds the feature; the computer then zooms to it:
import numpy as np
import sympy as sp
x = sp.symbols('x', real=True)
f = x**6 - sp.Rational(3, 1000) * x**2 + sp.Rational(1, 100)
crit = sorted(float(c) for c in sp.solve(sp.diff(f, x), x) if c.is_real)
print("critical numbers:", [round(c, 4) for c in crit])
for c in crit:
print(f" f({c:+.4f}) = {float(f.subs(x, c)):.6f}")
# how big is the dip compared with the plot's vertical range on [-2, 2]?
fn = sp.lambdify(x, f)
xs = np.linspace(-2, 2, 401)
print("range on [-2, 2]:", round(float(fn(xs).max() - fn(xs).min()), 3), " depth of the dip:", round(float(fn(0) - fn(crit[0])), 6))critical numbers: [-0.1778, 0.0, 0.1778]
f(-0.1778) = 0.009937
f(+0.0000) = 0.010000
f(+0.1778) = 0.009937
range on [-2, 2]: 63.988 depth of the dip: 6.3e-05
The dip is about a millionth of the visible range: invisible in the full view, obvious in a window like . Always compute the critical points before trusting a picture.
Roots that have no formula
For most functions cannot be solved exactly. Then the strategy is: plot to locate sign changes, then refine each with a bracketing solver (bisection, Brent) or Newton's method.
import numpy as np
from scipy.optimize import brentq
f = lambda x: np.sin(3 * x) + 0.5 * x**2 - x
df = lambda x: 3 * np.cos(3 * x) + x - 1
xs = np.linspace(-2, 3, 501)
d = df(xs)
brackets = [(xs[i], xs[i + 1]) for i in range(len(xs) - 1) if d[i] * d[i + 1] < 0]
for a, b in brackets:
c = brentq(df, a, b)
kind = "min" if 3 * -3 * np.sin(3 * c) + 1 > 0 else "max" # f''(c) = −9 sin 3c + 1
print(f"critical point x = {c:+.6f} f = {f(c):+.6f} ({kind})")critical point x = -0.366015 f = -0.457320 (min)
critical point x = +0.463686 f = +0.627707 (max)
critical point x = +1.513463 f = -1.353422 (min)
critical point x = +2.837772 f = +1.979103 (max)
Sign changes on a grid can miss two roots that sit closer together than the spacing. A finer grid, or interval arithmetic in serious software, closes that gap.
Families of curves
Many models have a parameter, and the interesting question is how the shape changes with it. Take
Its derivative is a cubic, so has either one or three critical numbers. A cubic has three distinct real roots exactly when its discriminant is positive (for this is the familiar ). With , , :
So for the curve has a single minimum, and for it develops a second dip (two minima and a maximum). SymPy confirms the threshold, and a plot of several members shows the transition:
import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
x, c = sp.symbols('x c', real=True)
d = sp.discriminant(4*x**3 - 2*c*x + 1, x)
print("discriminant:", d, " zero at c =", sp.solve(d, c))
xs = np.linspace(-1.9, 1.7, 400)
plt.figure(figsize=(6, 4))
for cv in [0, 1, 1.4, 1.6, 3]:
plt.plot(xs, xs**4 - cv * xs**2 + xs, label=f"c = {cv}")
n = sum(1 for r in np.roots([4, 0, -2 * cv, 1]) if abs(r.imag) < 1e-7)
print(f"c = {cv}: {n} real critical number(s)")
plt.ylim(-3, 4); plt.legend(); plt.grid(alpha=0.3)discriminant: 128*c**3 - 432 zero at c = [3/2]
c = 0: 1 real critical number(s)
c = 1: 1 real critical number(s)
c = 1.4: 1 real critical number(s)
c = 1.6: 3 real critical number(s)
c = 3: 3 real critical number(s)
At exactly two of the critical numbers merge into a double root, a horizontal inflection point: the boundary between the two shapes. This merging is called a bifurcation, and it is the same mathematics that decides when a physical system flips between one stable state and two.
In software
- Adaptive plotting. Good plotters (Mathematica, SymPy's adaptive mode, Desmos) sample more densely where the curve bends, measured by how far the midpoint deviates from the chord, a discrete second derivative. Fixed-step plotters alias fast oscillations into fake patterns.
- Parameter sweeps. Hyperparameter sweeps in ML, design-space exploration in engineering and "what if" sliders in dashboards are families of curves. Knowing where the qualitative behaviour changes (the bifurcation values) tells you where to sample densely.
- Robust root bracketing. Libraries such as SciPy's
brentqrequire a sign change in a bracket because it guarantees a root (Intermediate Value Theorem), unlike Newton, which may wander off.
Exercises
Exercise 1: Count the critical points
How many critical numbers does have? Which are maxima, which minima?
Answer
Two: local maximum at , local minimum at .
Solution
, zero at . is −20 at −1 (max) and 20 at 1 (min).
Exercise 2: Find a hidden critical point numerically
Find all critical points of on with a sign-change scan and brentq.
Answer
(local max) and (local min).
Solution
, .
import numpy as np
from scipy.optimize import brentq
df = lambda x: np.exp(x) - 6 * x
xs = np.linspace(-1, 5, 601)
for a, b in zip(xs[:-1], xs[1:]):
if df(a) * df(b) < 0:
r = brentq(df, a, b)
print(round(r, 4), "max" if np.exp(r) - 6 < 0 else "min")0.2045 max
2.8331 min
Exercise 3: A family threshold
For which does have two critical numbers?
Answer
For .
Solution
, which has two real solutions exactly when (one, a double root, at ; none for ).
Exercise 4: Verify the bifurcation value
Check numerically that has a double root at . Where is it?
Answer
At : and .
Solution
A double root satisfies and . With : , and for .
c = 1.5
for x in (0.5, -0.5):
print(x, 4 * x**3 - 2 * c * x + 1, 12 * x**2 - 2 * c)0.5 0.0 0.0
-0.5 2.0 0.0
Test yourself
Why can a default plot of on hide a local maximum?
- The function is discontinuous
- The dip is far smaller than the vertical range of the window
- Plotters cannot draw polynomials
- The maximum is at an endpoint
What does a positive discriminant of the cubic tell you about ?
- has three distinct real roots, so has three critical numbers
- has no critical numbers
- is increasing everywhere
- has an inflection point at 0
Why does SciPy's brentq need a bracket with ?
- For speed only
- To compute the derivative
- The sign change guarantees a root inside by the Intermediate Value Theorem
- It needs exactly two roots