Hướng dẫn quadratic equation python - phương trình bậc hai python

Đưa ra một phương trình bậc hai, nhiệm vụ là giải phương trình hoặc tìm ra rễ của phương trình. Hình thức tiêu chuẩn của phương trình bậc hai là $$ AX^2 + BX + C = 0 $$ trong đó, $ a, b $, và $ c $ là số và số thực và cũng $ a \ ne 0 $ 0. Nếu $ a $ bằng $ 0 $, phương trình đó không hợp lệ phương trình bậc hai.

Xem thêm: Hướng dẫn lập trình Python - Hướng dẫn Python

1. Python giải phương trình bậc hai bằng cách sử dụng công thức trực tiếp

Sử dụng công thức bậc hai dưới đây, chúng ta có thể tìm thấy gốc của phương trình bậc hai.

Đặt $ \ delta = b^2-4ac $, sau đó:

  • Nếu $ b^2 - 4ac
  • Nếu $ b^2-4ac = 0 $, thì rễ là có thật và cả hai gốc đều giống nhau $ x = \ frac {-b} {2a} $.
  • Nếu $ b^2-4ac> 0 $, thì rễ là có thật và khác nhau $$ 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 giải phương trình bậc hai bằng mô -đun toán học phức tạp

Đầu tiên, chúng ta phải tính toán phân biệt đối xử và sau đó tìm hai giải pháp của phương trình bậc hai bằng cách sử dụng & nbsp; ____ 11 & nbsp; mô -đun.

# 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()
1 Mô -đun - Các hàm toán học cho các số phức - & nbsp; cung cấp quyền truy cập vào các hàm toán học cho các số phức. Các hàm trong mô-đun này chấp nhận số nguyên, số dấu phẩy động hoặc số phức là đối số. Họ cũng sẽ chấp nhận bất kỳ đối tượng Python nào có phương thức
# 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()
3 hoặc
# 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()
4: các phương pháp này được sử dụng để chuyển đổi đối tượng thành số điểm phức tạp hoặc nổi, và sau đó chức năng được áp dụng cho kết quả chuyển đổi.

# 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()

Hình thức tiêu chuẩn của phương trình bậc hai là:

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

Các giải pháp của phương trình bậc hai này được đưa ra bởi:

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

Mã nguồn

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

Đầu ra

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

Chúng tôi đã nhập mô -đun

# 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()
1 để thực hiện căn bậc hai phức tạp. Đầu tiên, chúng tôi tính toán phân biệt đối xử và sau đó tìm hai giải pháp của phương trình bậc hai.

Bạn có thể thay đổi giá trị của A, B và C trong chương trình trên và kiểm tra chương trình này.

Đưa ra một phương trình bậc hai, nhiệm vụ là giải phương trình hoặc tìm ra rễ của phương trình. Dạng tiêu chuẩn của phương trình bậc hai là -

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 quadratic equation python - phương trình bậc hai 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

Phương pháp 1: Sử dụng công thức trực tiếp Using the direct formula

Sử dụng công thức bậc hai dưới đây, chúng ta có thể tìm thấy gốc của phương trình bậc hai.

Đặt $ \ delta = b^2-4ac $, sau đó:

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

Nếu $ b^2 - 4ac

Nếu $ b^2-4ac = 0 $, thì rễ là có thật và cả hai gốc đều giống nhau $ x = \ frac {-b} {2a} $.

Nếu $ b^2-4ac> 0 $, thì rễ là có thật và khác nhau $$ x = \ frac {-b \ pm \ sqrt {b^2-4ac}} {2a}. $$ $$

2. Python giải phương trình bậc hai bằng mô -đun toán học phức tạp

Đầu tiên, chúng ta phải tính toán phân biệt đối xử và sau đó tìm hai giải pháp của phương trình bậc hai bằng cách sử dụng & nbsp; ____ 11 & nbsp; mô -đun.

# 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))
3
# 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))
4
# 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))
5
# 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))
6
# 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))
7

# 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()
1 Mô -đun - Các hàm toán học cho các số phức - & nbsp; cung cấp quyền truy cập vào các hàm toán học cho các số phức. Các hàm trong mô-đun này chấp nhận số nguyên, số dấu phẩy động hoặc số phức là đối số. Họ cũng sẽ chấp nhận bất kỳ đối tượng Python nào có phương thức
# 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()
3 hoặc
# 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()
4: các phương pháp này được sử dụng để chuyển đổi đối tượng thành số điểm phức tạp hoặc nổi, và sau đó chức năng được áp dụng cho kết quả chuyển đổi.

Hình thức tiêu chuẩn của phương trình bậc hai là:

Các giải pháp của phương trình bậc hai này được đưa ra bởi:

# 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))
3
# 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))
4
# 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))
5
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
2
# 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))
7

Mã nguồn

Đầu ra

# 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))
3
# 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))
4
# 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))
5
# 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()
00
# 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))
7

Chúng tôi đã nhập mô -đun

# 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()
1 để thực hiện căn bậc hai phức tạp. Đầu tiên, chúng tôi tính toán phân biệt đối xử và sau đó tìm hai giải pháp của phương trình bậc hai.

Bạn có thể thay đổi giá trị của A, B và C trong chương trình trên và kiểm tra chương trình này.

Đưa ra một phương trình bậc hai, nhiệm vụ là giải phương trình hoặc tìm ra rễ của phương trình. Dạng tiêu chuẩn của phương trình bậc hai là -

Phương pháp 1: Sử dụng công thức trực tiếp

Có những trường hợp quan trọng sau đây.

# 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()
6
# 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()
7

# 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))
3
# 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))
4
# 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))
5
# 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()
45
# 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))
7

real and different roots
2.0
-12.0
5
real and different roots
2.0
-12.0
6

# 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()
8
# 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()
9

Output:

real and different roots
2.0
-12.0

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
0
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
1
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
2
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
3
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
4
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
3__
Using the complex math module

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
0
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
3
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
2
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
5
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
6
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
7

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
0
(-b ± (b ** 2 - 4 * a * c) ** 0.5) / (2 * a)
9
# 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))
0
# 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))
1
# 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))
2

Đưa ra một phương trình bậc hai, nhiệm vụ là giải phương trình hoặc tìm ra rễ của phương trình. Dạng tiêu chuẩn của phương trình bậc hai là -

Phương pháp 1: Sử dụng công thức trực tiếp

Có những trường hợp quan trọng sau đây.

# 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()
6
# 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()
7

# 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()
8
# 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()
9

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
0
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
1
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
2
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
3
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
4
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
3__

# 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))
4
# 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))
5
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
02
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
03

# 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))
4
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
05

# 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))
4
ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0
07

Output:

# 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()
0