Volume 1: Foundations to Precalculus · Part II: Algebra

Chapter 8. Quadratics

Chapter map: the quadratic is the first equation in this book that cannot be solved by "undo each operation in turn", and the first function whose graph bends. It is also the best-studied object in elementary algebra, which makes it the ideal place to practise the habits this book cares about: seeing one object as words, symbols, a picture and a computation; knowing several methods and choosing between them; deriving the famous formula rather than memorising it; and knowing exactly when the formula, run on a real computer, gives the wrong answer. Quadratics are also the first non-linear model you meet in practice: anything accelerating, the area of a shape with one adjustable length, any "price times quantity", and any sum of squared errors is one.

Where this chapter sits

You need: solving linear equations (Chapter 5); square roots and exponent laws (Chapter 3); expanding brackets, factoring trinomials, and the factor theorem (Chapter 7). Plotting points in the plane (Chapter 10) helps, but the essentials are recalled here.

This chapter unlocks: Chapter 11 (the quadratic is the model non-linear function, and its transformations are the template for all function transformations); Chapter 15 (what happens when the discriminant is negative); Chapter 18 (optimisation and Newton’s method, previewed here); Chapter 32 (floating-point error, of which the quadratic formula is the textbook case); and Chapter 26 (ray tracing, met here as an application).

8.1. Three problems that are secretly the same

8.1.1. A ball thrown upwards

You stand on a platform and throw a ball straight up. It leaves your hand 2 m above the ground, moving upwards at 15 m/s. When does it hit the ground? How high does it go?

If you did sixth-form mechanics, you met the constant-acceleration ("SUVAT") equations. The one that matters here is

\[s = ut + \tfrac12 a t^2 ,\]

where \(s\) is displacement, \(u\) initial velocity, \(a\) acceleration and \(t\) time. Measuring upwards, \(u = 15\) m/s and \(a = -9.8\) m/s² (gravity pulls down). Adding the 2 m starting height, the height after \(t\) seconds is

\[h(t) = 2 + 15t - 4.9t^2 .\]

"When does it hit the ground?" becomes: solve \(2 + 15t - 4.9t^2 = 0\). The unknown appears both squared and unsquared. You cannot "subtract 2, then divide by 15" as you would for a linear equation, because the \(t^2\) term refuses to go away. You need a new idea.

8.1.2. A fence against a wall

You have 100 m of fencing and want a rectangular pen against a long wall, so you only need to fence three sides. If the two sides perpendicular to the wall are \(x\) metres each, the side parallel to the wall is \(100 - 2x\), and the area is

\[A(x) = x(100 - 2x) = 100x - 2x^2 .\]

Which \(x\) gives the biggest pen? Again a squared term, and this time the question is not "where is it zero?" but "where is it largest?"

8.1.3. Breaking even

A small company finds that, selling \(q\) hundred units a month, its profit in thousands of pounds is

\[P(q) = -2q^2 + 120q - 1000 .\]

At low volume, fixed costs swamp revenue; at very high volume, it has to cut prices so far that it loses money again. For which volumes does it break even (\(P = 0\))? Which volume maximises profit?

8.1.4. What they share

All three are about an expression of the form

\[ax^2 + bx + c\]

and all three ask the same two questions: where is it zero? and where is it largest (or smallest)? By the end of this chapter you will answer both for any such expression, by several methods, and know which method to reach for. (Answers: the ball lands after about 3.19 s and peaks at about 13.48 m; the best pen is 25 m by 50 m; the company breaks even at 1,000 and 5,000 units and does best at 3,000.)

8.2. The vocabulary of quadratics

Before methods, words. Many of these will feel half-familiar; the point of this section is to make each one crisp.

Quadratic expression and quadratic equation

A quadratic expression is a polynomial of degree 2: \(ax^2 + bx + c\) with \(a \neq 0\). A quadratic equation is an equation that can be rearranged to

\[ax^2 + bx + c = 0, \qquad a \neq 0 ,\]

its standard form. The word comes from Latin quadratum, "a square", because the defining feature is the squared unknown \(x^2\). (Confusingly, quad usually means four; here it refers to the four sides of a square.)

Coefficients of a quadratic

In \(ax^2 + bx + c\): \(a\) is the leading (or quadratic) coefficient, \(b\) the linear coefficient, and \(c\) the constant term. In \(7x^2 - 3x + 4\), \(a = 7\), \(b = -3\), \(c = 4\). Always read the signs with the coefficients: in \(x^2 - 5x + 6\), \(b = -5\), not \(5\). The condition \(a \neq 0\) matters: if \(a = 0\) the equation is linear, not quadratic.

Root, zero, solution

A root or solution of \(ax^2 + bx + c = 0\) is a value of \(x\) that makes the equation true. The same number is called a zero of the function \(f(x) = ax^2 + bx + c\), because \(f\) takes the value zero there. On a graph, real roots are the x-intercepts: where the curve meets the \(x\)-axis. A quadratic has at most two roots.

Parabola

The graph of \(y = ax^2 + bx + c\) is a parabola: a symmetric U-shaped curve. It opens upwards (a "cup", \(\cup\)) if \(a > 0\) and downwards (a "cap", \(\cap\)) if \(a < 0\). From Greek parabolē, "a placing alongside"; the name comes from the ancient Greek classification of conic sections (Chapter 10).

Vertex

The vertex of a parabola is its turning point: the lowest point if \(a > 0\) (a minimum), the highest if \(a < 0\) (a maximum). For \(y = ax^2 + bx + c\) it is at \(x = -\dfrac{b}{2a}\). The \(y\)-value there is the minimum value or maximum value of the quadratic.

Axis of symmetry

The axis of symmetry is the vertical line through the vertex, \(x = -\dfrac{b}{2a}\). The parabola is its own mirror image in this line. Consequence: the two roots (when they exist) sit at equal distances either side of it, so the vertex is exactly halfway between the roots.

Discriminant

The discriminant of \(ax^2 + bx + c\) is the number

\[\Delta = b^2 - 4ac .\]

(\(\Delta\) is capital Greek delta; some books write \(D\).) It "discriminates" between the cases: \(\Delta > 0\), two distinct real roots; \(\Delta = 0\), one repeated real root; \(\Delta < 0\), no real roots. Section 8.7 explains why.

Completing the square

Completing the square means rewriting \(x^2 + px\) as \(\left(x + \tfrac{p}{2}\right)^2 - \left(\tfrac{p}{2}\right)^2\), so that the variable appears only once, inside a square. It is the key step in solving any quadratic and in finding its vertex (Section 8.5).

Here is the whole vocabulary on one picture:

Graph of y equals x squared minus 2x minus 3 with roots at minus 1 and 3, vertex at 1, minus 4, axis of symmetry x equals 1, and y-intercept at minus 3
Figure 53. Anatomy of a parabola

Read it carefully, because every label is a fact you can compute from the symbols:

  • \(a = 1 > 0\), so the parabola opens upwards and the vertex is a minimum.

  • The \(y\)-intercept is \(c = -3\) (set \(x = 0\)).

  • The roots are \(-1\) and \(3\), because \(x^2 - 2x - 3 = (x + 1)(x - 3)\).

  • The axis of symmetry is halfway between the roots, \(x = 1\), which agrees with \(-b/(2a) = -(-2)/2 = 1\).

  • The vertex is \((1, f(1)) = (1, 1 - 2 - 3) = (1, -4)\).

  • The discriminant is \((-2)^2 - 4(1)(-3) = 4 + 12 = 16 > 0\): two roots, as seen.

8.3. One object, four representations

The project specification behind this book singles out the quadratic as the example of something that is simultaneously an expression, a function, a curve, a set of roots, a computation and a model. Fluency means moving between these views without friction, so it is worth making each translation explicit before learning any solution method.

Four boxes, words, symbols, pictures and computation, joined by two-way arrows labelled model, graph, sample, implement and describe
Figure 54. Words, symbols, pictures and computation for the thrown ball

Words → symbols. "Thrown up at 15 m/s from 2 m" becomes \(h(t) = 2 + 15t - 4.9t^2\) via the SUVAT equation. Each coefficient has a physical meaning: \(c = 2\) is the starting height, \(b = 15\) the starting speed, \(a = -4.9 = -\tfrac12 g\) half the acceleration. Negative \(a\): the parabola opens downwards, so there is a maximum height. The words predicted the shape before you drew anything. Try it: Explore the thrown ball’s parabola.

Symbols → pictures. Different symbolic forms reveal different features of the graph. Standard form \(ax^2 + bx + c\) shows the \(y\)-intercept \(c\). Factored form \(a(x - r_1)(x - r_2)\) shows the roots. Vertex form \(a(x - h)^2 + k\) shows the vertex \((h, k)\) (Section 8.8). For the ball, vertex form is approximately \(h(t) = -4.9(t - 1.53)^2 + 13.48\): the peak at \(t \approx 1.53\) s, height \(\approx 13.48\) m, is visible without plotting a single point.

Pictures → words. "Symmetric about its peak" says: the ball takes as long to climb from the release height to the top as to fall back to that height. "Two intercepts, one of them at negative time" says: the equation has a root at negative \(t\) that the physics throws away (it is when the ball would have been at ground level, had its parabolic flight started earlier).

Symbols ↔ computation. A quadratic is a three-number object \((a, b, c)\) that a computer evaluates with two multiplications and two additions (Horner, Chapter 7). Going the other way, computation reveals a quadratic hiding in data: in a table of values at equally spaced inputs, the second differences are constant and equal to \(2a\) times the square of the spacing.

def f(x):
    return x**2 - 2*x - 3

xs = list(range(-1, 5))
ys = [f(x) for x in xs]
d1 = [b - a for a, b in zip(ys, ys[1:])]     # first differences
d2 = [b - a for a, b in zip(d1, d1[1:])]     # second differences
print("x :", xs)
print("y :", ys)
print("d1:", d1)
print("d2:", d2)
x : [-1, 0, 1, 2, 3, 4]
y : [0, -3, -4, -3, 0, 5]
d1: [-3, -1, 1, 3, 5]
d2: [2, 2, 2, 2]

Linear functions have constant first differences (constant slope, Chapter 5). Quadratics have constant second differences: the slope itself changes at a constant rate. That is exactly what constant acceleration means for the ball: velocity changes steadily, so position is quadratic. In Chapter 17 you will see this as "the second derivative of a quadratic is the constant \(2a\)".

Four views at once

Three sliders for \(a\), \(b\), \(c\). Four synchronised panels: the equation in standard, vertex and factored form (factored form greys out when there are no real roots); the parabola with vertex, axis and roots marked; a table of values with first and second differences; and a short sentence in words ("opens downward, maximum 13.48 at t = 1.53, two real roots"). Drag the graph’s vertex directly and watch the coefficients change.

8.4. Method 1: solving by factoring

8.4.1. The problem

Solve \(x^2 - 5x + 6 = 0\).

8.4.2. Intuition and why it works

If you can write the quadratic as a product of two linear factors, the zero-product property (Chapter 7) finishes the job: a product is zero exactly when one of its factors is zero. For a monic quadratic \(x^2 + bx + c\), expanding \((x + m)(x + n) = x^2 + (m + n)x + mn\) shows you need two numbers with sum \(b\) and product \(c\).

