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

The quadratic formula (with ) is:

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 (). The part inside the square root () 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 then there would be 2 distinct solutions for (or x-intercepts) in the equation .\

If then there would be one value for in the equation .

If , 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 in the equation .

**Creating The Quadratic Formula Function In R**

In R, a function has the following format.

1 2 3 |
functionName <- function(arg_1, arg_2, ..., arg_n) { < Put Code Here > } |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# Quadratic Formula In R: # Reference: http://stackoverflow.com/questions/15589601/print-string-and-variable-contents-on-the-same-line-in-r # Quadratic equation form of ax^2 + bx + c # Create quadratic formula function: quadraticRoots <- function(a, b, c) { print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, ".")) discriminant <- (b^2) - (4*a*c) if(discriminant < 0) { return(paste0("This quadratic equation has no real numbered roots.")) } else if(discriminant > 0) { x_int_plus <- (-b + sqrt(discriminant)) / (2*a) x_int_neg <- (-b - sqrt(discriminant)) / (2*a) return(paste0("The two x-intercepts for the quadratic equation are ", format(round(x_int_plus, 5), nsmall = 5), " and ", format(round(x_int_neg, 5), nsmall = 5), ".")) } else #discriminant = 0 case x_int <- (-b) / (2*a) return(paste0("The quadratic equation has only one root. This root is ", x_int)) } |

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 (). 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 . In this case we have , and . The function call in R would be quadraticRoots(1, 0 , 5).

1 2 3 4 5 |
> # Test Cases: > > quadraticRoots(1, 0, 5) [1] "You have chosen the quadratic equation 1x^2 + 0x + 5." [1] "This quadratic equation has no real numbered roots." |

This quadratic equation has no real roots. The discriminant would be .

__Example Two__

The quadratic formula applied to the equation yields:

1 2 3 |
quadraticRoots(1, 7, 5) [1] "You have chosen the quadratic equation 1x^2 + 7x + 5." [1] "The two x-intercepts for the quadratic equation are -0.80742 and -6.19258." |

__Example Three__

In the equation we get:

1 2 3 |
> quadraticRoots(2, 1.5, 2) [1] "You have chosen the quadratic equation 2x^2 + 1.5x + 2." [1] "This quadratic equation has no real numbered roots." |

__Example Four__

1 2 3 |
> quadraticRoots(-3, -5, 7) [1] "You have chosen the quadratic equation -3x^2 + -5x + 7." [1] "The two x-intercepts for the quadratic equation are -2.57338 and 0.90672." |

__Example Five__

1 2 3 |
> quadraticRoots(2, 4, 2) [1] "You have chosen the quadratic equation 2x^2 + 4x + 2." [1] "The quadratic equation has only one root. This root is -1" |

__Example Six__

1 2 3 |
> quadraticRoots(1, 2, 1) [1] "You have chosen the quadratic equation 1x^2 + 2x + 1." [1] "The quadratic equation has only one root. This root is -1" |