Lesson 13 of 18
Polynomial Roots
Finding Roots of Quadratics
A quadratic equation has roots given by the quadratic formula:
x = rac{-b pm sqrt{b^2 - 4ac}}{2a}
The expression under the square root, , is the discriminant:
- → two distinct real roots
- → one repeated real root
- → no real roots (complex)
import math
def quadratic_roots(a, b, c):
disc = b**2 - 4*a*c
if disc < 0:
return None # complex roots
x1 = (-b - math.sqrt(disc)) / (2*a)
x2 = (-b + math.sqrt(disc)) / (2*a)
return sorted([round(x1, 6), round(x2, 6)])
print(quadratic_roots(1, -3, 2)) # [1.0, 2.0] (x-1)(x-2)=0
print(quadratic_roots(1, 0, -4)) # [-2.0, 2.0] (x-2)(x+2)=0
print(quadratic_roots(1, 0, 1)) # None (complex roots)
Applications
- Signal processing — filter design uses polynomial roots
- Control systems — stability determined by root locations
- Numerical methods — root-finding is fundamental to scientific computing
Your Task
Implement quadratic_roots(a, b, c) that returns a sorted list of real roots [x1, x2] rounded to 6 decimal places, or None if the roots are complex (discriminant ).
Pyodide loading...
Loading...
Click "Run" to execute your code.