Example 47. Procedure — Solving a quadratic by factoring

Input: a quadratic equation.

Steps:

  1. Rearrange to standard form \(ax^2 + bx + c = 0\) (everything on one side, zero on the other).

  2. Remove any common factor.

  3. Factor: for \(a = 1\), find \(m, n\) with \(m + n = b\), \(mn = c\); for \(a \neq 1\), use the \(ac\) method (Chapter 7).

  4. Set each factor equal to zero and solve the resulting linear equations.

  5. Check each root in the original equation.

Output: the roots, or "does not factor over the integers" (then switch method).

8.4.3. Worked examples

(a) \(x^2 - 5x + 6 = 0\). Need sum \(-5\), product \(6\): \(-2\) and \(-3\).

\[\begin{aligned} x^2 - 5x + 6 &= 0\\ (x - 2)(x - 3) &= 0 &&\text{(}(-2)+(-3) = -5,\ (-2)(-3) = 6\text{)}\\ x - 2 = 0 \quad&\text{or}\quad x - 3 = 0 &&\text{(zero-product property)}\\ x = 2 \quad&\text{or}\quad x = 3 . \end{aligned}\]

Check: \(4 - 10 + 6 = 0\) ✓, \(9 - 15 + 6 = 0\) ✓.

(b) \(2x^2 + x = 6\).

\[\begin{aligned} 2x^2 + x - 6 &= 0 &&\text{(standard form)}\\ 2x^2 + 4x - 3x - 6 &= 0 &&\text{(}ac = -12 = 4 \cdot (-3),\ 4 + (-3) = 1\text{)}\\ 2x(x + 2) - 3(x + 2) &= 0 &&\text{(group)}\\ (2x - 3)(x + 2) &= 0\\ x = \tfrac32 \quad&\text{or}\quad x = -2 . \end{aligned}\]

(c) The break-even problem: \(-2q^2 + 120q - 1000 = 0\).

\[\begin{aligned} -2(q^2 - 60q + 500) &= 0 &&\text{(common factor } -2\text{)}\\ q^2 - 60q + 500 &= 0 &&\text{(divide by } -2\text{)}\\ (q - 10)(q - 50) &= 0 &&\text{(sum } -60\text{, product } 500\text{)}\\ q = 10 \quad&\text{or}\quad q = 50 . \end{aligned}\]

The company breaks even at 1,000 and 5,000 units (\(q\) counts hundreds).

(d) \(x^2 = 7x\). Do not divide by \(x\):

\[x^2 - 7x = 0 \;\Longrightarrow\; x(x - 7) = 0 \;\Longrightarrow\; x = 0 \text{ or } x = 7 .\]
Factoring pitfalls
  • Zero on one side first. \((x - 1)(x - 2) = 6\) does not give \(x - 1 = 6\) or \(x - 2 = 6\). Expand, rearrange to \(x^2 - 3x - 4 = 0\), then factor: \((x - 4)(x + 1) = 0\).

  • Dividing by the variable loses the root \(x = 0\), as in example (d).

  • Most quadratics do not factor over the integers. \(x^2 - 2x - 1 = 0\) has roots \(1 \pm \sqrt 2\); no amount of hunting for integer pairs will find them. Factoring is a fast path, not a general method.

When to choose it. When the coefficients are small integers and you can see the factor pair within a few seconds. A quick test: a quadratic with integer coefficients factors over the integers exactly when its discriminant \(b^2 - 4ac\) is a perfect square (\(0, 1, 4, 9, 16, \ldots\)). For \(x^2 - 5x + 6\), \(\Delta = 25 - 24 = 1 = 1^2\): factor it.

8.5. Method 2: completing the square

8.5.1. The problem

Solve \(x^2 + 6x - 7 = 0\), but pretend you cannot see the factorisation. More to the point: find a method that works for every quadratic, including \(x^2 - 2x - 1 = 0\), which does not factor nicely.

8.5.2. Intuition: equations that are easy

Some quadratics are trivially easy. \(x^2 = 16\) gives \(x = \pm 4\). Equally easy is

\[(x + 3)^2 = 16 \;\Longrightarrow\; x + 3 = \pm 4 \;\Longrightarrow\; x = 1 \text{ or } x = -7 .\]

What makes these easy is that \(x\) appears only once, inside a square, so you can undo the square with a square root and then undo the addition. The general quadratic \(x^2 + 6x - 7\) has \(x\) in two places (\(x^2\) and \(6x\)). Completing the square merges those two appearances into one.

8.5.3. Why it works: literally completing a square

Think of \(x^2 + 6x\) as an area: an \(x\)-by-\(x\) square plus a rectangle \(6\) by \(x\). Cut the rectangle into two halves, each \(3\) by \(x\), and attach one to the right side of the square and the other to the top. You now have an almost-square shape of side \(x + 3\), missing a \(3\)-by-\(3\) corner.

On the left, an x by x square and a 6 by x strip. On the right, the strip has been split into two 3 by x strips placed on two sides of the square, leaving a 3 by 3 corner missing to make a square of side x plus 3
Figure 55. Completing the square: the missing corner has area \((b/2)^2\)

Add that corner, area \(3^2 = 9\), and you have completed the square:

\[x^2 + 6x + 9 = (x + 3)^2 .\]

In general, half the coefficient of \(x\), squared, is the missing corner:

\[x^2 + px + \left(\frac{p}{2}\right)^2 = \left(x + \frac{p}{2}\right)^2 .\]

(Expand the right side to check: \(x^2 + 2\cdot\tfrac{p}{2}x + \tfrac{p^2}{4} = x^2 + px + \tfrac{p^2}{4}\).) This is the perfect-square identity \((a + b)^2 = a^2 + 2ab + b^2\) from Chapter 7, read backwards.

To keep an equation balanced, whatever you add to one side you must add to the other.

Example 48. Procedure — Solving \(ax^2 + bx + c = 0\) by completing the square

Input: \(a, b, c\) with \(a \neq 0\).

Steps:

  1. If \(a \neq 1\), divide every term by \(a\). Now the equation is \(x^2 + px + q = 0\).

  2. Move the constant to the right: \(x^2 + px = -q\).

  3. Add \((p/2)^2\) to both sides.

  4. Rewrite the left side as \((x + p/2)^2\); simplify the right side to a single number \(R\).

  5. If \(R < 0\): no real solutions (a square cannot be negative); stop.

  6. Take square roots of both sides, remembering \(\pm\): \(x + p/2 = \pm\sqrt{R}\).

  7. Subtract \(p/2\): \(x = -p/2 \pm \sqrt{R}\).

Output: the real roots (two if \(R > 0\), one if \(R = 0\)).

8.5.4. Worked example 1: \(x^2 + 6x - 7 = 0\)

\[\begin{aligned} x^2 + 6x - 7 &= 0\\ x^2 + 6x &= 7 &&\text{(add 7 to both sides)}\\ x^2 + 6x + 9 &= 7 + 9 &&\text{(add } (6/2)^2 = 9 \text{ to both sides)}\\ (x + 3)^2 &= 16 &&\text{(left side is a perfect square)}\\ x + 3 &= \pm 4 &&\text{(square root, both signs)}\\ x &= -3 \pm 4 &&\text{(subtract 3)}\\ x = 1 \quad&\text{or}\quad x = -7 . \end{aligned}\]

Cross-check by factoring: \(x^2 + 6x - 7 = (x + 7)(x - 1)\) ✓.

8.5.5. Worked example 2: leading coefficient not 1, irrational roots

Solve \(2x^2 - 8x + 3 = 0\).

\[\begin{aligned} 2x^2 - 8x + 3 &= 0\\ x^2 - 4x + \tfrac32 &= 0 &&\text{(divide every term by } a = 2\text{)}\\ x^2 - 4x &= -\tfrac32 &&\text{(constant to the right)}\\ x^2 - 4x + 4 &= -\tfrac32 + 4 &&\text{(add } (-4/2)^2 = 4\text{)}\\ (x - 2)^2 &= \tfrac52 &&\\ x - 2 &= \pm\sqrt{\tfrac52} = \pm\frac{\sqrt{10}}{2} &&\text{(}\sqrt{5/2} = \sqrt{10}/\sqrt{4}\text{)}\\ x &= 2 \pm \frac{\sqrt{10}}{2} . \end{aligned}\]

Numerically \(\sqrt{10}/2 \approx 1.5811\), so \(x \approx 3.5811\) or \(x \approx 0.4189\). Check the first: \(2(3.5811)^2 - 8(3.5811) + 3 \approx 25.649 - 28.649 + 3 = 0.000\) ✓.

8.5.6. Worked example 3: no real roots

Solve \(x^2 - 2x + 3 = 0\). Try it: See the roots 1 ± i√2.

\[\begin{aligned} x^2 - 2x &= -3\\ x^2 - 2x + 1 &= -3 + 1 &&\text{(add } (-2/2)^2 = 1\text{)}\\ (x - 1)^2 &= -2 . \end{aligned}\]

A real number squared is never negative, so there are no real solutions. Geometrically, the parabola \(y = (x - 1)^2 + 2\) has its lowest point at height 2 and never reaches the axis. (In Chapter 15 you will allow \(\sqrt{-2} = i\sqrt 2\), and this equation gets the two roots \(1 \pm i\sqrt{2}\).)

8.5.7. Where you’d use this

Completing the square turns "find the best value" into "read off the vertex", because \(a(x - h)^2 + k\) is smallest (for \(a > 0\)) when the square is zero.

  • Statistics: why the mean? Given data \(2, 4, 9\), which single number \(c\) is "closest" to all of them, in the sense of making the sum of squared errors \(S(c) = (c - 2)^2 + (c - 4)^2 + (c - 9)^2\) smallest? Expanding, \(S(c) = 3c^2 - 30c + 101 = 3(c - 5)^2 + 26\). The minimum is at \(c = 5\), which is exactly the mean, with \(S = 26\) (try \(c = 4\): \(S = 29\)). The same algebra with \(n\) data points shows that the mean always minimises squared error, which is why least-squares fitting (Chapter 31) and the "mean squared error" loss in machine learning lead back to averages.

  • Optimisation by hand. The fence and café-revenue problems in Section 8.14 are vertex-reading problems; completing the square is how you find a maximum without calculus.

Completing-the-square slips
  • Add to both sides. Writing \(x^2 + 6x + 9 = 7\) (forgetting to add 9 on the right) is the classic error.

  • Divide by \(a\) first. The "half of \(p\), squared" rule only works when the coefficient of \(x^2\) is 1. With \(2x^2 - 8x\), half of \(-8\) squared is 16, which is wrong; dividing by 2 first gives the correct 4.

  • The \(\pm\). \((x + 3)^2 = 16\) has two solutions. \(\sqrt{16} = 4\) (the square-root symbol always means the non-negative root), but the equation \(y^2 = 16\) has solutions \(y = \pm 4\).

Why bother, when there is a formula? Three reasons. Completing the square is how the formula is derived (next section). It produces vertex form, which tells you the maximum or minimum of a quadratic (Section 8.8). And it generalises: you will complete the square again for circles (Chapter 10), in integrals (Chapter 19), and for the Gaussian distribution (Chapter 30).

Al-Khwarizmi’s square

The geometric picture above is essentially the one in Muhammad ibn Musa al-Khwarizmi’s The Compendious Book on Calculation by Completion and Balancing (Baghdad, around 820 CE). His worked problem was "a square and ten roots are equal to thirty-nine", i.e. \(x^2 + 10x = 39\); he split the ten into strips of five, completed the square with \(25\), got \(64 = 8^2\), and so \(x = 3\). (He ignored the negative root \(-13\); negative numbers were not yet accepted as answers.) The "completion" in his title is al-jabr, from which we get the word algebra; his own name gives us algorithm. Babylonian scribes solved the same kind of problem on clay tablets around 1800 BCE, using recipes that amount to the same steps.

8.6. Method 3: the quadratic formula, derived

8.6.1. The problem

Completing the square works every time, but it means redoing the same steps for each new equation. As programmers know, when you find yourself repeating a procedure with different inputs, you write a function. The quadratic formula is completing the square done once, symbolically, for general \(a, b, c\).

8.6.2. Derivation, every step shown

Start from the general quadratic, with \(a \neq 0\):

\[\begin{aligned} ax^2 + bx + c &= 0 \\[4pt] x^2 + \frac{b}{a}x + \frac{c}{a} &= 0 &&\text{(divide every term by } a\text{; allowed since } a \neq 0\text{)}\\[4pt] x^2 + \frac{b}{a}x &= -\frac{c}{a} &&\text{(subtract } c/a \text{ from both sides)}\\[4pt] x^2 + \frac{b}{a}x + \left(\frac{b}{2a}\right)^2 &= \left(\frac{b}{2a}\right)^2 - \frac{c}{a} &&\text{(add half the } x\text{-coefficient, squared, to both sides)}\\[4pt] \left(x + \frac{b}{2a}\right)^2 &= \frac{b^2}{4a^2} - \frac{c}{a} &&\text{(left side is now a perfect square)}\\[4pt] \left(x + \frac{b}{2a}\right)^2 &= \frac{b^2}{4a^2} - \frac{4ac}{4a^2} &&\text{(common denominator } 4a^2\text{: multiply } c/a \text{ by } 4a/4a\text{)}\\[4pt] \left(x + \frac{b}{2a}\right)^2 &= \frac{b^2 - 4ac}{4a^2} &&\text{(combine the fractions)}\\[4pt] x + \frac{b}{2a} &= \pm\sqrt{\frac{b^2 - 4ac}{4a^2}} &&\text{(square root of both sides; both signs; needs } b^2 - 4ac \ge 0\text{)}\\[4pt] x + \frac{b}{2a} &= \pm\frac{\sqrt{b^2 - 4ac}}{2a} &&\text{(}\sqrt{4a^2} = 2|a|\text{; the } \pm \text{ absorbs the sign of } a\text{)}\\[4pt] x &= -\frac{b}{2a} \pm \frac{\sqrt{b^2 - 4ac}}{2a} &&\text{(subtract } b/2a \text{ from both sides)}\\[4pt] x &= \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} . &&\text{(single fraction)} \end{aligned}\]

Two steps deserve a comment.

  • Where the discriminant appears. After completing the square, the right-hand side is \((b^2 - 4ac)/(4a^2)\). The denominator \(4a^2\) is always positive, so the sign of the right-hand side is the sign of \(b^2 - 4ac\). A square cannot equal a negative number, so if \(b^2 - 4ac < 0\) the derivation stops: no real roots. The discriminant is not an arbitrary extra rule; it is the quantity whose square root you need.

  • The \(|a|\) subtlety. \(\sqrt{4a^2} = 2|a|\), not \(2a\) (if \(a = -3\), \(\sqrt{36} = 6 = 2|a|\)). But since we are taking \(\pm\) anyway, \(\pm\frac{\sqrt{\Delta}}{2|a|}\) and \(\pm\frac{\sqrt{\Delta}}{2a}\) describe the same pair of numbers.

Quadratic formula

The solutions of \(ax^2 + bx + c = 0\) (\(a \neq 0\)) are

\[x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a} .\]

Read it as "the axis of symmetry \(-b/(2a)\), plus or minus a half-width \(\sqrt{\Delta}/(2a)\)". The two roots are symmetric about the vertex.

That reading is worth pausing on. The formula says the roots are

\[x = \underbrace{-\frac{b}{2a}}_{\text{axis of symmetry}} \;\pm\; \underbrace{\frac{\sqrt{b^2 - 4ac}}{2a}}_{\text{distance from axis to each root}} ,\]

which is exactly what the picture of the parabola shows: two roots, mirror images about the vertical line through the vertex.

Example 49. Procedure — Solving with the quadratic formula

Input: a quadratic equation.

Steps:

  1. Rearrange to \(ax^2 + bx + c = 0\) and write down \(a\), \(b\), \(c\) with their signs.

  2. Compute the discriminant \(\Delta = b^2 - 4ac\).

  3. If \(\Delta < 0\): no real roots (two complex roots, Chapter 15). Stop.

  4. If \(\Delta = 0\): one repeated root \(x = -b/(2a)\). Stop.

  5. Otherwise compute \(\sqrt{\Delta}\) and the two roots \(x = (-b \pm \sqrt\Delta)/(2a)\).

  6. Simplify surds if an exact answer is wanted; otherwise round only at the end.

  7. Check (substitute, or use Vieta’s formulas, Section 8.12).

Output: zero, one or two real roots.

8.6.3. Worked example: \(3x^2 - 5x - 1 = 0\)

\(a = 3\), \(b = -5\), \(c = -1\).

\[\begin{aligned} \Delta &= b^2 - 4ac = (-5)^2 - 4(3)(-1) = 25 + 12 = 37 &&\text{(} > 0\text{: two real roots)}\\ x &= \frac{-(-5) \pm \sqrt{37}}{2 \cdot 3} &&\text{(substitute; note } -b = +5\text{)}\\ &= \frac{5 \pm \sqrt{37}}{6} . \end{aligned}\]

With \(\sqrt{37} \approx 6.0828\): \(x \approx \frac{11.0828}{6} \approx 1.8471\) or \(x \approx \frac{-1.0828}{6} \approx -0.1805\).

Check with Vieta (Section 8.12): sum of roots should be \(-b/a = 5/3 \approx 1.6667\); \(1.8471 + (-0.1805) = 1.6666\) ✓. Product should be \(c/a = -1/3\); \(1.8471 \times (-0.1805) \approx -0.3334\) ✓.

8.6.4. Worked example: the ball lands

\(-4.9t^2 + 15t + 2 = 0\): \(a = -4.9\), \(b = 15\), \(c = 2\).

\[\begin{aligned} \Delta &= 15^2 - 4(-4.9)(2) = 225 + 39.2 = 264.2, \qquad \sqrt{\Delta} \approx 16.2542\\ t &= \frac{-15 \pm 16.2542}{2(-4.9)} = \frac{-15 \pm 16.2542}{-9.8}\\ t &\approx \frac{1.2542}{-9.8} \approx -0.128 \quad\text{or}\quad t \approx \frac{-31.2542}{-9.8} \approx 3.189 . \end{aligned}\]

The ball hits the ground after about \(3.19\) s. The negative root is discarded: the model only applies after release. (Discarding roots for physical reasons is normal and is part of the modelling step, not a defect of the mathematics.)

8.6.5. Where you’d use this

The formula is for the quadratics that do not factor nicely, which is nearly all of them in practice.

  • Road safety. A driver needs 0.7 s to react, then brakes at \(6\ \text{m/s}^2\). At speed \(u\) m/s the stopping distance is \(0.7u + \frac{u^2}{12}\) (thinking distance plus braking distance \(u^2/(2 \times 6)\)). What is the fastest speed from which the car can stop within 40 m? Multiply \(0.7u + \frac{u^2}{12} = 40\) by 12: \(u^2 + 8.4u - 480 = 0\), so \(u = \frac{-8.4 + \sqrt{8.4^2 + 1920}}{2} = \frac{-8.4 + 44.62}{2} \approx 18.1\) m/s, about 65 km/h. (The negative root is discarded.)

  • Finance: rate of return. You invest $1000 and receive $600 after one year and $600 after two. The yearly return \(r\) that makes this fair satisfies \(1000 = \frac{600}{g} + \frac{600}{g^2}\) with \(g = 1 + r\). Multiplying by \(g^2\): \(1000g^2 - 600g - 600 = 0\), so \(g = \frac{600 + \sqrt{600^2 + 4 \cdot 1000 \cdot 600}}{2000} \approx 1.1307\). The internal rate of return is about 13.1% a year. With more years of payments the equation becomes a higher-degree polynomial, and spreadsheets solve it numerically.

  • Computing. Every ray tracer and many collision detectors solve this formula millions of times per second (Section 8.15), which is why its numerical behaviour (Section 8.11) matters.

Quadratic-formula slips
  • Sign of \(b\). If \(b = -5\) then \(-b = 5\) and \(b^2 = 25\). Type \((-5)^2\), not \(-5^2\) (which calculators and Python read as \(-(5^2) = -25\)).

  • The fraction bar covers everything. It is \((-b \pm \sqrt\Delta)/(2a)\), not \(-b \pm \sqrt\Delta/(2a)\). In code, write (-b + d) / (2 * a); -b + d / 2 * a is wrong twice over.

  • Not in standard form. For \(x^2 = 3x + 4\), first rewrite as \(x^2 - 3x - 4 = 0\), so \(b = -3\), \(c = -4\).

  • On a computer, the formula itself can fail when \(b^2 \gg 4ac\); see Section 8.11.

Did anyone discover "the" formula?

No single person. The Indian mathematician Brahmagupta (628 CE) stated a rule in words equivalent to the formula for one root, and Sridhara (around 900 CE) gave a version that is close to the modern form. The compact symbolic formula needed symbolic algebra itself, which arrived with Viète (1590s) and Descartes (1637). Before that every rule was a recipe written in prose, which is one reason a "procedure" box is such a natural way to present it: that is how it was first written down.

US curriculum

Solving quadratics by factoring, completing the square and the quadratic formula is core Algebra I (grades 8–9; Common Core standard A-REI.B.4, which explicitly asks students to derive the formula by completing the square). Complex roots and the full discriminant analysis appear in Algebra II.

8.7. The discriminant: how many roots, before you find them

8.7.1. The problem

Sometimes you do not need the roots, only to know whether they exist. Does a ray hit a sphere (Section 8.15)? Does a ball thrown at a given speed ever reach a given height? Is a trinomial factorable over the integers? All three are answered by the sign (or the squareness) of one number.

8.7.2. Why it works

The derivation showed

\[\left(x + \frac{b}{2a}\right)^2 = \frac{b^2 - 4ac}{4a^2} .\]

The left side is a square, hence \(\ge 0\). So:

  • \(\Delta > 0\): the right side is positive and has two square roots, \(\pm\sqrt{\Delta}/(2a)\): two distinct real roots.

  • \(\Delta = 0\): the right side is zero, whose only square root is \(0\): one repeated root \(x = -b/(2a)\), a root of multiplicity 2 (Chapter 7). The quadratic is a perfect square \(a(x + b/2a)^2\).

  • \(\Delta < 0\): the right side is negative and no real number squares to it: no real roots (two complex-conjugate roots).

