algebra25 min

Quadratic Equations

Solving ax^2 + bx + c = 0 using factoring, completing the square, and the quadratic formula

0/9Not Started

Why This Matters

A quadratic equation has the form ax^2 + bx + c = 0, where the highest power of x is 2. Quadratics model anything that curves: the path of a thrown ball, the area of a growing rectangle, the revenue curve of a pricing model. Unlike linear equations which have at most one solution, quadratics can have zero, one, or two solutions -- and the discriminant tells you which case you are in before you even start solving.

The quadratic formula, x = (-b plus-or-minus sqrt(b^2 - 4ac)) / 2a, is one of the most famous formulas in all of mathematics. It works for every quadratic equation, whether the roots are neat integers, messy fractions, or even complex numbers. The expression under the square root, b^2 - 4ac, is the discriminant: positive means two real roots, zero means one repeated root, and negative means no real roots.

In programming, quadratic equations appear in physics engines (projectile motion), optimization problems (finding maxima and minima), and anywhere you need to find where a parabolic curve intersects a line or axis. The quadratic formula is a short function that solves a huge class of problems.

Define Terms

Visual Model

ax^2 + bx + c = 0Standard form
Discriminantb^2 - 4ac
D > 0Two real roots
D = 0One repeated root
D < 0No real roots
Quadratic Formula(-b +/- sqrt(D)) / 2a
Solutionsx1 and x2

The full process at a glance. Click Start tour to walk through each step.

The discriminant reveals the number of roots before you solve. The quadratic formula handles every case.

Code Example

Code
// The quadratic formula
function solveQuadratic(a, b, c) {
  const discriminant = b * b - 4 * a * c;
  console.log(`Equation: ${a}x^2 + ${b}x + ${c} = 0`);
  console.log(`Discriminant: ${discriminant}`);

  if (discriminant < 0) {
    console.log("No real roots");
    return [];
  } else if (discriminant === 0) {
    const root = -b / (2 * a);
    console.log(`One repeated root: x = ${root}`);
    return [root];
  } else {
    const sqrtD = Math.sqrt(discriminant);
    const root1 = (-b + sqrtD) / (2 * a);
    const root2 = (-b - sqrtD) / (2 * a);
    console.log(`Two roots: x = ${root1} and x = ${root2}`);
    return [root1, root2];
  }
}

// x^2 - 5x + 6 = 0  =>  x = 2 and x = 3
solveQuadratic(1, -5, 6);

// 2x^2 + 3x - 5 = 0  =>  x = 1 and x = -2.5
console.log();
solveQuadratic(2, 3, -5);

// x^2 + 4x + 4 = 0  =>  x = -2 (repeated)
console.log();
solveQuadratic(1, 4, 4);

// x^2 + 1 = 0  =>  no real roots
console.log();
solveQuadratic(1, 0, 1);

// Verify a root
function verify(a, b, c, x) {
  const result = a * x * x + b * x + c;
  console.log(`\nVerify x=${x}: ${a}(${x})^2 + ${b}(${x}) + ${c} = ${result}`);
}
verify(1, -5, 6, 2); // Should be 0
verify(1, -5, 6, 3); // Should be 0

Interactive Experiment

Try these exercises:

  • Solve x^2 - 9 = 0 by factoring (difference of squares) and verify with the quadratic formula.
  • What discriminant value gives x^2 + 6x + 9 = 0? How many roots does it have?
  • Try x^2 + 2x + 5 = 0. What is the discriminant? Why are there no real roots?
  • Create a quadratic with roots at x = 1 and x = 4. Hint: multiply (x - 1)(x - 4).
  • Find the vertex (peak/valley) of x^2 - 6x + 5 using x = -b/(2a). What is y at the vertex?

Quick Quiz

Coding Challenge

Quadratic Formula

Write a function called `quadraticFormula` that takes a, b, c and returns an array of the real roots of ax^2 + bx + c = 0. If the discriminant is negative, return an empty array. If the discriminant is 0, return an array with one element. If positive, return an array with two elements sorted from smallest to largest. Round all values to 2 decimal places.

Loading editor...

Real-World Usage

Quadratic equations describe curved relationships in many domains:

  • Projectile motion: The height of a thrown object follows h(t) = -4.9t^2 + v0*t + h0. Setting h = 0 and solving gives landing time.
  • Optimization: Maximum revenue, minimum cost, and optimal dimensions often occur at the vertex of a parabola.
  • Physics: Kinematic equations, gravitational potential energy, and spring force (Hooke law) involve quadratics.
  • Computer graphics: Intersecting a ray with a sphere requires solving a quadratic equation for the parameter t.
  • Economics: Profit = Revenue - Cost curves are often quadratic, and the break-even points are the roots.

Connections