To solve a quadratic graph, find the x-intercepts (roots) by setting the quadratic equation equal to zero and solving for x, or find the vertex and axis of symmetry.
Key takeaways:
A quadratic equation is a polynomial equation of degree 2, represented as
. Python’s
math
library is used to calculate the discriminant and find the roots using the quadratic formula.The solution can be validated by substituting the roots back into the original quadratic equation.
In this Answer, we’ll discuss the quadratic equation, its nomenclature, formation, and implementation to solve a quadratic equation in Python.
Quadratic equations in mathematics are simple, and yet can be a bit tricky. A polynomial equation with a maximum power of degree 2 is known as a quadratic equation or equation with degree 2. It might be confusing to call it quadratic, which usually means four, but we use it for equations of power 2. To understand why, we need to look into the origin of the word “quadrant.”
The word “Quadrant” originates from the Latin word “Quadri,” which means four, like quadrilaterals, quadrangles, and quadrants. Now, the area of quadrangles, squares, or quadrilaterals is calculated as x^2
which is a polynomial equation with degree 2. That’s why an equation with a maximum degree of 2 is called a quadratic equation.
A quadratic equation is composed of variables, coefficients, and constants. The generic form of a quadratic equation is a
and b
are coefficients, c
is constant, and x
is a variable. Here, the coefficient a
can’t be 0 because it would make the term ax^2
equals to 0, and the equation will become a linear equation. Here's what the graph of a quadratic equation looks like:
Before diving into the code, let’s examine the steps in solving the quadratic equation. We’ll use the quadratic formula
Discriminant:
Check if the discriminant is positive, negative, or zero
Roots by analyzing the value of the discriminant
The Python code in the widget below shows the code to find the discriminant and then the roots to solve the quadratic equation.
import mathdef find_DiscriminantAndRoots(a, b, c):# 1. calculating discriminantdiscriminant = b**2 - 4*a*c# 2. Check if the discriminant is positive, negative, or zeroif discriminant > 0:# 3. If the discriminant is +ve, we will have two real rootsrealRoot1 = (-b + math.sqrt(discriminant)) / (2*a)realRoot2 = (-b - math.sqrt(discriminant)) / (2*a)return ("This quadratic equation has two real roots:", realRoot1, realRoot2)elif discriminant < 0:# 3. If the discriminant is -ve, we will have two complex rootsreal_part = -b / (2*a)imaginary_part = math.sqrt(-discriminant) / (2*a)compRoot1 = complex(real_part, imaginary_part)compRoot2 = complex(real_part, -imaginary_part)return ("This quadratic equation has two complex roots:", compRoot1, compRoot2)else:# 3. If the discriminant is 0, then we will have only one real rootrealRoot = -b / (2*a)return ("This quadratic equation has one real root:", realRoot)# Sample valuesa = 1; b = -5; c = 4if a == 0:print("Invalid equation! Please check the value of coefficient 'a' as it can not be 0.")else:result = find_DiscriminantAndRoots(a, b, c)print(result)
Executing the code above will give us two real roots, 4 and 1. Let’s have a brief overview of the highlighted lines in the code widget above:
Line 1: We import the math
library of Python.
Line 5: We calculate the discriminant.
Lines 8–12: We find the real roots if the value of the discriminant is positive.
Lines 14–20: We find the complex roots if the value of the discriminant is negative.
Lines 22–25: We find the real root if the value of the discriminant is neither positive nor negative.
Line 28: We define the sample coefficients for our quadratic equation.
Lines 30–31: We check the value of a
to validate the quadratic equation.
Lines 32–34: If the equation is valid, then we call the find_DiscriminantAndRoots
function and print the output.
We can validate the roots by putting their values back in the original quadratic equation. Each root should satisfy the quadratic equation. Putting the values of the sample coefficients in the generic quadratic equation, we get the quadratic equation
Let's test our understanding with a short quiz
What is the general form of a quadratic equation?
In this answer, we learned about the quadratic equation, its general form, and how to solve it using quadratic formula in Python. We saw a step-by-step implementation of the code and then validated the roots by putting them in the original equation. We saw that both roots satisfied the equation.
Haven’t found what you were looking for? Contact Us
Free Resources