![]() |
VOOZH | about |
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.
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
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.
Now we are going to write a code using R to solve quadratic equations using quadratic formulas.
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.
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.
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.
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.
We have created a function with the name 'result()' that will take three arguments representing 'a', 'b', 'c'.
Output:
[1] -0.5763735 -6.2765677Here, D>0 that's why we got two real roots.
Output:
[1] "There are no real roots."Here, D<0 that's why it doesn't have any real roots.
Output:
[1] 1Here, D=0 that's why the equation has only one root.
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.