Hướng dẫn solve quadratic equation python

Given a quadratic equation the task is solve the equation or find out the roots of the equation. Standard form of quadratic equation is $$ax^2+ bx + c =0$$ where, $a, b$, and $c$ are coefficient and real numbers and also $a \ne 0$ 0. If $a$ is equal to $0$ that equation is not valid quadratic equation.

See more: Hướng dẫn lập trình Python – Python Guide

1. Python Solve Quadratic Equation Using the Direct Formula

Using the below quadratic formula we can find the root of the quadratic equation.

Let $\Delta =b^2-4ac$, then:

  • If $b^2 – 4ac<0$, then roots are complex (not real).
  • If $b^2 – 4ac=0$, then roots are real and both roots are same $x=\frac{-b}{2a}$.
  • If $b^2 – 4ac>0$, then roots are real and different $$ x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}.$$

# Python program to find roots of quadratic equation 
import math 


# function for finding roots 
def equationroots(): 

   a = float(input('Enter coefficient a: '))
   while a == 0:
      print("Coefficient a can not equal 0")
      a = float(input('Enter coefficient a: '))
   b = float(input('Enter coefficient b: '))
   c = float(input('Enter coefficient c: '))
   
   # calculating dcriminant using formula 
   d = b * b - 4 * a * c 
   
   # checking condition for dcriminant 
   if d > 0: 
      print("Your equation has real and different roots:") 
      print((-b + math.sqrt(d))/(2 * a)) 
      print((-b - math.sqrt(d))/(2 * a)) 
   
   elif d == 0: 
      print("Your equation has real and same roots:") 
      print(-b / (2 * a)) 
   
   # when dcriminant is less than 0 
   else: 
      print("Your equation has complex roots:") 
      print(- b / (2 * a), " +", math.sqrt(-d),'i') 
      print(- b / (2 * a), " -", math.sqrt(-d),'i') 


equationroots() 

2. Python Solve Quadratic Equation Using the Complex Math Module

First, we have to calculate the discriminant and then find two solution of quadratic equation using cmath module.

cmath module — Mathematical functions for complex numbers — provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments. They will also accept any Python object that has either a __complex__() or a __float__() method: these methods are used to convert the object to a complex or floating-point number, respectively, and the function is then applied to the result of the conversion.

# Python program to find roots of quadratic equation 
import cmath 


# function for finding roots 
def equationroots(): 

   a = float(input('Enter coefficient a: '))
   while a == 0:
      print("Coefficient a can not equal 0")
      a = float(input('Enter coefficient a: '))
   b = float(input('Enter coefficient b: '))
   c = float(input('Enter coefficient c: '))
   
   # calculating dcriminant using formula 
   d = b * b - 4 * a * c 
   
   if d == 0: 
      print("Your equation has real and same roots:") 
      print(-b / (2 * a)) 
   
   # when dcriminant is not equal 0 
   else: 
      print("Your equation has complex roots:") 
      print(- b / (2 * a), " +", cmath.sqrt(d)) 
      print(- b / (2 * a), " -", cmath.sqrt(d)) 


equationroots() 

input()

The standard form of a quadratic equation is:

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0

The solutions of this quadratic equation is given by:

(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)

Source Code

# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
import cmath

a = 1
b = 5
c = 6

# calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

Output

Enter a: 1
Enter b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)

We have imported the cmath module to perform complex square root. First, we calculate the discriminant and then find the two solutions of the quadratic equation.

You can change the value of a, b and c in the above program and test this program.

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.

Hướng dẫn solve quadratic equation python

Examples:

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.

Hướng dẫn solve quadratic equation python

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

import math 

def equationroots( a, b, c): 

    dis = b * b - 4 * a *

    sqrt_val = math.sqrt(abs(dis)) 

    if dis > 0

        print(" real and different roots "

        print((-b + sqrt_val)/(2 * a)) 

        print((-b - sqrt_val)/(2 * a)) 

    elif dis == 0

        print(" real and same roots"

        print(-b / (2 * a)) 

    else:

        print("Complex Roots"

        print(- b / (2 * a), " + i", sqrt_val) 

        print(- b / (2 * a), " - i", sqrt_val) 

a = 1

b = 10

c = -24

if a == 0

        print("Input correct quadratic equation"

else:

    equationroots(a, b, c)

Output:

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.

import cmath

a = 1

b = 4

c = 2

dis = (b**2) - (4 * a*c)

ans1 = (-b-cmath.sqrt(dis))/(2 * a)

ans2 = (-b + cmath.sqrt(dis))/(2 * a)

print('The roots are')

print(ans1)

print(ans2)

Output:

The roots are
(-3.414213562373095+0j)
(-0.5857864376269049+0j)