Open In App

Solve Quadratic Equation in R

R language is the language of data visualization and data analytics. It is used to solve complex problems or to visualize the given datasets. In this article, we are going to learn how we can solve quadratic equations using R Programming Language.

What is a Quadratic Formula?

The quadratic formula is used to find the x-intercepts of the quadratic equation, We use the quadratic formula to solve the quadratic equation.



Where ‘x’ is given or we to find out and ‘a’, ‘b’, and ‘c’ are known numbers such that a!=0



Understanding Discriminant and its Three Cases

Notice the above formula and the square root part of it. ( b2-4ac ) is called the discriminant and it is used to determine the real roots that we will get or not.

These three cases are used to determine whether we get the solution or not, if the discriminant is negative we don’t solve the quadratic equation.

Solving Quadratic using R language

Now we are going to write a code using R to solve quadratic equations using quadratic formulas.

# Quadratic equation looks like this ax^2 + bx + c
 
# Creating Function
 
quadRoots <- 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) {  # if discriminant is less than zero i.e no real roots
    return(paste0("This quadratic equation has no real numbered roots."))
  }
  else if(discriminant > 0) { # If discriminant is greater than 0 ie real roots
    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  i.e only one root
    x_int <- (-b) / (2*a)
    return(paste0("The quadratic equation has only one root. This root is ",
                  x_int))
}

                    

Initially, we created a function named ‘quadRoots()’ in which we define variable and calculate the discriminant. Based on discriminant value, the function call the correct if-else ladder.

quadRoots(2,43,45)

                    

Output:

[1] "You have chosen the quadratic equation 2x^2 + 43x + 45."
[1] "The two x-intercepts for the quadratic equation are -1.10311 and -20.39689."

Here, we get (b2-4ac) > 0, that’s why we got two solutions.

quadRoots(2,3,4)

                    

Output:

[1] "You have chosen the quadratic equation 2x^2 + 3x + 4."
[1] "This quadratic equation has no real numbered roots."

After writing the function in R call the function with any three parameters as a, b, and c.

quadRoots(2, 4, 2)

                    

Output:

[1] "You have chosen the quadratic equation 2x^2 + 4x + 2."
[1] "The quadratic equation has only one root. This root is -1"

Here we get ‘D=0’, which means that the quadratic equation has only one real root.

Solving Quadratic using another function in R language

# Function 2
result <- function(a,b,c){
   
  if(discriminant(a,b,c) > 0){ # first case when D>0
     
    x_1 = (-b+sqrt(discriminant(a,b,c)))/(2*a)
     
    x_2 = (-b-sqrt(discriminant(a,b,c)))/(2*a)
     
    result = c(x_1,x_2)
  }
  else if(discriminant(a,b,c) == 0){ # second case when D=0
   
      x = -b/(2*a)
  }
  else {"There are no real roots."} # third case when D<0
}
 
#function 2
# dicriminant function for calculating discriminant
discriminant<-function(a,b,c){
  b^2-4*a*c
}

                    

We have created a function with the name ‘result()’ that will take three arguments representing ‘a’, ‘b’, ‘c’.

a<-result(34,233,123)
a

                    

Output:

[1] -0.5763735 -6.2765677

Here, D>0 that’s why we got two real roots.

a<-result(2,-5,4)
a

                    

Output:

[1] "There are no real roots."

Here, D<0 that’s why it doesn’t have any real roots.

a<-result(1,-2,1)
a

                    

Output:

[1] 1

Here, D=0 that’s why the equation has only one root.

Conclusion

R is useful for solving mathematical problems not simple but also complex problems. This language is majorly used for data visualization and solving complex mathematical problems. In the article, we have learned to solve quadratic equations using quadratic formulas.


Article Tags :