The Quadratic Formula In R

Hi. This page will be about the quadratic formula in R. Since I come from a mathematics and a statistics background I am more familiar with the statistical program R. This guide can also be for people who use Python and other programming languages.


Table Of Contents


The Quadratic Formula?

The quadratic formula is a useful formula for solving x-intercepts of quadratic equations in the form of

    \[y = ax^2 + bx + c\]

The quadratic formula (with a \neq 0) is:

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

It is preferable to use the quadratic formula when factoring techniques do not work.


The Discriminant And Three Cases

Notice how in the quadratic formula there is a square root part after the plus and minus sign (\pm). The part inside the square root (b^2 - 4ac) is called the discriminant.

An important property of square roots is that square roots take on numbers which are at least 0 (non-negative). A negative number inside the square root is undefined (in the real numbers).

There are three cases for the discriminant. Each case determines the number of solutions in a quadratic equation.

If (b^2 - 4ac) > 0 then there would be 2 distinct solutions for x (or x-intercepts) in the equation 0 = ax^2 + bx + c.\

If (b^2 - 4ac) = 0 then there would be one value for x in the equation 0 = ax^2 + bx + c.

If (b^2 - 4ac) < 0, we would have a negative value inside the square root. The square root of a negative value is undefined. There would be no real-numbered values for x in the equation 0 = ax^2 + bx + c.


Creating The Quadratic Formula Function In R

In R, a function has the following format.

Since the quadratic formula has three cases with the discriminant we need if, else if and else statements. The usage of print and paste0() allows for printing strings in R.

Here is my full code in R.

The format() function with round() is used to round the answers (x-intercepts) to five decimal places.


Using The Quadratic Formula Through Examples

The quadratic formula can be applied to any quadratic equation in the form y = ax^2 + bx + c (a \neq 0). It does not really matter whether the quadratic form can be factored or not.

Example One

In this example, the quadratic formula is used for the equation y = x^2 + 5. In this case we have a = 1, b= 0 and c = 5. The function call in R would be quadraticRoots(1, 0 , 5).

This quadratic equation has no real roots. The discriminant would be -4 \times 1 \times 5 = -20.

Example Two

The quadratic formula applied to the equation y = x^2 + 7x + 5 yields:

Example Three

In the equation y = 2x^2 + 1.5x + 2 we get:

Example Four

Example Five

Example Six

 

Leave a Reply