Graphically, the discriminant measures how far the vertex is from the \(x\)-axis, relative to how the parabola opens. The vertex height is \(k = -\Delta/(4a)\) (we prove this in Section 8.8). For an upward parabola (\(a > 0\)), \(\Delta > 0\) means \(k < 0\): the minimum is below the axis, so the curve must cross it twice.

Three upward parabolas with the same axis: one dips below the x-axis crossing it twice (discriminant 16), one just touches at x equals 1 (discriminant 0), and one stays above the axis (discriminant minus 8)
Figure 56. The sign of the discriminant decides how many times the parabola meets the axis

8.7.3. Worked example

How many real roots does each have?

Equation \(\Delta = b^2 - 4ac\) Conclusion

\(x^2 - 2x - 3 = 0\)

\(4 + 12 = 16\)

two; and \(16 = 4^2\), so it factors: \((x+1)(x-3)\)

\(4x^2 - 12x + 9 = 0\)

\(144 - 144 = 0\)

one repeated: \(x = 12/8 = 3/2\); \((2x - 3)^2\)

\(x^2 - 2x + 3 = 0\)

\(4 - 12 = -8\)

none (real)

\(x^2 - 2x - 1 = 0\)

\(4 + 4 = 8\)

two irrational: \(1 \pm \sqrt 2\)

Does the ball reach 15 m? Solve \(2 + 15t - 4.9t^2 = 15\), i.e. \(-4.9t^2 + 15t - 13 = 0\). \(\Delta = 225 - 4(-4.9)(-13) = 225 - 254.8 = -29.8 < 0\). No: it never gets that high (consistent with the peak of 13.48 m). Does it reach 10 m? \(-4.9t^2 + 15t - 8 = 0\) has \(\Delta = 225 - 156.8 = 68.2 > 0\): yes, twice, once going up and once coming down.

8.7.4. Where you’d use this

The discriminant answers yes/no questions ("does it ever happen?") without solving anything.

  • Everyday geometry. Can a rectangle with perimeter 20 m have area 30 m²? Sides \(x\) and \(10 - x\) give \(x(10 - x) = 30\), i.e. \(x^2 - 10x + 30 = 0\), with \(\Delta = 100 - 120 = -20 < 0\). No: the largest area with that perimeter is the 5 by 5 square, 25 m².

  • Engineering: springs and dampers. A car’s suspension, a door closer and a measuring instrument’s needle all behave like a mass \(m\) on a spring of stiffness \(k\) with damping \(c\). Their motion is governed by the roots of \(ms^2 + cs + k = 0\), and the sign of \(\Delta = c^2 - 4mk\) decides the character. \(\Delta < 0\): it overshoots and oscillates (underdamped). \(\Delta > 0\): it creeps back slowly (overdamped). \(\Delta = 0\): it returns as fast as possible without overshooting (critically damped). For \(m = 1\) kg and \(k = 100\) N/m, critical damping needs \(c^2 = 400\), i.e. \(c = 20\) N·s/m. Engineers tune door closers and instrument needles to sit near that value.

  • Computing. A ray misses a sphere exactly when the discriminant of its intersection quadratic is negative, so a renderer can reject most rays with one multiply-and-compare and never take a square root (Section 8.15).

Factorability test

A quadratic \(ax^2 + bx + c\) with integer coefficients factors into linear factors with integer (or rational) coefficients if and only if its discriminant is a perfect square. Reason: the roots \((-b \pm \sqrt\Delta)/(2a)\) are rational exactly when \(\sqrt\Delta\) is.

Discriminant explorer

Fix \(a\) and \(b\); slide \(c\). Watch the parabola rise, the two roots slide together, merge at \(\Delta = 0\), and vanish. A second panel plots the roots in the complex plane: as they leave the real axis they become a symmetric pair \(p \pm qi\) moving vertically, preparing the ground for Chapter 15.

8.8. Three forms, and the graph of a quadratic

8.8.1. The problem

Find the maximum height of the ball and when it occurs; find the best fence. These are questions about the vertex, not the roots. Each of the three standard ways of writing a quadratic answers a different question directly:

Form Formula Reveals

Standard

\(y = ax^2 + bx + c\)

\(y\)-intercept \(c\); coefficients for the formula and discriminant

Factored (intercept)

\(y = a(x - r_1)(x - r_2)\)

roots \(r_1, r_2\)

Vertex

\(y = a(x - h)^2 + k\)

vertex \((h, k)\), axis \(x = h\), max/min value \(k\)

In all three, \(a\) is the same number and has the same meaning: direction (sign) and steepness (size).

8.8.2. Transformations of \(y = x^2\)

Vertex form is easiest to understand as a sequence of moves applied to the basic parabola \(y = x^2\), whose vertex is the origin.

  • \(y = x^2 + k\): shift up by \(k\) (down if \(k < 0\)).

  • \(y = (x - h)^2\): shift right by \(h\). (The minus sign surprises people. The new curve reaches height 0 when \(x - h = 0\), i.e. at \(x = h\), so the vertex has moved to \(x = h\).)

  • \(y = ax^2\): stretch vertically by factor \(a\); \(|a| > 1\) is narrower, \(|a| < 1\) wider; \(a < 0\) also reflects in the \(x\)-axis.

Combining them: \(y = a(x - h)^2 + k\) is \(y = x^2\) stretched by \(a\), then moved so that its vertex is at \((h, k)\).

The parabola y equals x squared with four transformed copies: shifted right 2 and up 1, narrowed by factor 2, widened by factor one quarter, and flipped with vertex at minus 1, 3
Figure 57. Transformations of the basic parabola

These rules are not special to parabolas. Replacing \(x\) by \(x - h\) shifts any graph right by \(h\), and multiplying by \(a\) stretches any graph vertically. You will reuse this exact toolkit for every function family in Chapter 11.

8.8.3. Converting between forms

Standard → vertex: complete the square (keeping the equation as a function, so we add and subtract rather than adding to both sides).

Example 50. Procedure — Standard form to vertex form

Input: \(y = ax^2 + bx + c\).

Steps:

  1. Factor \(a\) out of the first two terms: \(y = a\left(x^2 + \frac{b}{a}x\right) + c\).

  2. Inside the bracket, add and subtract \(\left(\frac{b}{2a}\right)^2\).

  3. Write the first three terms inside as a square; move the subtracted term out of the bracket (multiplying it by \(a\)).

  4. Simplify the constant.

Output: \(y = a(x - h)^2 + k\) with \(h = -\dfrac{b}{2a}\) and \(k = c - \dfrac{b^2}{4a}\).

Worked example. Write \(y = 2x^2 - 12x + 13\) in vertex form.

\[\begin{aligned} y &= 2(x^2 - 6x) + 13 &&\text{(factor 2 from the } x \text{ terms)}\\ &= 2(x^2 - 6x + 9 - 9) + 13 &&\text{(add and subtract } (-6/2)^2 = 9\text{)}\\ &= 2\big((x - 3)^2 - 9\big) + 13 &&\text{(perfect square)}\\ &= 2(x - 3)^2 - 18 + 13 &&\text{(distribute the 2)}\\ &= 2(x - 3)^2 - 5 . \end{aligned}\]

Vertex \((3, -5)\), a minimum because \(a = 2 > 0\). The minimum value of \(2x^2 - 12x + 13\) is \(-5\), attained at \(x = 3\).

The general vertex. Doing the same symbolically:

\[\begin{aligned} ax^2 + bx + c &= a\left(x^2 + \frac{b}{a}x\right) + c\\ &= a\left(x^2 + \frac{b}{a}x + \frac{b^2}{4a^2} - \frac{b^2}{4a^2}\right) + c\\ &= a\left(x + \frac{b}{2a}\right)^2 - \frac{b^2}{4a} + c\\ &= a\left(x + \frac{b}{2a}\right)^2 - \frac{b^2 - 4ac}{4a} . \end{aligned}\]

So \(h = -b/(2a)\) and \(k = -\Delta/(4a)\), which is the fact we used in Section 8.7.

Vertex → standard: expand. \(2(x - 3)^2 - 5 = 2(x^2 - 6x + 9) - 5 = 2x^2 - 12x + 13\).

Standard → factored: find the roots. By any method; then \(ax^2 + bx + c = a(x - r_1)(x - r_2)\). For \(2x^2 - 12x + 13\): \(2(x - 3)^2 = 5\) gives \(x = 3 \pm \sqrt{5/2}\), so \(y = 2\left(x - 3 - \sqrt{5/2}\right)\left(x - 3 + \sqrt{5/2}\right)\). Do not forget the leading \(a\): the factored form of \(2x^2 - 12x + 13\) is not \((x - r_1)(x - r_2)\), which has leading coefficient 1.

Factored → vertex: average the roots. The vertex is midway between the roots: \(h = (r_1 + r_2)/2\); then \(k = f(h)\). For \(y = (x + 1)(x - 3)\): \(h = (-1 + 3)/2 = 1\), \(k = (2)(-2) = -4\). Vertex \((1, -4)\), matching the anatomy diagram.

8.8.4. Sketching a parabola by hand

Example 51. Procedure — Sketching \(y = ax^2 + bx + c\)

Steps:

  1. Sign of \(a\): cup (\(a > 0\)) or cap (\(a < 0\)).

  2. \(y\)-intercept \((0, c)\).

  3. Vertex \(h = -b/(2a)\), \(k = f(h)\); draw the axis \(x = h\).

  4. Discriminant: if \(\Delta \ge 0\), find the roots and mark them.

  5. Reflect the \(y\)-intercept in the axis to get a free extra point \((2h, c)\).

  6. Draw a smooth symmetric curve through the points.

For the ball: cap; intercept \((0, 2)\); vertex at \(t = -15/(2 \cdot (-4.9)) = 15/9.8 \approx 1.531\), \(h(1.531) = 2 + 225/19.6 \approx 13.48\); roots \(\approx -0.128\) and \(\approx 3.189\); mirror point \((3.061, 2)\).

Graph of height h of t equals 2 plus 15t minus 4.9 t squared from t equals 0 to about 3.19 seconds, peaking at about 13.48 metres at 1.53 seconds
Figure 58. Height of the thrown ball
US curriculum

Graphing quadratics, vertex form and transformations of \(y = x^2\) are Algebra I topics (Common Core F-IF.C.8a: "use the process of factoring and completing the square in a quadratic function to show zeros, extreme values, and symmetry of the graph"). General function transformations are developed in Algebra II and Precalculus.

8.9. Method 4 and 5: graphing and numerical root finding

8.9.1. Graphing

Plot \(y = ax^2 + bx + c\) and read off where it crosses the axis. On paper this gives one or two significant figures; with graphing software or a zoomable plot, more. Its real value is not precision but understanding: a graph shows at a glance how many roots there are, roughly where, and whether the answer from a formula is plausible. Graphing is also the natural method when the "quadratic" is buried in a more complicated equation, such as \(x^2 = 2^x\), where you might first want to know how many solutions to look for (there are three).

8.9.2. Numerical root finding

A numerical method does not produce a formula; it produces a sequence of better and better approximations. For a quadratic this is usually unnecessary, because the formula exists. But understanding the numerical approach here, where you can check the answer exactly, prepares you for equations that have no formula: polynomials of degree 5 and above, equations mixing polynomials with \(e^x\) or \(\sin x\), and the equations inside every physics engine.

