# 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.

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.

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 (). 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).

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

Example Two

The quadratic formula applied to the equation  yields:

Example Three

In the equation  we get:

Example Four

Example Five

Example Six