<![CDATA[Category: linear algebra | /usr/sbin/blog]]> 2012-09-15T22:52:07-06:00 http://usrsb.in/blog/ Octopress <![CDATA[Fibonacci Sequence, Part II]]> 2011-08-03T16:49:00-06:00 http://usrsb.in/blog/blog/2011/08/03/fibonacci-sequence-part-ii The matrix equation in the last post can actually be whittled down a bit further to produce another equation that, in some ways, is easier to work with. The result is as follows:

fib

Fk is, of course, the kth Fibonacci number. Now, before I go on, I want to point out that I stumbled upon these two equations and the proofs for these equations in Gilbert Strang's excellent Linear Algebra and Its Applications. All credit goes to him for coming up with this, and I recommend his book for an even more in depth explanation.

Anyway, back to the equation, which I found interesting for a few reasons:

  1. This equation allows you to easily find the kth term using only a pocket calculator. The last equation requires something that can deal with matrices.
  2. This also makes it easier to use in programming applications. There's no need to write functions or import libraries to deal with matrices. It's also probably faster than writing some sort of recursive function to compute the kth Fibonacci number.
  3. As Strang points out, that equation, amazingly, produces an integer, despite all the fractions and square roots.
  4. We can simplify that equation further for a very good approximation (so good that it's not really an approximation).

To see how we get that equation from the equation in the last post, let's begin with the way Mr. Strang has it written in his book (it's the same as mine, but flipped upside down).

title

Where F0=0, F1=1, F2=1, etc. Let's now make some substitutions:

title

We now have:

title

This hasn't changed the content of the equation at all. We've just substituted in new symbols to represent the different terms.

The next step is to diagonalize the matrix A. Remember that diagonalizing A produces A = SΛS-1 where S contains A's eigenvectors and Λ contains A's eigenvalues. Substituting A = SΛS-1 into the previous equation yields:

title

Notice how everything but the first and last S and S-1 cancel, giving the final form: SΛkS-1u0. This is important, because Λ is a diagonal matrix, making Λk very simple:

title

λ1 and λ2 are, of course, the eigenvalues of A. Let's now make one last substitution and put all this diagonalization stuff together. First we define c as

title

Then we substitute it into the equation:

title

That might need some explaining. The first bit is simply the equation with c substituted in. Following that, the first matrix is S, but written to show that it contains x1 and x2, which are the eigenvectors of A placed vertically in the two columns. The middle matrix is Λk and the last is simply the matrix c. Finally, the matrices are multiplied out, yielding the final c1λk1x1+c2λk2x2

The final steps are simply to compute c, and A's eigenvalues and eigenvectors. I'll spare you the tedious algebra and simply tell you that:

title

title

This can be computed the standard way, by solving: det(A-λI)=0.

All the variables are then plugged into the equation for uk.

title

We want Fk, so we multiply, factor, and take the bottom row, giving the equation we want:

title

That's the full equation, but now notice that the second term is always less than 1/2. This mean we can simply drop it, yielding:

In fact, since the second term is always less than 1/2 and the full equation always gives us an integer, we can take this a step further: Rounding the approximation to the nearest integer will always give you the exact value for Fk. Cool.

∞ Permalink

]]>
<![CDATA[Using Matrices to Generate the Fibonacci Sequence]]> 2011-04-10T12:47:00-06:00 http://usrsb.in/blog/blog/2011/04/10/using-matrices-to-generate-the-fibonacci-sequence Here's a fun little matrix:

Fib Matrix

That generates the an and an+1 terms of the Fibonacci sequence.1 To see why, let's look at a recursive definition of the Fibonacci sequence.

Fib Def

Fib Def

That's easy enough to understand. The first and second terms are both 1, and the next term is the sum of the last term plus the current term. To see how this relates to the matrix above, we must turn this into a matrix equation. The trick here is to have a good understanding of matrix multiplication. If you need a refresher, here's a site with a tutorial. Let's look at an example of a 2x2 matrix multiplied by a 2x1 (even if you have a good understanding of matrix multiplication, hang tight, this is going somewhere).

title

Now let's make some substitutions and multiply it out:

title

As you can see, the effect of multiplying the b matrix by the Fibonacci matrix is that it moves the b1,2 position to the b1,1 position, and the b1,2 gets filled with the sum of b1,1 and b1,2. Let's now substitute the b's for an-1 and an:

title

Does the resultant matrix look familiar? If you look above, you'll see that it's the same as our recursive definition of the Fibonacci sequence! The top row is equal to the current number in the sequence (an) and the bottom row is equal to the next number in the sequence (an-1 + an = an+1). Continuing to multiply the resultant matrix by the Fibonacci matrix will cause consecutive entries to be produced. Because matrix multiplication is associative, we can move our multiplication to the exponent, and multiply that result by the first two terms in the sequence (0, 1), leading to our initial matrix:

Fib Matrix

References

1: Strang, Gilbert. "Linear Algebra and Its Applications."

∞ Permalink

]]>