Bisection is the simplest. If a continuous function is negative at one point and positive at another, it must cross zero somewhere between (the intermediate value theorem, Chapter 16). Repeatedly halve the interval, keeping the half where the sign change is.

Example 52. Procedure — Bisection

Input: continuous \(f\), an interval \(\lbrack lo, hi]\) with \(f(lo)\) and \(f(hi)\) of opposite signs, a tolerance \(\varepsilon\).

Steps:

  1. Compute the midpoint \(m = (lo + hi)/2\).

  2. If \(f(m)\) has the same sign as \(f(lo)\), set \(lo \leftarrow m\); otherwise set \(hi \leftarrow m\).

  3. Repeat until \(hi - lo < \varepsilon\).

Output: \(m\), within \(\varepsilon\) of a root. Each step halves the uncertainty, gaining one binary digit of accuracy.

def bisect(f, lo, hi, tol=1e-12):
    """Find a root of f in [lo, hi], assuming f(lo) and f(hi) differ in sign."""
    flo = f(lo)
    steps = 0
    while hi - lo > tol:
        mid = (lo + hi) / 2
        fmid = f(mid)
        if (fmid > 0) == (flo > 0):     # same sign as left end: root is right
            lo, flo = mid, fmid
        else:
            hi = mid
        steps += 1
    return (lo + hi) / 2, steps

root, steps = bisect(lambda x: x*x - 2*x - 1, 2, 3)
print(root, steps)       # exact answer: 1 + sqrt(2) = 2.41421356237...
2.414213562372879 40

Forty halvings to get twelve decimal places: steady but slow.

Newton’s method is much faster. Starting from a guess \(x_0\), it repeatedly slides down the tangent line to where the tangent meets the axis:

\[x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} ,\]

