In this article we will discuss how to solve a quadratic equation using Python.
Table of Contents
Introduction
Quadratic formula
Solving quadratic equation using Python
Complete code
Conclusion
Introduction
In algebra, quadratic equations are widely used in a lot of tasks. A quadratic equation (second-degree polynomial) always has a squared term which differentiates it from our usual linear equations.
In this tutorial we will use the math library (prebuilt in Python) for solving quadratic equations using Python.
Quadratic formula
We begin with understanding the standard form of quadratic equation:
$$ax^2 + bx + c = 0$$
where a, b, c are real numbers and (a neq 0).
So how do we know if the equation has a solution? And if it does, how many solutions?
Step 1: Calculating the discriminant
The first step to solve a quadratic equation is to calculate the discriminant. Using simple formula:
$$D = b^2 – 4ac$$
we can solve for discriminant and get some value. Next, if the value is:
positive, then the equation has two solutions
zero, then the equation has one repeated solution
negative, then the equation has no solutions
Step 2: Solving for x values
To solve for each x value, we use the following quadratic formula:
$$x = frac{-b pm sqrt{b^2 – 4ac}}{2a} = frac{-b pm D}{2a}$$
which means that:
$$x_1 = frac{-b + D}{2a}$$
$$x_2 = frac{-b – D}{2a}$$
and these are all the steps we need to take to find the solution for the quadratic equation.
Solving quadratic equation using Python
As an example, let’s consider the following quadratic equation:
$$x_1 – 5x_2 – 14 = 0$$
where (a = 1), (b = -5), and (c = -14).
Step 1: Get user input for a, b, and c coefficients
First, we need to get these coefficients entered by the user:
a, b, c = eval(input(“Please enter the a, b, c coefficients of your quadratic equation: “))
Here we will need to pass the three comma-separated values as: 1,-5,-14.
Clearly, if you pass anything other than a real number (string or boolean), it won’t break the input function but further calculations won’t work. To prevent this, you should consider adding a set of checks to validate the user input:
check_input = True
while check_input:
a, b, c = eval(input(“Please enter the a, b, c coefficients of your quadratic equation: “))
try:
float(a), float(b), float(c)
check_input = False
except ValueError:
print(“Please make sure the coefficients are real numbers and try again”)
check_input = True
So far we created the a, b, and c variables in Python.
Step 2: Calculate the discriminant
Next we will calculate discriminant. We will need to use the math library (prebuilt in Python) to use the square root function:
from math import sqrt
disc = sqrt(b*b-4*a*c)
For the values we entered above, the discriminant value should be 9.
Step 3: Find roots of the quadratic equation with quadratic formula using Python
And finally we solve for the roots of the equation. Recall that we also need to check if the discriminant is less than zero, then the quadratic equation has no solutions:
if disc >=0:
x1 = (-b+disc)/(2*a)
x2 = (-b-disc)/(2*a)
print(“The roots of the equation are:”, x1, x2)
else:
print(“The equation has no solutions”)
For our example we should get (x_1 = 7) and (x_2 = -2).
Complete code
from math import sqrt
check_input = True
while check_input:
a, b, c = eval(input(“Please enter the a, b, c coefficients of your quadratic equation: “))
try:
float(a), float(b), float(c)
check_input = False
except ValueError:
print(“Please make sure the coefficients are real numbers and try again”)
check_input = True
disc = sqrt(b*b-4*a*c)
if disc >=0:
x1 = (-b+disc)/(2*a)
x2 = (-b-disc)/(2*a)
print(“The roots of the equation are:”, x1, x2)
else:
print(“The equation has no solutions”)
Conclusion
In this article we covered how you can solve a quadratic equation using Python and math library.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Optimization articles.
The post Solve Quadratic Equation using Python appeared first on PyShark.
