TExcelTAD
Home › Calculus in Depth › Graphing with Calculus and Computers

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 ff 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

f(x)=x6−0.003x2+0.01f(x) = x^6 - 0.003x^2 + 0.01

On [−2,2][-2, 2] it looks like one smooth bowl. But f′(x)=6x5−0.006x=6x(x4−0.001)f'(x) = 6x^5 - 0.006x = 6x(x^4 - 0.001) has three zeros: x=0x = 0 and x=±0.0011/4≈±0.1778x = \pm 0.001^{1/4} \approx \pm 0.1778. So there is a small local maximum at 0 between two minima. Calculus finds the feature; the computer then zooms to it:

python · try it yourself
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 [−0.3,0.3][-0.3, 0.3]. Always compute the critical points before trusting a picture.

Roots that have no formula

For most functions f′(x)=0f'(x) = 0 cannot be solved exactly. Then the strategy is: plot f′f' to locate sign changes, then refine each with a bracketing solver (bisection, Brent) or Newton's method.

python · try it yourself
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

fc(x)=x4−cx2+xf_c(x) = x^4 - c x^2 + x

Its derivative fc′(x)=4x3−2cx+1f_c'(x) = 4x^3 - 2cx + 1 is a cubic, so fcf_c has either one or three critical numbers. A cubic ax3+px+qax^3 + px + q has three distinct real roots exactly when its discriminant −4ap3−27a2q2-4ap^3 - 27a^2q^2 is positive (for a=1a = 1 this is the familiar −4p3−27q2-4p^3 - 27q^2). With a=4a = 4, p=−2cp = -2c, q=1q = 1:

Δ=−4⋅4⋅(−2c)3−27⋅16⋅12=128c3−432>0  ⟺  c3>278  ⟺  c>32\Delta = -4 \cdot 4 \cdot (-2c)^3 - 27 \cdot 16 \cdot 1^2 = 128c^3 - 432 > 0 \quad \iff \quad c^3 > \frac{27}{8} \quad \iff \quad c > \frac{3}{2}

So for c<3/2c < 3/2 the curve has a single minimum, and for c>3/2c > 3/2 it develops a second dip (two minima and a maximum). SymPy confirms the threshold, and a plot of several members shows the transition:

python · try it yourself
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 c=3/2c = 3/2 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 brentq require 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 f(x)=x5−5x+1f(x) = x^5 - 5x + 1 have? Which are maxima, which minima?

Answer

Two: local maximum at x=−1x = -1, local minimum at x=1x = 1.

Solution

f′=5x4−5=5(x2−1)(x2+1)f' = 5x^4 - 5 = 5(x^2 - 1)(x^2 + 1), zero at x=±1x = \pm 1. f′′=20x3f'' = 20x^3 is −20 at −1 (max) and 20 at 1 (min).

Exercise 2: Find a hidden critical point numerically

Find all critical points of f(x)=ex−3x2f(x) = e^{x} - 3x^2 on [−1,5][-1, 5] with a sign-change scan and brentq.

Answer

x≈0.204x \approx 0.204 (local max) and x≈2.833x \approx 2.833 (local min).

Solution

f′(x)=ex−6xf'(x) = e^{x} - 6x, f′′(x)=ex−6f''(x) = e^{x} - 6.

python · try it yourself
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 kk does gk(x)=x3−kxg_k(x) = x^3 - kx have two critical numbers?

Answer

For k>0k > 0.

Solution

g′=3x2−k=0⇒x2=k/3g' = 3x^2 - k = 0 \Rightarrow x^2 = k/3, which has two real solutions exactly when k>0k > 0 (one, a double root, at k=0k = 0; none for k<0k < 0).

Exercise 4: Verify the bifurcation value

Check numerically that fc′(x)=4x3−2cx+1f_c'(x) = 4x^3 - 2cx + 1 has a double root at c=3/2c = 3/2. Where is it?

Answer

At x=1/2x = 1/2: f′(1/2)=0f'(1/2) = 0 and f′′(1/2)=0f''(1/2) = 0.

Solution

A double root satisfies f′=0f' = 0 and (f′)′=12x2−2c=0(f')' = 12x^2 - 2c = 0. With c=3/2c = 3/2: 12x2=3⇒x=±1/212x^2 = 3 \Rightarrow x = \pm 1/2, and 4(1/8)−3(1/2)+1=0.5−1.5+1=04(1/8) - 3(1/2) + 1 = 0.5 - 1.5 + 1 = 0 for x=1/2x = 1/2.

python · try it yourself
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 x6−0.003x2+0.01x^6 - 0.003x^2 + 0.01 on [−2,2][-2, 2] hide a local maximum?

  1. The function is discontinuous
  2. The dip is far smaller than the vertical range of the window
  3. Plotters cannot draw polynomials
  4. The maximum is at an endpoint

What does a positive discriminant of the cubic f′f' tell you about ff?

  1. f′f' has three distinct real roots, so ff has three critical numbers
  2. ff has no critical numbers
  3. ff is increasing everywhere
  4. ff has an inflection point at 0

Why does SciPy's brentq need a bracket [a,b][a, b] with f(a)⋅f(b)<0f(a) \cdot f(b) < 0?

  1. For speed only
  2. To compute the derivative
  3. The sign change guarantees a root inside by the Intermediate Value Theorem
  4. It needs exactly two roots