where \(f'(x)\) is the derivative, the slope of the tangent, which for \(f(x) = ax^2 + bx + c\) is \(2ax + b\). You will derive this properly in Chapter 17 and study Newton’s method, and how it can fail, in Chapter 18. For a preview, take \(f(x) = x^2 - 2\) (root \(\sqrt 2\)) and \(x_0 = 1\): the iteration is \(x_{n+1} = x_n - (x_n^2 - 2)/(2x_n)\), giving \(1 \to 1.5 \to 1.41\overline{6} \to 1.4142157 \to 1.41421356237469\). The number of correct digits roughly doubles each step: four steps where bisection would need about forty.

Bisection versus Newton

Plot a quadratic and run bisection and Newton side by side, one step per click. Bisection shows a shrinking bracket; Newton shows tangent lines. Choose \(x_0\) exactly at the vertex and watch Newton fail (horizontal tangent, division by zero); start Newton on \(x^2 + 1\) and watch it wander chaotically, because there is no real root to find.

8.10. Choosing a method

You now have five ways to solve a quadratic. They are not interchangeable: each has a sweet spot.

A quadratic such as \(x^2 - 5x + 6 = 0\) practically begs to be factored: two numbers with sum 5 and product 6 jump out, and \((x - 2)(x - 3) = 0\) is done in seconds with exact answers. Using Newton’s method there would be rather silly: you would get \(2.0000000000\) after several iterations of arithmetic, and you would still have to run it again from a different starting point to find the other root. Conversely, trying to factor \(3x^2 - 5x - 1\) wastes time hunting for integer pairs that do not exist.

Method Use when Avoid when

Factoring

small integer coefficients; \(\Delta\) is a perfect square; you want exact roots fast

roots are irrational or the coefficients are large or decimal

Completing the square

you need vertex form (max/min); \(a = 1\) and \(b\) even; deriving things

messy fractions would appear and you only want roots

Quadratic formula

any quadratic, by hand, when factoring is not obvious; exact surd answers

on a computer when \(b^2 \gg 4ac\), unless you use the stable variant

Graphing

you want to understand: how many roots, roughly where, sanity checks

you need several significant figures

Numerical (bisection, Newton)

the quadratic is part of a bigger non-polynomial equation; high-degree polynomials; inside an iterative algorithm

a closed form exists and you only need this one quadratic solved

Example 53. Procedure — Method selection for a quadratic equation

Steps:

  1. Put it in standard form. If \(b = 0\) (\(ax^2 + c = 0\)), solve directly: \(x^2 = -c/a\). If \(c = 0\), factor out \(x\).

  2. Compute \(\Delta\). If \(\Delta < 0\) and you only want real roots, stop.

  3. If \(\Delta\) is a perfect square and the coefficients are small, factor.

  4. If you also need the vertex, complete the square.

  5. Otherwise use the quadratic formula (by hand), or the numerically stable formula (on a computer).

  6. Sketch or estimate to sanity-check the answer.

Worked example of choosing. Which method for each?

  • \(x^2 - 49 = 0\): \(b = 0\), so \(x = \pm 7\) directly. (Difference of squares, if you prefer.)

  • \(x^2 + 8x + 11 = 0\): \(\Delta = 64 - 44 = 20\), not a square, but \(b\) is even and \(a = 1\): completing the square is quick: \((x + 4)^2 = 5\), \(x = -4 \pm \sqrt5\).

  • \(0.37x^2 - 1.92x + 0.5 = 0\): decimals, so formula (or calculator): \(\Delta = 3.6864 - 0.74 = 2.9464\), \(x = (1.92 \pm 1.7165)/0.74\), so \(x \approx 4.914\) or \(x \approx 0.2750\).

  • \(x^2 = \cos x\): not a quadratic at all; graph to see there are two solutions (near \(\pm 0.82\)), then Newton.

8.11. When the formula fails: catastrophic cancellation

8.11.1. The problem

The quadratic formula is exact. Floating-point arithmetic is not. Solve Try it: See the 25% error.

\[x^2 - 10^8 x + 1 = 0\]

on a computer using the formula exactly as written:

import math

def naive_roots(a, b, c):
    d = math.sqrt(b * b - 4 * a * c)
    return (-b + d) / (2 * a), (-b - d) / (2 * a)

print(naive_roots(1.0, -1e8, 1.0))
(100000000.0, 7.450580596923828e-09)

The roots multiply to \(c/a = 1\) (Vieta), so if one is \(10^8\) the other must be \(10^{-8}\) (to about 16 digits: the exact small root is \(1.0000000000000000 \times 10^{-8}\)). The computer reports \(7.45 \times 10^{-9}\): an error of 25%, from a formula you just derived with complete rigour. Nothing is wrong with the algebra. Something is wrong with how the computer carried it out.

8.11.2. Why it happens

A double-precision float carries about 16 significant decimal digits (Chapter 32). Here \(b^2 = 10^{16}\) and \(4ac = 4\). Then

\[\sqrt{b^2 - 4ac} = \sqrt{10^{16} - 4} \approx 10^8 - 2\times 10^{-8} = 99\,999\,999.99999998 .\]

That number needs 18 significant digits, but doubles near \(10^8\) are spaced \(2^{-26} \approx 1.49 \times 10^{-8}\) apart, so the nearest one the computer can store is \(10^8 - 1.49 \times 10^{-8}\). The small root is computed as

\[\frac{-b - \sqrt{\Delta}}{2a} = \frac{10^8 - 99\,999\,999.99999998\ldots}{2},\]

the difference of two nearly equal numbers. Their first 16 digits agree and cancel, leaving only the last one or two digits, which are mostly rounding error: the computer gets \((1.49 \times 10^{-8})/2 \approx 7.45 \times 10^{-9}\), exactly the wrong answer printed above. This is catastrophic cancellation: subtraction of nearly equal approximate quantities destroys relative accuracy. The large root, \((-b + \sqrt\Delta)/(2a)\), adds two numbers of the same sign and is perfectly accurate.

Catastrophic cancellation

Catastrophic cancellation is the loss of significant digits when two nearly equal floating-point numbers are subtracted. The subtraction itself is exact; the problem is that the inputs' small rounding errors become large relative to the small result. The cure is almost always algebraic: rewrite the expression so the subtraction never happens.

8.11.3. A by-hand version

You can see the effect without a computer by pretending your calculator keeps only 4 significant digits. Solve \(x^2 + 62.10x + 1 = 0\) (true roots \(\approx -0.01611\) and \(\approx -62.08\)): Try it: The 4-digit calculator.

\[\begin{aligned} b^2 &= 3856.41 \to 3856 &&\text{(round to 4 digits)}\\ b^2 - 4ac &= 3856 - 4 = 3852\\ \sqrt{3852} &= 62.064\ldots \to 62.06\\ x_1 &= \frac{-62.10 + 62.06}{2} = \frac{-0.04}{2} = -0.02000 . &&\text{(only one significant digit survived)} \end{aligned}\]

The answer \(-0.02000\) is 24% too large in magnitude. The other root, \((-62.10 - 62.06)/2 = -124.16/2 \to -124.2/2 = -62.10\), is off only in its last digit (true value \(-62.08\)).

8.11.4. The fix: compute the safe root first, then use Vieta

The product of the roots is \(x_1 x_2 = c/a\) (Section 8.12). So compute the root that involves no cancellation, and get the other from the product. Concretely, choose the sign in front of the square root to match the sign of \(b\), so that \(b\) and \(\operatorname{sign}(b)\sqrt\Delta\) add rather than cancel:

\[q = -\frac{1}{2}\Big(b + \operatorname{sign}(b)\sqrt{b^2 - 4ac}\Big), \qquad x_1 = \frac{q}{a}, \qquad x_2 = \frac{c}{q} .\]

Here \(\operatorname{sign}(b)\) is \(+1\) if \(b \ge 0\) and \(-1\) if \(b < 0\). Why is \(c/q\) a root? Multiply the two roots from the textbook formula: \(\frac{-b + \sqrt\Delta}{2a}\cdot\frac{-b - \sqrt\Delta}{2a} = \frac{b^2 - \Delta}{4a^2} = \frac{4ac}{4a^2} = \frac{c}{a}\). So if \(x_1 = q/a\) is one root, the other is \(\frac{c/a}{q/a} = \frac{c}{q}\).

In the 4-digit example: \(62.10 + 62.06 = 124.16 \to 124.2\), so \(q = -62.10\), \(x_2 = q/a = -62.10\) and \(x_1 = c/q = 1/(-62.10) = -0.01610\). The true value is \(-0.016107\ldots\), so the small root is now off by at most one unit in the fourth digit, instead of 24%.

import math

def stable_roots(a, b, c):
    d = math.sqrt(b * b - 4 * a * c)
    q = -(b + math.copysign(d, b)) / 2   # b and +-d have the same sign: no cancellation
    return q / a, c / q

print(stable_roots(1.0, -1e8, 1.0))
(100000000.0, 1e-08)

math.copysign(d, b) returns \(d\) with the sign of \(b\): exactly the \(\operatorname{sign}(b)\sqrt\Delta\) term.

Log-log plot: the textbook formula’s relative error grows steadily from about 1e-15 at b equals 10 to 100 percent from about b equals 1.3e8, while the stable formula stays at machine precision throughout
Figure 59. Relative error of the small root of \(x^2 + bx + 1 = 0\) as \(|b|\) grows

The plot tells the story: the textbook formula loses roughly two digits of accuracy for every factor of 10 in \(|b|\) (its relative error grows like \(b^2\) times the machine precision \(\approx 10^{-16}\), about 25% at \(|b| = 10^8\) and 100% from about \(1.34\times 10^8\)), while the stable version stays at full precision throughout. Try it: Slide b and watch digits go.

Formulas are not algorithms

A formula that is exactly correct in real-number arithmetic can be a poor algorithm in floating point. Warning signs: subtracting quantities that may be nearly equal; computing a small number as the difference of two large ones; adding terms of wildly different size. The quadratic formula has a second hazard: \(b^2\) can overflow (exceed \(\approx 1.8 \times 10^{308}\)) even when the roots are modest, so production solvers also rescale the coefficients. Even the discriminant itself suffers cancellation when \(b^2 \approx 4ac\) (nearly repeated roots); then the roots are genuinely sensitive to tiny changes in the coefficients, and no rewriting can fully fix that. This is the difference between an unstable algorithm (fixable) and an ill-conditioned problem (not), a distinction developed in Chapter 32.

Computer-science connection: a production-quality quadratic solver

Here is a solver that combines the method-selection logic and the stable formula. It treats the degenerate case \(a = 0\) (a linear equation) explicitly, distinguishes the three discriminant cases, and never subtracts nearly equal numbers to form a root.

import math

def solve_quadratic(a, b, c):
    """Real roots of a*x**2 + b*x + c = 0, computed stably.

    Returns a sorted tuple of 0, 1 or 2 roots.
    """
    if a == 0:                              # not quadratic: bx + c = 0
        if b == 0:
            raise ValueError("no unknown left to solve for")
        return (-c / b,)
    disc = b * b - 4 * a * c
    if disc < 0:
        return ()                           # complex roots only
    if disc == 0:
        return (-b / (2 * a),)              # repeated root
    q = -0.5 * (b + math.copysign(math.sqrt(disc), b))
    return tuple(sorted((q / a, c / q)))

print(solve_quadratic(1, -5, 6))            # factorable
print(solve_quadratic(1, -1e8, 1))          # cancellation-prone
print(solve_quadratic(1, 2, 1))             # repeated root
print(solve_quadratic(1, 0, 1))             # no real roots
print(solve_quadratic(-4.9, 15, 2))         # the ball
(2.0, 3.0)
(1e-08, 100000000.0)
(-1.0,)
()
(-0.12798267536970298, 3.1892071651656213)

Note that disc == 0 is an exact floating-point comparison. In practice a computed discriminant is rarely exactly zero, so code that must classify "tangent" cases (collision detection, for example) compares against a tolerance instead; which tolerance is right depends on the scale of the problem.

Real libraries go further. NumPy’s np.roots does not use the formula at all; it builds a companion matrix whose eigenvalues are the roots and calls a general eigenvalue solver (Chapter 28), which works for polynomials of any degree.

How much precision do you lose?

Slide \(b\) from \(10\) to \(10^{10}\) in \(x^2 + bx + 1 = 0\). Show both formulas' results digit by digit, with correct digits in green and wrong digits in red, and the number of lost digits plotted against \(\log_{10}|b|\). Add a "precision" switch (16, 7, or 4 significant digits) to see the same collapse happen earlier in single precision and in the by-hand 4-digit model.

The quadratic formula in the wild

George Forsythe’s 1966 essay "How do you solve a quadratic equation?" used exactly this example to argue that numerical analysis is not optional knowledge for anyone who computes. William Kahan, the principal architect of the IEEE 754 floating-point standard, later wrote a careful paper on computing the discriminant itself accurately, using fused multiply–add instructions. The humble quadratic is the classic test case that numerical analysts use to teach the subject.

8.12. Vieta’s formulas: what the coefficients know about the roots

8.12.1. The problem

The stable algorithm relied on "the roots multiply to \(c/a\)". Where does that come from, and what else is true?

8.12.2. Why it works

If \(r_1\) and \(r_2\) are the roots of \(ax^2 + bx + c\), the factor theorem says

\[\begin{aligned} ax^2 + bx + c &= a(x - r_1)(x - r_2)\\ &= a\big(x^2 - (r_1 + r_2)x + r_1 r_2\big) &&\text{(expand)}\\ &= ax^2 - a(r_1 + r_2)x + a r_1 r_2 . \end{aligned}\]

Two polynomials are equal for every \(x\) only if their coefficients match, so

\[b = -a(r_1 + r_2), \qquad c = a\,r_1 r_2 .\]
Vieta’s formulas

For \(ax^2 + bx + c = 0\) with roots \(r_1, r_2\) (real or complex, counted with multiplicity):

\[r_1 + r_2 = -\frac{b}{a}, \qquad r_1 r_2 = \frac{c}{a} .\]

For a monic quadratic \(x^2 + bx + c\): the roots sum to \(-b\) and multiply to \(c\). This is exactly why factoring \(x^2 + bx + c\) means "find two numbers with sum \(b\) and product \(c\)" (the numbers are the negatives of the roots).

8.12.3. Uses

  • Checking answers without substituting (as in the formula example above).

  • Building a quadratic from its roots. Roots \(3 \pm \sqrt2\): sum \(6\), product \(9 - 2 = 7\), so \(x^2 - 6x + 7 = 0\).

  • The other root for free. \(x = 2\) is a root of \(3x^2 - 4x - 4 = 0\); the product of roots is \(-4/3\), so the other root is \(-\tfrac{4}{3} \div 2 = -\tfrac{2}{3}\).

  • Stable computation, as in Section 8.11.

  • Symmetric expressions in the roots without finding them. For \(x^2 - 5x + 3 = 0\), \(r_1^2 + r_2^2 = (r_1 + r_2)^2 - 2r_1 r_2 = 25 - 6 = 19\).

Viète

François Viète (1540–1603), a French lawyer and royal cryptanalyst who broke Spanish ciphers for Henri IV, was among the first to use letters systematically for known quantities as well as unknowns, which is what made general formulas like these expressible at all. His relations extend to every degree: for a cubic \(x^3 + bx^2 + cx + d\) with roots \(r_1, r_2, r_3\), the roots sum to \(-b\), their pairwise products sum to \(c\), and their product is \(-d\).

8.13. Quadratic inequalities

8.13.1. The problem

For which times is the ball above 10 m? For which production levels is the company profitable? These ask where a quadratic is positive (or negative), not where it is zero: inequalities such as \(-2q^2 + 120q - 1000 > 0\).

8.13.2. Intuition

A parabola can only change sign where it crosses the axis, at its roots. Between consecutive roots its sign is constant. So: find the roots, then decide the sign on each interval. For an upward parabola (\(a > 0\)) with two roots, the quadratic is negative between the roots and positive outside them; for a downward one, the reverse.

Graph of y equals x squared minus x minus 6, crossing the x-axis at minus 2 and 3. The region outside the roots, where the curve is above the axis, is shaded green; between the roots it is below the axis and shaded red
Figure 60. A quadratic is negative between its roots and positive outside them (for \(a > 0\))
Example 54. Procedure — Solving a quadratic inequality

Input: an inequality involving a quadratic.

Steps:

  1. Rearrange to \(ax^2 + bx + c > 0\) (or \(<\), \(\ge\), \(\le\)): zero on one side.

  2. Find the real roots (any method). If there are none, the quadratic has the sign of \(a\) everywhere: the solution is "all real \(x\)" or "no \(x\)".

  3. Sketch the parabola (direction from \(a\); crossings at the roots), or make a sign table using test points.

  4. Read off the intervals where the sign is as required. Include the roots for \(\ge\) or \(\le\); exclude them for \(>\) or \(<\).

Output: the solution set, as inequalities or interval notation.

8.13.3. Worked examples

(a) \(x^2 - x - 6 > 0\). Try it: Sign chart for x² − x − 6.

\[x^2 - x - 6 = (x + 2)(x - 3), \qquad \text{roots } -2,\ 3 .\]

\(a = 1 > 0\), so the parabola is above the axis outside the roots: \(x < -2\) or \(x > 3\). A sign table confirms:

Interval \(x + 2\) \(x - 3\) product

\(x < -2\) (test \(-3\))

\(-\)

\(-\)

\(+\)

\(-2 < x < 3\) (test \(0\))

\(+\)

\(-\)

\(-\)

\(x > 3\) (test \(4\))

\(+\)

\(+\)

\(+\)

In interval notation the answer is \((-\infty, -2) \cup (3, \infty)\). Here round brackets mean the endpoint is excluded, square brackets would mean included, \(\infty\) is not a number but a marker for "no upper end", and \(\cup\) (union) means "or".

(b) \(-x^2 + 4x + 5 \ge 0\). Multiply by \(-1\) to make the leading coefficient positive, reversing the inequality: Try it: Sign chart for example (b).

\[\begin{aligned} x^2 - 4x - 5 &\le 0 &&\text{(multiply by } -1\text{; flip } \ge \text{ to } \le\text{)}\\ (x - 5)(x + 1) &\le 0 &&\text{(roots } -1,\ 5\text{)}\\ -1 \le x &\le 5 . &&\text{(between the roots, endpoints included)} \end{aligned}\]

(c) Profitable production: \(-2q^2 + 120q - 1000 > 0\) is \(q^2 - 60q + 500 < 0\) (divide by \(-2\), flip), i.e. \((q - 10)(q - 50) < 0\), so \(10 < q < 50\): between 1,000 and 5,000 units. Try it: Sign chart for the profit.

(d) The ball above 10 m: \(-4.9t^2 + 15t - 8 > 0\). Roots (formula, \(\Delta = 68.2\), \(\sqrt\Delta \approx 8.2583\)): \(t = (15 \mp 8.2583)/9.8 \approx 0.688\) and \(2.373\). Downward parabola, so positive between the roots: about \(0.69 < t < 2.37\) seconds, a window of \(\sqrt{\Delta}/|a| \approx 1.69\) s. Try it: When is the ball above 10 m?.

Inequality traps
  • Square-rooting an inequality. \(x^2 > 4\) does not mean \(x > 2\); it means \(x > 2\) or \(x < -2\) (try \(x = -5\)). Always go via the roots and a sketch.

  • Multiplying or dividing by a negative number flips the inequality. Dividing by an expression whose sign you do not know is forbidden: you cannot tell whether to flip.

  • No real roots does not mean no solution. \(x^2 + 1 > 0\) is true for every \(x\); \(x^2 + 1 < 0\) for none.

US curriculum

Quadratic inequalities and sign analysis usually appear in Algebra II, and again in Precalculus for polynomial and rational inequalities. Interval notation is typically introduced in Algebra II.

8.14. Applications

8.14.1. Projectile motion

Every object moving under constant acceleration (and nothing else) has a position that is a quadratic function of time. That includes a thrown ball, a dropped phone, a braking car (constant deceleration), and a rocket in the short phase where thrust and gravity are roughly constant.

Worked example: maximum height. For \(h(t) = 2 + 15t - 4.9t^2\), the vertex is at

\[t^* = -\frac{b}{2a} = -\frac{15}{2(-4.9)} = \frac{15}{9.8} \approx 1.531 \text{ s},\]

and the maximum height is

\[h(t^*) = c - \frac{b^2}{4a} = 2 - \frac{225}{4(-4.9)} = 2 + \frac{225}{19.6} \approx 2 + 11.480 = 13.48 \text{ m}.\]

In SUVAT language you would have said: at the top the velocity is zero, \(v = u + at\) gives \(0 = 15 - 9.8t\), so \(t = 15/9.8\). Same answer, reached through physics instead of algebra, and a good illustration that the vertex of a quadratic is where its rate of change is zero, the idea that drives Chapter 18.

Braking distance. A car travelling at \(u\) m/s decelerates at \(d\) m/s². Its stopping distance is \(u^2/(2d)\): quadratic in speed. Doubling speed quadruples braking distance, which is why the UK Highway Code’s stopping distances grow so steeply.

8.14.2. Optimising area and revenue

Any quantity that is a product of two things, one increasing and one decreasing linearly in the same variable, is a downward quadratic and has a maximum at its vertex.

The fence. \(A(x) = x(100 - 2x) = -2x^2 + 100x\). Vertex at \(x = -100/(2 \cdot (-2)) = 25\). Area \(A(25) = 25 \times 50 = 1250\) m². Shortcut: the roots are \(0\) and \(50\), and the vertex is midway between them.

Left, a rectangle against a wall with two sides of length x and a side of 100 minus 2x. Right, the area function rising to a maximum of 1250 at x equals 25 and returning to zero at x equals 50
Figure 61. The pen against a wall and its area function

Revenue. A café’s simple demand model says it would sell 1000 coffees a week if they were free, and loses 20 weekly sales for every £1 on the price: demand \(q = 1000 - 20p\). Revenue is price times quantity:

\[R(p) = p(1000 - 20p) = -20p^2 + 1000p .\]

Roots \(p = 0\) and \(p = 50\); vertex midway at \(p = 25\), with \(R(25) = 25 \times 500 = 12{,}500\), i.e. £12,500 a week. Raising the price beyond £25 loses more in volume than it gains in margin. (Real demand curves are not linear, but this is the first model economists draw, and the reasoning is the same with a better model plus calculus.)

The break-even company is at its best at the midpoint of its break-even volumes, \(q = 30\): \(P(30) = -1800 + 3600 - 1000 = 800\), i.e. £800,000 at 3,000 units.

8.14.3. The reflective property: dishes, headlights and telescopes

Every parabola has a special point, its focus. For \(y = \dfrac{x^2}{4f}\) the focus is at \((0, f)\). Every ray travelling parallel to the axis that hits the inside of the parabola reflects through the focus. Spin the parabola about its axis to get a paraboloid, and every parallel ray striking the dish converges at one point. Try it: Rays meeting at the focus.

A parabola with vertical incoming rays reflecting off its surface and all passing through the focus at 0, f; a dashed horizontal directrix lies below
Figure 62. Rays parallel to the axis all reflect through the focus

That is why:

  • Satellite dishes put the receiver at the focus: the faint parallel signal from a satellite 36,000 km away is concentrated there.

  • Reflecting telescopes (Newton’s design and every large modern telescope) use paraboloidal mirrors to bring starlight to a focus.

  • Headlights and torches run the reflection backwards: a bulb at the focus sends light out in a parallel beam.

  • Solar cookers and solar-thermal power plants concentrate sunlight on a pipe or pot at the focus.

Worked example: where to put the receiver. A dish is 60 cm across and 9 cm deep. Place its vertex at the origin, so its cross-section is \(y = x^2/(4f)\) and the rim is at \((30, 9)\): Try it: Place the receiver yourself.

\[\begin{aligned} 9 &= \frac{30^2}{4f} &&\text{(rim lies on the curve)}\\ 36f &= 900 &&\text{(multiply both sides by } 4f\text{)}\\ f &= 25 \text{ cm}. \end{aligned}\]

The receiver sits 25 cm in front of the centre of the dish. The reason the property holds is a short calculation with distances: every point of the parabola is equally far from the focus and from a line called the directrix (\(y = -f\)); the proof uses tangent lines and is done in Chapter 10.

Whispering galleries and burning mirrors

A legend says Archimedes set fire to Roman ships at Syracuse (212 BCE) with "burning mirrors". Whether or not it happened (modern attempts have produced mixed results), Diocles' treatise On Burning Mirrors (around 190 BCE) contains the first known proof of the focal property of the parabola. The same geometry, applied to the ellipse, makes "whispering galleries" where a whisper at one focus is heard clearly at the other.

8.15. Computer-science connection: ray–sphere intersection

8.15.1. The problem

A ray tracer makes images by firing a ray from the camera through each pixel and asking: what does this ray hit first? For a sphere, that question is a quadratic equation, solved millions of times per frame. This is perhaps the most-executed quadratic formula in the world.

8.15.2. Setting it up

A ray starts at a point \(\mathbf{o}\) (the camera) and travels in direction \(\mathbf{d}\). Bold letters denote vectors, here just triples of numbers \((x, y, z)\) (Chapter 26). The points on the ray are

\[\mathbf{p}(t) = \mathbf{o} + t\,\mathbf{d}, \qquad t \ge 0,\]

meaning: start at \(\mathbf{o}\) and move \(t\) units of \(\mathbf{d}\); component by component, \(p_x = o_x + t d_x\) and so on. The parameter \(t\) measures how far along the ray you are; smaller \(t\) is closer to the camera.

A sphere with centre \(\mathbf{c}\) and radius \(r\) is the set of points at distance \(r\) from \(\mathbf{c}\). Squared distance is computed by Pythagoras in 3D: for a vector \(\mathbf{v} = (v_x, v_y, v_z)\),

\[\mathbf{v}\cdot\mathbf{v} = v_x^2 + v_y^2 + v_z^2 ,\]

the dot product of \(\mathbf{v}\) with itself. More generally, \(\mathbf{u}\cdot\mathbf{v} = u_x v_x + u_y v_y + u_z v_z\); it distributes over addition just like ordinary multiplication.

A ray from a camera point passing through a circle, with the entry point labelled t1 and the exit point labelled t2; the centre c and radius r are marked
Figure 63. A ray hitting a sphere: the two values of \(t\) are where the ray enters and leaves

8.15.3. Deriving the quadratic

The ray point \(\mathbf{p}(t)\) is on the sphere when its squared distance from the centre is \(r^2\). Write \(\mathbf{f} = \mathbf{o} - \mathbf{c}\) for the vector from the centre to the camera. Then \(\mathbf{p}(t) - \mathbf{c} = \mathbf{f} + t\mathbf{d}\), and

\[\begin{aligned} (\mathbf{f} + t\mathbf{d})\cdot(\mathbf{f} + t\mathbf{d}) &= r^2 &&\text{(squared distance equals } r^2\text{)}\\ \mathbf{f}\cdot\mathbf{f} + 2t\,(\mathbf{d}\cdot\mathbf{f}) + t^2\,(\mathbf{d}\cdot\mathbf{d}) &= r^2 &&\text{(expand, like } (f + td)^2\text{)}\\ \underbrace{(\mathbf{d}\cdot\mathbf{d})}_{a}\,t^2 + \underbrace{2(\mathbf{d}\cdot\mathbf{f})}_{b}\,t + \underbrace{(\mathbf{f}\cdot\mathbf{f} - r^2)}_{c} &= 0 . &&\text{(standard form in } t\text{)} \end{aligned}\]

A quadratic in \(t\), and every piece of this chapter now has a geometric meaning:

  • \(\Delta < 0\): the ray misses the sphere. (This is the common case, and you can decide it without a square root.)

  • \(\Delta = 0\): the ray grazes the sphere tangentially.

  • \(\Delta > 0\): two intersections, \(t_1 < t_2\): entering and leaving. The visible surface is at the smallest positive \(t\).

  • \(t_1 < 0 < t_2\): the camera is inside the sphere.

  • Both negative: the sphere is behind the camera.

8.15.4. Worked example

Camera at \(\mathbf{o} = (0, 1, 0)\), looking along \(\mathbf{d} = (1, 0, 0)\); sphere centre \(\mathbf{c} = (5, 0, 0)\), radius \(r = 2\).

\[\begin{aligned} \mathbf{f} &= \mathbf{o} - \mathbf{c} = (-5, 1, 0)\\ a &= \mathbf{d}\cdot\mathbf{d} = 1\\ b &= 2(\mathbf{d}\cdot\mathbf{f}) = 2(1\cdot(-5) + 0\cdot 1 + 0 \cdot 0) = -10\\ c &= \mathbf{f}\cdot\mathbf{f} - r^2 = 25 + 1 + 0 - 4 = 22\\ \Delta &= b^2 - 4ac = 100 - 88 = 12 > 0 &&\text{(hit)}\\ t &= \frac{10 \pm \sqrt{12}}{2} = 5 \pm \sqrt3 \approx 3.268 \text{ or } 6.732 . \end{aligned}\]

The visible point is at \(t \approx 3.268\): \(\mathbf{p} \approx (3.268, 1, 0)\). Check: its distance from the centre is \(\sqrt{(3.268 - 5)^2 + 1^2} = \sqrt{3.000 + 1} = 2\) ✓.

import math

def dot(u, v):
    return sum(ui * vi for ui, vi in zip(u, v))

def ray_sphere(origin, direction, centre, radius, eps=1e-9):
    """Smallest t > eps where origin + t*direction meets the sphere, or None."""
    f = [o - c for o, c in zip(origin, centre)]   # centre -> camera
    a = dot(direction, direction)
    b = 2 * dot(direction, f)
    c = dot(f, f) - radius * radius
    disc = b * b - 4 * a * c
    if disc < 0:
        return None                               # ray misses
    q = -0.5 * (b + math.copysign(math.sqrt(disc), b))   # stable formula
    if q == 0:
        return None                               # degenerate: grazing at origin
    t1, t2 = sorted((q / a, c / q))
    if t1 > eps:
        return t1                                 # first hit in front of camera
    if t2 > eps:
        return t2                                 # camera is inside the sphere
    return None                                   # sphere is behind the camera

print(ray_sphere((0, 1, 0), (1, 0, 0), (5, 0, 0), 2))   # hit
print(ray_sphere((0, 3, 0), (1, 0, 0), (5, 0, 0), 2))   # passes above: miss
print(ray_sphere((5, 0, 0), (1, 0, 0), (5, 0, 0), 2))   # from inside
3.267949192431123
None
2.0

The small eps guards against a classic ray-tracing bug, "shadow acne": a ray bounced off a surface, starting on that surface, finds a spurious intersection at \(t \approx 10^{-15}\) due to rounding, so the surface shadows itself. The stable formula matters here too, though not where you might guess. A small sphere far away is harmless: both formulas agree to about 16 digits. The textbook formula loses digits when \(c\) is tiny compared with \(b^2\), which happens when the ray starts very close to the surface of a large sphere (a shadow or reflection ray leaving a big floor or planet). Then the near root comes from cancellation, which is exactly the shadow-acne situation above.

A one-sphere ray tracer

A 200 by 200 pixel canvas. For each pixel, fire a ray and colour it by the discriminant: black for \(\Delta < 0\), and shaded by \(t_1\) (near is bright) otherwise. Move the camera into the sphere and watch the \(t_1 < 0 < t_2\) case take over. Then toggle "textbook formula" and push the sphere far away to see cancellation noise appear at the edges.

US curriculum

Ray–sphere intersection is not school mathematics; it uses Algebra I quadratics with vectors from Precalculus (or a college linear-algebra course). It is a common first project in university computer-graphics courses.

8.16. Interactive tools

Browser tools on the companion website that go with this chapter. Each example link in the chapter opens one of them with that example already set up.

8.17. Exercises

  1. (Recognition) For \(5 - 3x^2 + 2x = 0\), identify \(a\), \(b\), \(c\) and compute the discriminant. How many real roots?

  2. (Recognition) Without solving, state the vertex, the axis of symmetry and whether the vertex is a maximum or minimum: (a) \(y = -3(x + 2)^2 + 7\) (b) \(y = (x - 4)(x + 6)\).

  3. (Application) Solve by factoring: (a) \(x^2 + x - 20 = 0\) (b) \(3x^2 - 10x - 8 = 0\) (c) \(4x^2 = 9\).

  4. (Application) Solve by completing the square, giving exact answers: (a) \(x^2 - 8x + 3 = 0\) (b) \(3x^2 + 12x - 1 = 0\).

  5. (Application) Solve with the quadratic formula, giving exact and 4-significant-figure answers: \(2x^2 + 3x - 7 = 0\).

  6. (Application) Write \(y = -x^2 + 6x - 5\) in vertex form and in factored form, and sketch it.

  7. (Application) Solve \(2x^2 - 5x - 3 \le 0\) and \(x^2 + 4x + 5 > 0\).

  8. (Problem solving) Find all values of \(k\) for which \(x^2 + kx + 9 = 0\) has exactly one real root. For which \(k\) does it have no real roots?

  9. (Problem solving) A stone is thrown upward at 20 m/s from a cliff top 25 m above the sea (take \(g = 10\) m/s²). Find when it hits the water and its greatest height above the sea.

  10. (Problem solving) A rectangle has perimeter 36 cm and area 72 cm². Find its dimensions using Vieta’s formulas (set up a quadratic whose roots are the side lengths).

  11. (Problem solving) Derive the formula for the \(x\)-coordinate of the vertex, \(-b/(2a)\), from the quadratic formula by averaging the two roots.

  12. (Computing) Using the naive_roots and stable_roots functions, solve \(x^2 + 10^5 x + 1 = 0\) and \(x^2 - 10^{12} x + 1 = 0\). How many correct significant digits does the naive small root have in each case?

  13. (Computing) Modify ray_sphere to return the surface normal at the hit point, the unit vector \((\mathbf{p} - \mathbf{c})/r\), and test it on the worked example.

Answers
  1. Standard form \(-3x^2 + 2x + 5 = 0\): \(a = -3\), \(b = 2\), \(c = 5\). \(\Delta = 4 - 4(-3)(5) = 4 + 60 = 64 > 0\): two real roots (and since \(64 = 8^2\) they are rational: \(x = (-2 \pm 8)/(-6)\), i.e. \(-1\) and \(5/3\)).

  2. (a) Vertex \((-2, 7)\), axis \(x = -2\), maximum (\(a = -3 < 0\)). (b) Roots \(4, -6\); axis \(x = (4 + (-6))/2 = -1\); \(y(-1) = (-5)(5) = -25\); vertex \((-1, -25)\), minimum.

  3. (a) \((x + 5)(x - 4) = 0\): \(x = -5, 4\). (b) \(ac = -24 = -12 \cdot 2\), \(-12 + 2 = -10\): \(3x^2 - 12x + 2x - 8 = 3x(x - 4) + 2(x - 4) = (3x + 2)(x - 4)\): \(x = -\tfrac23, 4\). (c) \(4x^2 - 9 = (2x - 3)(2x + 3) = 0\): \(x = \pm\tfrac32\).

  4. (a) \(x^2 - 8x = -3\); add 16: \((x - 4)^2 = 13\); \(x = 4 \pm \sqrt{13}\). (b) Divide by 3: \(x^2 + 4x = \tfrac13\); add 4: \((x + 2)^2 = \tfrac{13}{3}\); \(x = -2 \pm \sqrt{13/3} = -2 \pm \tfrac{\sqrt{39}}{3}\).

  5. \(\Delta = 9 + 56 = 65\); \(x = \dfrac{-3 \pm \sqrt{65}}{4}\); \(\sqrt{65} \approx 8.0623\); \(x \approx 1.266\) or \(x \approx -2.766\). Vieta check: sum \(-1.500 = -3/2\) ✓, product \(\approx -3.502 \approx -7/2\) ✓.

  6. \(y = -(x^2 - 6x) - 5 = -\big((x - 3)^2 - 9\big) - 5 = -(x - 3)^2 + 4\). Vertex \((3, 4)\), maximum. Roots: \((x - 3)^2 = 4\), \(x = 1, 5\), so \(y = -(x - 1)(x - 5)\). Sketch: cap through \((1, 0)\), \((5, 0)\), peak \((3, 4)\), \(y\)-intercept \((0, -5)\).

  7. \(2x^2 - 5x - 3 = (2x + 1)(x - 3)\), roots \(-\tfrac12, 3\), upward parabola, \(\le 0\) between: \(-\tfrac12 \le x \le 3\). Second: \(\Delta = 16 - 20 < 0\) and \(a > 0\), so it is always positive: all real \(x\).

  8. One root when \(\Delta = k^2 - 36 = 0\): \(k = \pm 6\) (giving \((x \pm 3)^2\)). No real roots when \(k^2 < 36\): \(-6 < k < 6\).

  9. \(h(t) = 25 + 20t - 5t^2\). Hits water: \(5t^2 - 20t - 25 = 0\), \(t^2 - 4t - 5 = 0\), \((t - 5)(t + 1) = 0\), \(t = 5\) s. Greatest height at \(t = 20/10 = 2\) s: \(25 + 40 - 20 = 45\) m above the sea.

  10. Sides \(s_1 + s_2 = 18\), \(s_1 s_2 = 72\). They are the roots of \(x^2 - 18x + 72 = 0 = (x - 6)(x - 12)\): 6 cm by 12 cm.

  11. \(\dfrac{1}{2}\left(\dfrac{-b + \sqrt\Delta}{2a} + \dfrac{-b - \sqrt\Delta}{2a}\right) = \dfrac12 \cdot \dfrac{-2b}{2a} = -\dfrac{b}{2a}\). The square roots cancel, which is the symmetry of the parabola in algebraic form.

  12. For \(b = 10^5\) the naive small root is \(-1.0000003385 \times 10^{-5}\) against the true \(-1.0000000001 \times 10^{-5}\): a relative error of about \(3 \times 10^{-7}\), so only 6 or 7 correct digits (roughly 10 lost, consistent with error \(\approx b^2 \times 10^{-16} = 10^{-6}\)). For \(b = -10^{12}\), \(b^2 - 4 = 10^{24} - 4\) rounds to exactly \(10^{24}\), the square root is exactly \(10^{12}\), and the naive small root is \(0.0\): every digit lost. The stable version gives \(10^{-12}\) correctly.

  13. After computing t, form p = [o + t * d for o, d in zip(origin, direction)] and return [(pi - ci) / radius for pi, ci in zip(p, centre)]. For the worked example the normal is approximately \((-0.866, 0.5, 0)\), pointing back towards the camera, as it should.

8.18. Mastery check

  1. Without solving, how many real roots does \(4x^2 - 20x + 25 = 0\) have? What is special about this quadratic?

  2. Solve \(x^2 - 5x + 6 = 0\) and \(x^2 - 5x + 5 = 0\). Which method did you use for each, and why?

  3. Starting from \(ax^2 + bx + c = 0\), derive the quadratic formula, justifying each step.

  4. Write \(y = 3x^2 + 12x + 7\) in vertex form. State its minimum value and where it occurs.

  5. Solve \(x^2 + 2x - 15 < 0\), giving your answer in interval notation.

  6. A rectangular field is to be fenced on all four sides with 240 m of fencing. What dimensions maximise its area, and what is the maximum area?

  7. The roots of \(x^2 + 10^9 x + 1 = 0\) are needed in double-precision floating point. Which formula gives the root near \(0\) accurately, and why does the textbook formula fail?

  8. Set up (but do not solve) the quadratic in \(t\) for a ray from \((0, 0, 0)\) in direction \((0, 0, 1)\) meeting a sphere of radius 1 centred at \((0, 0, 4)\). Then solve it and interpret both roots.

Decision rule: All eight correct without hesitation → move on to Chapter 9 (or Chapter 11, which builds directly on the forms and transformations here). Items 1–3 shaky → reread Section 8.5 and Section 8.6; the derivation is the heart of the chapter. Items 4–6 shaky → reread Section 8.8, Section 8.13 and Section 8.14. Items 7–8 shaky → reread Section 8.11 and Section 8.15. Stuck on more than three → work the chapter exercises first, then retry.

Mastery check answers
  1. \(\Delta = 400 - 400 = 0\): exactly one (repeated) root. The quadratic is a perfect square, \((2x - 5)^2\), and its graph touches the axis at \(x = \tfrac52\).

  2. \(x^2 - 5x + 6 = (x - 2)(x - 3)\): \(x = 2, 3\). Factoring, because \(\Delta = 1\) is a perfect square and the factor pair is obvious. For \(x^2 - 5x + 5\): \(\Delta = 25 - 20 = 5\), not a square, so use the formula: \(x = \tfrac{5 \pm \sqrt5}{2} \approx 3.618, 1.382\).

  3. See Section 8.6: divide by \(a\); move \(c/a\) across; add \((b/2a)^2\) to both sides; write the left side as \((x + b/2a)^2\) and the right side over \(4a^2\) as \((b^2 - 4ac)/4a^2\); take \(\pm\) square roots (requires \(b^2 - 4ac \ge 0\)); subtract \(b/2a\).

  4. \(3(x^2 + 4x) + 7 = 3\big((x + 2)^2 - 4\big) + 7 = 3(x + 2)^2 - 5\). Minimum value \(-5\) at \(x = -2\).

  5. \((x + 5)(x - 3) < 0\); upward parabola, negative between the roots: \(-5 < x < 3\), i.e. \((-5, 3)\).

  6. Sides \(x\) and \(120 - x\); area \(A = x(120 - x)\), roots \(0, 120\), vertex at \(x = 60\). A 60 m by 60 m square, area 3600 m².

  7. Use \(q = -\tfrac12\big(b + \operatorname{sign}(b)\sqrt{\Delta}\big)\), then the small root is \(c/q\). Here \(q \approx -10^9\) and \(c/q \approx -10^{-9}\). The textbook formula computes the small root as \((-b + \sqrt{\Delta})/2\) with \(b = 10^9\), subtracting two numbers that agree in essentially all their digits (in fact \(b^2 - 4\) rounds to \(b^2\), so the naive result is exactly 0): catastrophic cancellation.

  8. \(\mathbf{f} = (0, 0, -4)\), \(a = 1\), \(b = 2(\mathbf{d}\cdot\mathbf{f}) = -8\), \(c = 16 - 1 = 15\): \(t^2 - 8t + 15 = 0 = (t - 3)(t - 5)\). The ray enters the sphere at \(t = 3\) (point \((0, 0, 3)\), the visible surface) and leaves at \(t = 5\) (point \((0, 0, 5)\), the back of the sphere).