Bobbi Towers / May 10 2019

Completing the square

1. Quadratic Equations

Rewrite the quadratic equation by completing the square.

A quadratic equation consists of a quadratic coefficient :a, linear coefficient :b and a constant :c on the left, and 0 on the right. Let's write a function that will take these values and render it as a human-readable string:

(defn print-quadratic-equation [[a b c & r]]
  (str (if (not= 1 a) a) "x^2 "
       (if (not= 1 b)
         (if (pos? b)
           (str "+ " b "x ")
           (str "- " (- b) "x ")))
       (if (not= 0 c)
         (if (pos? c)
           (str "+ " c " ")
           (str "- " (- c) " ")))
       "= " (if (first r) (first r) 0)))

(-> [4 20 25]
  print-quadratic-equation)
"4x^2 + 20x + 25 = 0"

The right side of the equation is assumed to be 0 if no r term is supplied. We've included conditionals for not rendering a constant of 0 or a coefficient of 1 because that would be redundant. To handle subtraction, we check if the number is less than 0, and if so, subtract it from 0 and print it.

Now we can write functions that will return a new equation with the appropriate manipulation. Let's first move the constant :c over to the right by subtracting it from both sides:

(defn subtract-c [[a b c r]]
  [a b (- c c) (- 0 c)])

(-> [4 20 25]
  subtract-c
  print-quadratic-equation)
"4x^2 + 20x = -25"

Now divide everything by the quadratic coefficient :a to get x^2 by itself:

(defn divide-by-a [[a b c r]]
  [1 (/ b a) 0 (/ r a)])

(-> [4 20 25]
  subtract-c
  divide-by-a
  print-quadratic-equation)
"x^2 + 5x = -25/4"

Now we want to complete the left side into a perfect square. To do that, we add to both sides:

(defn complete-square [[a b c r]]
  (let [square (* (/ b 2) (/ b 2))]
    [a b square (+ square r)]))

(-> [4 20 25]
  subtract-c
  divide-by-a
  complete-square
  print-quadratic-equation)
"x^2 + 5x + 25/4 = 0"

We can now rewrite the left side of the equation as a squared term.

(defn rewrite-squared [[a b c r]]
  (str "(x " (if (pos? (/ b 2))
               (str "+ " (/ b 2))
               (str "- " (- (/ b 2))))
       ")^2 = " r))

(-> [1 7 12]
  subtract-c
  divide-by-a
  complete-square
  rewrite-squared)
"(x + 7/2)^2 = 1/4"

2. Quadratic Functions

Rewrite the function by completing the square.

(defn print-quadratic-function [[a b c]]
  (str "f(x) = "
       (if (not= 1 a) a) "x^2 "
       (if (not= 1 b)
         (if (pos? b)
           (str "+ " b "x ")
           (str "- " (- b) "x ")))
       (if (not= 0 c)
         (if (pos? c)
           (str "+ " c)
           (str "- " (- c))))))

(-> [4 -16 7]
  print-quadratic-function)
"f(x) = 4x^2 - 16x + 7"
(defn divide-fn-by-a [[a b c]]
  [a (/ b a) c])
user/divide-fn-by-a
(defn complete-square-fn [[a b c]]
  (let [square (* (/ b 2) (/ b 2))]
    (str "f(x) = " a "(x " (if (pos? (/ b 2))
                 (str "+ " (/ b 2))
                 (str "- " (- (/ b 2))))
         ")^2 " (if (pos? (- c (* a square)))
                  (str "+ " (- c (* a square)))
                  (str "- " (- (- c (* a square))))))))

(-> [2 -7 5]
  divide-fn-by-a
  complete-square-fn)
"f(x) = 2(x - 7/4)^2 - 9/8"