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. Show
In this tutorial we will use the math library (prebuilt in Python) for solving quadratic equations using Python We will also need numpy to work with arrays, and matplotlib to plot the graph of the quadratic equation using Python. If you don’t have it installed, please open “Command Prompt” (on Windows) and install it using the following code:
Quadratic formulaWe 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? Solve quadratic equationStep 1: Calculate discriminantThe 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:
Step 2: Solve for x valuesTo solve for each x value, we use the following quadratic formula: $$x = \frac{-b \pm \sqrt{b^2 – 4ac}}{2a} = \frac{-b \pm \sqrt{D}}{2a}$$ which means that: $$x_1 = \frac{-b + \sqrt{D}}{2a}$$ $$x_2 = \frac{-b – \sqrt{D}}{2a}$$ and these are all the steps we need to take to find the solution for the quadratic equation. Solve quadratic equation (Example)As an example, let’s consider the following quadratic equation: $$x^2 – 5x – 14 = 0$$ where \(a = 1\), \(b = -5\), and \(c = -14\). As the first step, we will calculate the discriminant: $$D = b^2 – 4ac = (-5)^2 – 4 \times 1 \times (-14) = 81$$ Since \(D > 0\), this equation will have two roots. Let’s calculate \(x_1\) and \(x_2\): $$x_1 = \frac{-b + \sqrt{D}}{2a} = \frac{-(-5) + \sqrt{81}}{2 \times 1} = 7$$ $$x_2 = \frac{-b + \sqrt{D}}{2a} = \frac{-(-5) – \sqrt{81}}{2 \times 1} = -2$$ The solution for this equation is: \(x_1 = 7\) and \(x_2 = -2\) Solve quadratic equation using PythonAs 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 equation coefficients (a , b, c)First, we need to get these coefficients entered by the user:
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:
So far we created the a, b, and c variables in Python. Step 2: Calculate discriminant using PythonNext we will calculate discriminant. We will need to use the math library (prebuilt in Python) to use the square root function:
For the values we entered above, the discriminant value should be 81. Step 3: Find roots of quadratic equation using PythonAnd 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:
For our example we should get \(x_1 = 7\) and \(x_2 = -2\). Plot quadratic function using PythonThe graph representation of a quadratic equation is a parabola. When the quadratic equation is simplified to be equal to zero, the roots of the equation are the y-axis intercepts. Let’s plot the quadratic equation from the previous section using Python and see where the roots of the equation are located. We begin by importing the required libraries:
Next let’s determine the \(x\)-axis range we would like to consider. For this example, let’s take a range between -10 and 15:
And we can have our function for \(y\) using the parameters from the previous section:
First we will plot the x-axis and y-axis as dashed lines along with the axis labels:
And then plot the parabola:
And you should get: On the graph above you can see that points (-2, 0) and (7, 0) are the x-axis intercepts which are also the roots of the quadratic equation. Program to solve quadratic equations using Python 0ConclusionIn 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. How to do quadratic formula Python?The standard form of the quadratic equation in python is written as px² + qx + r = 0.. If b*b < 4*a*c, then roots are complex.. If b*b == 4*a*c, then roots are real, and both roots are the same.. If b*b > 4*a*c, then roots are real and different.. How do you write the quadratic formula?The quadratic formula helps us solve any quadratic equation. First, we bring the equation to the form ax²+bx+c=0, where a, b, and c are coefficients. Then, we plug these coefficients in the formula: (-b±√(b²-4ac))/(2a) . See examples of using the formula to solve a variety of equations.
How to write an equation in Python?Equations. In [1]: from sympy import symbols, Eq. x = symbols('x') eq1 = Eq(4*x + 2). In [3]: x, y = symbols('x y') eq2 = Eq(2*y - x, 5). In [4]: x, y, z = symbols('x y z') eq2 = Eq(2*y - x - 5) eq3 = eq2. subs(x,z) eq3. Out[4]: Eq(2*y - z - 5, 0). |