Sage: Polynomial Interpolation

In an earlier post I mentioned that it's not that uncommon for math teachers and even education professionals making up academic resources to mess up badly on sequences. For example, the IXL site, which is, in general, an excellent resource for teachers and students makes a common error with sequences both here and here. Experiment with the problems that are randomly created at those 2 links. Try entering a "wrong" answer and you'll get an explanation of what the "right" answer is. But the "logic" they're using is that here's a formula that describes the sequence, therefore the next term is... The problem is that if you follow the same logic you can justify the missing terms of the sequence to be whatever you want. Therefore those problems have no correct answer and should not be given. Moreover, they shouldn't mark other answers as wrong.

Whenever teachers and math professionals are wrong on the math, you've got a teachable moment. This lesson would be for someone teaching about matrices who has gotten through reduced row echelon form. I'll suggest that you start a class by working through the material on the IXL website and have them figure out what the terms of the sequence are. Write the various sequences on the board to refer to later. When they've gotten comfortable ask them what will happen if you put in a term that doesn't seem to fit the pattern. They'll predict that you will be marked wrong. After showing them that you are indeed marked wrong by IXL figure out a formula to justify your answer and have students confirm that it works. You now have a formula that justifies a sequence that IXL was marking you wrong for. Get the class to discuss what it must mean for an answer to be correct (you can find a formula) and for what it means to be incorrect (it's impossible for anyone to find a formula).

Now it's time for math! Elicit that a sequence is a function from the positive integers into the real numbers (or integers, depending on how you teach it). Remind them that this means the sequence -5, 3, 11, 19, 27, ... corresponds to the function where f(1)=-5, f(2)=3, f(3)=11, f(4)=19, f(5)=27. Give them the mathematics known as polynomial interpolation or Lagrangian interpolation, and using one of the IXL sequences written on your board, set up a Vandermonde matrix. Work through the mathematics to create the polynomial. Get those calculators out to solve the matrix equation. And at the end you'll have a polynomial which they'll need to confirm works to generate the sequence. By the end of class your students will have learned about Vandermonde matrices, seen the math behind polynomial interpolation, used the calculator to power through some of the calculations and construct a polynomial that shows that even the "experts" get things wrong. That's a lesson the class will remember long after they've forgotten how to do the math.

But it's easy to make mistake with calculations so I've constructed a Sage program to go through the steps to create an interpolating polynomial using Vandermode matrices. It's posted on the Python/Sage page. A little warning is necessary, however. Usually you just go to a SageCellServer (or SageSandbox on this site) and copy/paste the code and press "Evaluate" to get the code to run. For reasons I don't understand, sometimes you get some sort of I/O Error such as the one below.

That error can pop up at various stages in compilation. The code runs, sometime it's just a matter of pressing "Evaluate" several times.

In order to set the code for your sequence, you need to alter xvalues and yvalues in the code. For example, if your sequence is 3,2,_,0,-1,-2, _,... and someone says that the pattern is to subtract 1 from the previous terms so the missing numbers are 1 and -3 then you'll need to pick the values you want in your sequence. If you choose to complete the sequence as 3,2,5,0,-1,-2,11,... then you'll need to set xvalues = [1,2,3,4,5,6,7] because you have 7 terms in your sequence and yvalues = [3,2,5,0,-1,-2,11] because those are the terms of your sequence. Press "Evaluate" and you'll get a polynomial that goes through those points. So there is a formula for your sequence--it's just not obvious to most people.

Here are some stories that caught my eye this past week: