Build Your Own Quadratic Equation Solver in Python

Rhett Allain
The Startup
Published in
5 min readAug 12, 2020

--

Oh, sure — you can do a quick Google search and find a bunch of quadratic equation solvers online. But wouldn’t it be more fun to just build one yourself? Yup. I’m going to show you how to do that.

What is the Quadratic Equation?

Suppose you have a some quadratic function. Maybe it looks like this.

Now, you want to find the values where f(x) = 0, so you get the following.

What do you do next? Well, you could try to factor this to isolate x, but sometimes that just doesn’t work out so well. Another option is to just plot the function and see where it crosses the x-axis.

Solution by Graphing

Here’s what that would look like (code here in python if you want it).

I put two red points where the function crosses the x-axis. Since I’m just getting the values by inspecting this graph — it’s not perfect. But this gives two x-values at about -1.27 and 2.76. That’s not too bad.

The Quadratic Equation

Actually, the quadratic equation isn’t something new. It’s just a generic solution to factoring a second order polynomial. Suppose your basic function looks like this.

--

--

Rhett Allain
The Startup

Physics faculty, science blogger of all things geek. Technical Consultant for CBS MacGyver and MythBusters. WIRED blogger.