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.
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.
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\).
In R, a function has the following format.
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.
# 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.
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)
.
# 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."
Example Two
The quadratic formula applied to the equation \(y = x^2 + 7x + 5\) yields:
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 \(y = 2x^2 + 1.5x + 2\) we get:
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
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
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
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"