xxxxxxxxxx
import math
def solve_quadratic(a, b, c):
# Calculate the discriminant
discriminant = b**2 - 4*a*c
# Check if the equation has complex roots
if discriminant < 0:
return "Complex Roots"
# Calculate the roots
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)
# Return the roots
return root1, root2
# Example usage
a = 1
b = -5
c = 6
roots = solve_quadratic(a, b, c)
print("Roots:", roots)
xxxxxxxxxx
(-b-sqrt(b**2-4*a*c))/(2*a)
(-b+sqrt(b**2-4*a*c))/(2*a)
If that doesn't work,
((0-b)-sqrt(b**2-4*a*c))/(2*a)
((0-b)+sqrt(b**2-4*a*c))/(2*a)
Quadratic Equation are equations where the highest power of the variable (usually x) is 2, hence the word Quadratic Equations
The standard form of Quadratic Equations is as follows...
xxxxxxxxxx
ax^2 + bx + c = 0 where a ≠ 0
This is because, even with an equation such as x^2 = 5, you can always* change it to the standard form.
xxxxxxxxxx
x^2 = 5
x^2 - 5 = 0
x^2 - 5 + 0x = 0 ### 0 * x is 0 anyways so we can add that
x^2 + 0x - 5 = 0
1x^2 + 0x - 5 = 0 ### 1 * x^2 is x^2 so we can add that (no change)
There! We got 1x^2 + 0x - 5 = 0!
You can cross check it with the standard form and see its the same!
Did you wonder why "a" can't be zero in the standard form? Well look here.
xxxxxxxxxx
5x^2 + 10x + 20 = 0 # PERFECTLY NORMAL QUADRATIC EQUATION
0x^2 + 10x + 20 = 0 # changing "a" to 0
10x + 20 = 0
At first glance, one may not notice the issue but let me pin point the definition of Quadratic Equation...
"Quadratic Equation are equations where the highest power of the variable is 2"
The definition states the highest power of x should be 2 but in 10x + 20 = 0 the highest power is 1 (where is no power, there is 1) so this equation became a linear equation! so to prevent that is why "a" or the coefficient of "x^2" can't equal to 0 in a Quadratic Equation
thank u! <3
NOW GO LEARN
-----------------
* = take with a pinch of salt
xxxxxxxxxx
Formula:
(-b±√(b²-4ac))/(2a)
general form:
ax^2 + bx + c = 0
x^2 + bx + c = 0
some of the other hidden forms:
x^2 = bx - c (quadriatic after transposition)
c/bx+d = x (where you end up with a quadriatic after cross multiplication)
xxxxxxxxxx
import math
def solve_quadratic_equation(a, b, c):
discriminant = b**2 - 4*a*c
if discriminant > 0:
# Two distinct real roots
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)
return root1, root2
elif discriminant == 0:
# One real root
root = -b / (2*a)
return root
else:
# Complex roots
real_part = -b / (2*a)
imaginary_part = math.sqrt(abs(discriminant)) / (2*a)
root1 = complex(real_part, imaginary_part)
root2 = complex(real_part, -imaginary_part)
return root1, root2
# Example usage with coefficients a, b, and c
a = 1
b = -3
c = 2
roots = solve_quadratic_equation(a, b, c)
print("Roots:", roots)