Quadratic equation with complex roots

Dedi_Khana
3 min readMar 6, 2023

--

On occasion, quadratic equations do not possess real roots, and rather possess solutions in the form of complex numbers.

A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, defined as the square root of -1. The real part of the complex number is a, and the imaginary part is b. Complex numbers are useful in mathematics and engineering, where they are used to represent quantities that have both magnitude and direction, such as electrical impedance, wave amplitudes, and phase angles. They are also used in physics, particularly in quantum mechanics and electromagnetism.

Here’s an example of a quadratic equation with complex number solutions:

x² + 4x + 5 = 0

To solve this equation, we can use the quadratic formula:

x = (-b ± sqrt(b² — 4ac)) / 2a

where a, b, and c are the coefficients of the quadratic equation.

Plugging in the values from our equation, we get:

x = (-4 ± sqrt(4² — 4(1)(5))) / 2(1)

x = (-4 ± sqrt(-4)) / 2

Since the square root of a negative number is an imaginary number, we can simplify this further:

x = (-4 ± 2i) / 2

x = -2 ± i

Therefore, the solutions to the quadratic equation x^2 + 4x + 5 = 0 are -2 + i and -2 - i.

In Python, we can represent complex numbers using the complex function, which takes two arguments: the real part and the imaginary part. Here's an example Python code that calculates the solutions to the quadratic equation and prints them:

import math
import matplotlib.pyplot as plt

# Define the coefficients of the quadratic equation
a = 1
b = 4
c = 5

# Calculate the discriminant
discriminant = b**2 - 4*a*c

# Check if the discriminant is negative (no real roots)
if discriminant < 0:
# Calculate the complex roots
root1 = complex(-b/(2*a), math.sqrt(-discriminant)/(2*a))
root2 = complex(-b/(2*a), -math.sqrt(-discriminant)/(2*a))
else:
# Calculate the real roots
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)

# Print the solutions
print("The solutions to the quadratic equation are:")
print(root1)
print(root2)

# Plot the complex root
if discriminant < 0:
plt.scatter(root1.real, root1.imag, color='red')
plt.scatter(root2.real, root2.imag, color='red')
plt.xlabel('Real')
plt.ylabel('Imaginary')
plt.title('Complex roots of quadratic equation')
plt.grid()
plt.show()

If we want to plot above function, we can use below code.

import math
import matplotlib.pyplot as plt
import numpy as np

# Define the coefficients of the quadratic equation
a = 1
b = 4
c = 5

# Define the range of x values to plot
x = np.linspace(-10, 10, 1000)

# Calculate the corresponding y values
y = a*x**2 + b*x + c


# Plot the function
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.grid()

If a quadratic equation does not have real number solutions, it means that the equation does not intersect the x-axis.

--

--