The complete guide to Polynomials | by Shaun Enslin | Towards Data Science

Need a brief and easy introduction to polynomials? Read ahead or watch this tutorial on youtube. You can also download the source code in this article from github.
The problem with linear regression is that data does not often go in a straight line. If we look at below data set, a linear function makes perfect sense.
Polynomials are defined by the formulae
The shape of the polynomial depends on the number of degree terms, and I have explained below, so as you can see, once you understand the shapes of the polynomials, you can start making calculated decisions on which degree it may be. Some of the degrees have names as seen below.
Blue = linear polynomial
Black = quadratic polynomial
Red = cubic polynomial
So, increasing the degrees of the polynomial, allows you to get the line to fit the data better. But, be careful, going to high also “overfits” the data and also does not give you an accurate prediction.
Now let us look at some other quadratic functions to see what happens when we vary the coefficient of x. We shall use a table of values in order to plot the graphs, but we will fill in only those values near the turning points of the functions.
You can see the symmetry in each row of the table, demonstrating that we have concentrated on the region around the turning point of each function. We can now use these values to plot the graphs.
Why dont you log into www.desmos.com and plot below polynomials. Try changing the coefficient and the constant terms to see the change in shape.
It’s important to understand how polynomials are factorized. This will then allow for an understanding later on how zero’s are calculated to indicate intercepts on the x axis. If you prefer to watch then see my youtube tutorial.
In fact, the process of factoring is so important that very little of algebra beyond this point can be accomplished without understanding it.
Example 1: Quadratic with a GCF
The easiest way to understand this is to do an example, let’s say we have the polynomial:
So, our first step is to see if we can find the greatest common factor (gfc). In this case, it would be 3, so this allows us to divide each term by 3. This results in:
Now, we need to factor 8 into 2 numbers multiplied together should equal 8, but added together, equal 6:
  8 / \-4 -2
So, -4 * -2 = 8, while -4–2 = 6Now that we have our -4 and -2, we can factorize our polynomial as follows:
F(x) = (x — 4) (x — 2)
Plugging a value for x in our factorized polynomial or the original, will give us the same result.
Example 2: Quadratic with no GCF
Now, what about when there is no gcf. Let’s take the polynomial:
Our first step is to multiply the 1st coefficient by the constant term.Ie. 2 * -3 = -6
Now, let’s find 2 numbers that multiply to -6, but also add to -5
  -6 /  \-6  +1
