Python Program to Solve Quadratic Equation

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

A Quadratic Equation is of the form a x ^ 2 + b x + c = 0 where, aa, bb, and cc are coefficients and a0a \neq 0. To solve a quadratic equation means to find the values of xx, which can be done using the quadratic formula.
We can write Python program to solve quadratic equation using the sqrt() function in math and cmath module.

What is a Quadratic Equation? How to Solve it?

A quadratic equation is a polynomial of degree 2 in one variable (generally xx), it is generally represented as f(x)=ax2+bx+c=0,(a0)f(x) = a x ^ 2 + b x + c = 0, (a \neq 0), where aa is the leading coefficient, bb is the coefficient of xx, and cc is the absolute term of f(x)f(x).
In this equation, aa cannot be 00, because then the degree will no longer be 22 and it won't be a quadratic equation.

A quadratic equation will always have two roots either both real or both complex (α\alpha and β\beta) assuming its coefficients are real.

When a quadratic polynomial is equated to 00, it is called a quadratic equation.
The general form of a quadratic equation is:

ax2+bx+c=0a x ^ 2 + b x + c = 0

Here are some examples of quadratic equations:

  • 3x2+x+4=03 x ^ 2 + x + 4 = 0, here aa is 33, bb is 11, and cc is 44.
  • x2+2x+2=0x ^ 2 + 2 x + 2 = 0, here aa is 11, bb is 22, and cc is 22.
  • 5x2+4=05 x ^ 2 + 4 = 0, here aa is 55, bb is 00, and cc is 44.

A quadratic equation can be solved using the quadratic formula, which is,

x=b±b24ac2ax = \frac { - b \pm \sqrt { b ^ 2 - 4 a c } } { 2 a }

This formula can also be written as,

x=b±D2ax = \frac { - b \pm \sqrt { D } } { 2 a }

Where, D=b24acD = b ^ 2 - 4 a c.

DD is called the discriminant, as it determines the nature of the roots, here are the cases:

  • If D>0D \gt 0: The roots are real and different.
    For example, DD of the quadratic equation x2+3x+2x ^ 2 + 3 x + 2 is 11 and its roots are 1-1 and 2-2.
  • If D=0D = 0: The roots are real and the same.
    For example, DD of the quadratic equation x24x+4x ^ 2 - 4 x + 4 is 00 and its roots are 22 and 22.
  • If D<0D \lt 0: The roots are complex and are conjugate pairs.
    For example, DD of the quadratic equation x2+x+1x ^ 2 + x + 1 is 3-3 and its roots are 12+i32\frac {-1} {2} + \frac {i \sqrt {3}}{2} and 12i32\frac {-1} {2} - \frac {i \sqrt {3}}{2}.

The first root of a quadratic equation is called alpha (α\alpha) and the second root is called beta (β\beta).

α=b+b24ac2a\alpha = \frac { - b + \sqrt { b ^ 2 - 4 a c } } { 2 a }
β=bb24ac2a\beta = \frac { - b - \sqrt { b ^ 2 - 4 a c } } { 2 a }

Note: In complex numbers, a conjugate pair is a pair of numbers whose product is a real number.

For example, 3+2i3 + 2i and 32i3 - 2i are conjugate pairs as their product is a real number.
(3+2i)×(32i)(3 + 2 i) \times (3 - 2 i)
=32(2i)2= 3 ^ 2 - (2 i) ^ 2
=9(4)= 9 - (-4)
=9+4=11= 9 + 4 = 11

Python Program to Solve Quadratic Equation

We will now implement the quadratic formula in Python to solve a quadratic equation of user input.

In the Python program, we will ask the user to input aa, bb, and cc, as we can build a quadratic equation using these coefficients.

Note: A complex number is of the form p+qip + q i, but in Python a complex number is represented as p+qjp + q j (jj represents iotaiota in Python).

Method 1: Using Quadratic Formula Directly

In this method, we will use the sqrt() function from the math module to calculate the discriminant DD.
There is a problem, the math.sqrt() function cannot calculate complex numbers which is necessary to find out complex roots of a quadratic equation. A workaround for this will be to find the square root of abs(D) and then multiply it with 1j1j if D<0D \lt 0.

Code

Output 1

Output 2

In the above example, we are taking the quadratic equation from the user as input and then calling the QuadRoots() function to return the roots of the quadratic equation. Inside the function, we are calculating the value of the discriminant to determine the nature of the roots. We have then used the sqrt() function from the math module to calculate the roots of the quadratic equation.

Method 2: Using The Complex Math Module

In this method, we will use the cmath.sqrt() function from the cmath module to calculate the discriminant DD.
The cmath module is used for complex maths, using this, we can find the square root of DD even if it is negative.

Code

Output 1

Output 2

In the above example, we are taking the quadratic equation from the user as input and then calling the QuadRoots() function to return the roots of the quadratic equation. Inside the function, we are calculating the value of the discriminant to determine the nature of the roots. We have then used the cmath.sqrt() function from cmath module to calculate the roots of the quadratic equation.

Conclusion

  • A quadratic equation is of the form ax2+bx+c=0a x ^ 2 + b x + c = 0, where a0a \neq 0.
  • We can solve a quadratic equation using the quadratic formula, which is x=b±D2ax = \frac {- b \pm \sqrt {D}} {2 a}, where DD is the discriminant and its value is b24acb ^ 2 - 4 a c.
  • We can determine the nature of the roots by calculating the DD.
  • If D>0D \gt 0, the roots are real and different, if D=0D = 0, the roots are real and same, and if D<0D \lt 0, the roots are complex and a conjugative pair.
  • A Python program to solve quadratic equation can be written using the sqrt() function in math and cmath module.
  • The sqrt() function from math module can only find the square root of positive numbers.
  • The cmath.sqrt() function from cmath module can find square root of any number.

Read More: