Solving Polynomials with iPython

College Algebra @ Seminole State College

Yesterday, I was asked to help a friend with some College Algebra homework. She attends Seminole State College and is having a bit of trouble solving polynomial equations.

If you recall from your own College Algebra notebook, a polynomial equation is defined as:

In mathematics, a polynomial is an expression consisting of variables (or indeterminates) and coefficients, that involves only the operations of addition, subtraction, multiplication, and non-negative integer exponents.

Yeah, you remembered that right?

On paper, a polynomial is expressed like this...

f(x) = 3x**2 + 4x**4  

In this example, the leading coefficient (4) is a positive integer and an even value. Our simple graph rises on the left and rises on the right like a stretched out U shape.

We spent an hour together working on evaluating these equations and plotting a few graphs. I thought we made some good progress.

I thought about our homework session A LOT on my ride home. I knew Python could be used to plot polynomials. I should have introduced her to iPython while I was there. Darn. Opportunity missed.

Once I got home and cracked open my trusty Surface Pro 3, I fired up iPython (a.k.a. Project Jupyter).

Oh, Hello, Numpy and Matplotlib!

After some quick research and a few attempts in my iPython notebook, I was generating some very nice plots of polynomials using numpy and matplotlib.

This is where iPython really shines. Data and scientific research.

And college math homework ;-)

In [ ]:

from numpy import *  
from matplotlib.pyplot import *

x = linspace(-1, 1, 100)

y = (-3*x**2 + 4*x**4)

# make a new figure

Out [ ]:

<matplotlib.figure.Figure at 0x1412e48>  

In [ ]:

# plot some data
plot(x, y)  

Out [ ]:

[<matplotlib.lines.Line2D at 0x2fe8128>]

In [ ]:

# show thin grid lines

# save the image

The image is saved to the directory in which you are running your iPython session.

And the plot from our function:

My Polynomial Equations Jupyter Notebook

Google also has a very nice polynomial factoring and plotting function baked right into Google Search.

Go to and enter:

f(x) = -3x**2 + 7x**3  

What does Google tell you?

Disclaimer: It only helps you learn if you use the search results data provided to then solve the equation yourself. Using Google to do your homework is ill-advised.

Now that I have stated the obvious...

Google gives you the the solved equation, the grid, the plot with the x and y intercepts and the ability to zoom in and out of the plot.

It's pretty freaking cool, nerds!

The only question I have now is...

Where the hell was the iPython, Google and/or the Internet when I was learning College Algebra?

Oh, yes... Of course, you're right. That was 1988.

edit: I found another polynomial graphing tool online that is simple to use.
Graph Sketch

edit #2: I am now using Anaconda as my default interpreter with iPython. It includes many of the math and scientific modules and there are no other tools or modules that need to be installed. It rocks.

Craig Derington

Secular Humanist, Libertarian, FOSS Evangelist building Cloud Apps developed on Red Hat Enterprise Linux and Ubuntu Server. My toolset includes Python, Celery, Flask, Django, MySQL, MongoDB and Git.

comments powered by Disqus