Next step is to expand our “- 5x” term to use above numbers. Keep in mindthat -6x + 1x = 5x.
So, lets break our 2 parts up
From the 1st red part, we can take out 2x, so 2x2–6x becomes
Our 2nd part, we can factor the 1 and get: 1(x — 3).So, we end up with
Great, lets factor out (x-3) and we end up with a factorized polynomial of
f(x) = (x-3) (2x + 1)
Example: Cubic polynomial factor by grouping
Dealing with quadratic polynomials is dealt with as above, so now, how do we deal with factoring a cubic polynomial.
Check if we can group by dividing the coefficients. In below case, both give us a value of 1.5, so we can factorize by grouping.
3/2 == -12/8 == -1.5
Group above into 2 terms and to factor them. Our GCF in the first term is x2 while the GCF in the 2nd term is -4.
Now, lets split up terms and we can factor out the (3x — 2)
This leaves a problem as we have a difference of perfect squares. We can correct this as follows by taking the square root of x2 and 4 to separate the term into 2 terms and we end up with our final factored polynomial.
f(x) = (3x -2) (x — 2) (x + 2)
What is a zero of polynomials?If you prefer to watch, then head over to youtube.
We need them only because we often use polynomials in our modelling of physical and other situations! The zero is very useful because it helps find the root. So, why find the root?
Well, let’s say that we know we have the problem,
So, what can x be?
The question becomes much more simple if we reduce one side to zero making it .
Now, we can plug it in to the quadratic formula and find x.
Of course, you may say that this only applies to quadratics. However, given any polynomial, if you make it equal to zero, it becomes much easier to find the roots.
So, to answer your question, we don’t actually need the zeros, however they are really, really convenient!
If we take the following factorized polynomial:P1(x) = (x-1)(x-2)(x-3)
Well, then any value of x where the above equation = “zero”.So any time any of the above 3 expressions are zero, then naturally the product of all 3, will also be zero
After factoring, this becomes easy enough to deduce and as we can see that when x is either 1, 2 or 3, the result of P1(x) will be zero.
Lets take another example that we can graph.
We can see from above, that our our points where each of the 3 terms will be zero, will be when x = [-2, -3/2, 2]. When we plot this polynomial, it is also evident in the graph.
I trust that clears up zero’s and these can now be seen in the next article when we discuss the multiplicity of a polynomial.
Briefly explained, multiplicity is the amount of times the polynomial crosses the x axis. If you prefer to watch, head over to youtube.
Lets take the following two polynomials which are already in factor form.
So, in our next step, if we want to figure out the multiplicity of the polynomial, we can see how many individual zero terms we have. In above examples. Our first step as below is to work out our zero values as indicated under each term.
So, if we create a table, we can now enter in the multiplicity column, how many zero terms we have for that zero x value.
If the multiplicity is odd, then we do have a sign change, while if the multiplicity is even, then we do not have a sign change. This tells us that:
For P1, since all multiplicities are odd, every time it hits the x axis, it changes sign.For P2, however, when it hits 3, there is no sign change, since multiplicity is even.
For P1, since all multiplicities are odd, every time it hits the x axis, it changes sign.
For P2, however, when it hits 3, there is no sign change, since multiplicity is even.
You can see this in action now below as each time P1 touches the x axis, it crosses from negative to positive or visa versa.
P2 however, does cross from negative to positive where x=1, but where x=3 it bounces off and stays positive.
It’s time we get to some coding, buckle up, lets give this a go...Source code available on github.If you take the following polynomial:
Now, if we look at the following x values into above equation, we will then get the following y results.
You can now start up Matlab and create your x and y vectors as below. Then run the polyfit command, entering 2 in the final parameter, since we are working with a quadratic function.
x=[0,1,2]y=[5,3,3]polyfit(x,y,2)
Running above, you should get a coefficient as below. So, perfect, from our data, polyfit found the correct polynomial coefficent.
ans =1.0000 -3.0000 5.0000
So, that was a nice basic example, but lets try something a little more complicated....
7.1 Generate sample data
In this example, we don’t have a dataset, but that’s OK, we can have Matlab generate some sample data for us to suite our quadratic. First, lets define our quadratic function as below and call it f.
clc;clear;close all;% declare our quadratic polynomialf = @(x) 2*x.2 — 4*x + 5;
Now, lets generate some sample x values using the unifrnd function. We are adding some variations to the y values with the + 1.5*randn(size(x)). After all, we dont want the data too perfect ;-)
% Create x by getting random uniform values between -1 and 5x = unifrnd(-1, 5, [100,1]);% Calculate y by running the polynomial but add some variationsy = f(x) + 1.5 * randn(size(x));
Plot the data points so we can visualise the data
% plot the data pointsfigure;plot(x,y,’rx’);
After executing, you will see we have generated a nice dataset to compliment our quadratic.
7.2 Find our best coefficient
Now that we have our data, lets run a polyfit on a quadratic to find the best coeffecient. We are choosing a quadratic as we can see there is one turn in the data at around ‘1’ on the x axis, thus the shape fits a quadtratic.
% find the best coeffeciant[p,S] = polyfit(x,y,2);
Se now have our best coefficient in ‘p’ and can go ahead and plot a line for our coeffeciant using polyval.
xx = linspace(-1, 5, 100);       % generate even spaced xx[yy, delta] = polyval(p,xx,S);   % Now get the y for xxhold on;plot(xx,yy);
After executing, your coefficient should be plotted
7.3 Plot our support/resistance lines
A nice little feature is that we can use the delta returned from the polyval to plot out our support and resistance lines for our polynomial. These are plotted to give us a 90% confidence that the data will fall in these ranges.
% Lets use our delta to plot our support and resistance linesplot(xx, yy + 2*delta, ‘b:’);plot(xx, yy — 2*delta, ‘b:’);
After reading this article, you should be able to:
Explain what polynomials are
Factorize polynomials
Calculate the zeros
Understand multiplicity of a polynomial
Finally, we have build a good example in Matlab creating data which has the shape of a quadratic polynomial. This now give you your hypothesis to predict Y, based on the X values.