Given a quadratic equation the task is solve the equation or find out the roots of the equation. Standard form of quadratic equation is –
ax2 + bx + c where, a, b, and c are coefficient and real numbers and also a ≠ 0. If a is equal to 0 that equation is not valid quadratic equation.
Input :a = 1, b = 2, c = 1 Output : Roots are real and same -1.0 Input :a = 2, b = 2, c = 1 Output : Roots are complex -0.5 + i 2.0 -0.5 - i 2.0 Input :a = 1, b = 10, c = -24 Output : Roots are real and different 2.0 -12.0
Method 1: Using the direct formula
Using the below quadratic formula we can find the root of the quadratic equation.
There are following important cases.
If b*b < 4*a*c, then roots are complex (not real). For example roots of x2 + x + 1, roots are -0.5 + i1.73205 and -0.5 - i1.73205 If b*b == 4*a*c, then roots are real and both roots are same. For example, roots of x2 - 2x + 1 are 1 and 1 If b*b > 4*a*c, then roots are real and different. For example, roots of x2 - 7x - 12 are 3 and 4
real and different roots 2.0 -12.0
Method 2: Using the complex math module
First, we have to calculate the discriminant and then find two solution of quadratic equation using cmath module.
The roots are (-3.414213562373095+0j) (-0.5857864376269049+0j)
- Python - Solve the Linear Equation of Multiple Variable
- Python Bokeh - Plotting Quadratic Curves on a Graph
- Python | Solve given list containing numbers and arithmetic operators
- Python | sympy.solve() method
- Python | Sympy Ellipse.equation() method
- Handwritten Equation Solver in Python
- Python | Sympy equation() method
- Python | Finding Solutions of a Polynomial Equation
- Python | Sympy Plane.equation() method
- SciPy - Integration of a Differential Equation for Curve Fit
- ML | Normal Equation in Linear Regression
- Python Program for Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Python Program for Program to Print Matrix in Z form
- Python Program for Program to calculate area of a Tetrahedron
- Python Program for Efficient program to print all prime factors of a given number
- Python Program for Program to find area of a circle
- Hello World Program : First program while learning Programming
- Python program to check if the list contains three consecutive common numbers in Python
- OpenCV Python Program to blur an image
- Output of Python Program | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.