Open In App

Solve Quadratic Equation in R

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

[Tex]x = \frac{{-b \pm \sqrt{{b^2 – 4ac}}}}{{2a}} [/Tex]

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

  • If a=0, then the equation is linear and not a quadratic equation.
  • a,b,c are the coefficients of the equation, they are also called linear coefficients, quadratic coefficients, or free term.

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.

  • Case 1: (b2-4ac) > 0
    If this case is true, then we would have two distinct solutions or two real roots.
  • Case 2: (b2-4ac) = 0
    If this case is true, then There would be one solution or one real root for the equation.
  • Case 3: (b2-4ac) < 0
    If this case is true, then there would be no real roots and hence we don’t have to solve the equation at all.

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.

R

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

  • The user has to pass three arguments in the function to get the result. The parameters represent ‘a’, ‘b’, ‘c’.
  • The ‘nsmall’ argument is passed in ‘format()’ to set the precision of a number of digits right to the decimal point.

R

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.

  • We stored the discriminant value in x_int_plus and x_int_neg. As we have to calculate the discriminant, once taking the ‘+’ sign and once taking the ‘-‘ sign.

R

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.

  • It calculates the discriminant (b2 – 4ac) to execute the correct if-else ladder.
  • If the discriminant is negative, the function returns a message indicating that the quadratic equation has no real-numbered roots.
  • The function calculates and returns the two distinct real roots using the quadratic formula if the discriminant is positive.
  • If it is zero, then the equation has only one real root.

R

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.

  • Calculating the roots of quadratic equations that cant be solved by using a simple factoring technique.

Solving Quadratic using another function in R language

R

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

  • Then, we have a second function with the name ‘discriminant()’ that will help us to find the discriminant.
  • The logic here is simple we follow the quadratic formula for solving the equation that can’t be solved by using the factoring technique.

R

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

Output:

[1] -0.5763735 -6.2765677

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

R

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.

R

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads