Archive for November, 2009

Math: The Rabbit Hole – Sin, Cos and Derivatives

November 30th, 2009

Something I do recall from high school calculus class is that the derivative of sin x is cos x. This is one of those little nuggets of information that really helps you through bigger, tougher equations. But why is this so? Down the rabbit whole we go.

First, we need to define what a derivative is (again). Its just the slope of a line at a given point. Slope is just rise over run, or the change in y or the change in x: \dfrac{dy}{dx} or \dfrac{\bigtriangleup y}{\bigtriangleup x}. In simple geometric terms this is pretty easy. Let’s go back to our simple y = x line:

dydx1

Using just a paper and pencil we can figure out the slope of this line at any given point. We can probably come up with the slope of any line at any other given point, too. In this example finding the first derivative, and its meaning, are very easy. But how do we do it with tougher examples? Well, lets look at the idea of finding the rate of chang of x at a given point. This is tough to do. We really need two points to find the slope. How do we pick another point? On a tight curve, using our point at x and another point will probably give us a false (but perhaps close) slope. What we need is another point that is infinitely close to x. Let’s say we have x and another value, x + h. Our equation to figure out the slope based on these two values would look like:

\dfrac{f(x+h) - f(x)}{h}

This is pretty straight forward so far. Our value h needs to be very, very small to give us an accurate slope. In fact, to give us a precise slope it has to be approaching zero. So what if we create a limit where the difference in our points is disappearing?

\lim_{h \to 0}  \dfrac{f(x+h) - f(x)}{h}

This is pretty and all, but it doesn’t really get us anywhere. Remember, we are trying to find some general rules to find derivatives. Here we need to take another “guess” at trying to find an equation we can simplify. Our guess is going to be to use f(x) = x^2 as a starting point. We do this because we want do be able create multiple terms and hopefully find a way to get rid as much junk as possible.

#1 – Starting equation: f(x) = x^2

#2 – Plug into our slope formula: \dfrac{(x + h)^2 - x^2}{h}

#3 – Expand: \dfrac{(x + h)(x + h) - x^2}{h}

#4 – Multiply: \dfrac{x^2 + h^2 + 2hx - x^2}{h}

#5 – Add (or subtract):\dfrac{ h^2 + 2hx}{h}

#6 Divide by h: 2x + h

So we have a general formula that if f(x) = x^n the f'(x) = nx^{n - 1}. For our dead-simple line f(x) = x, the derivative is f'(x) = x^0 or 1.

If you recall, we started with the derivative of sin x being cos x. So let’s plug and chug. It’s calculus, but it looks like algebra!

#1 – Starting equation: f(x) = sin x

#2 – Slope equation with sin plugged in: \dfrac{sin(x + h) - sin(x)}{h}

#3 – Trig: sin a - sin a = 2 sin 1/2 (a - b) cos 1/2 (a + b), which simplifies to \dfrac{2 cos (x + h/2) sin (h/2)}{h}. I had to look all of this up, which is why I titled this post the rabbit hole.

#4 – Pull h/2 over to sin: 2 cos (x + h/2) \dfrac{sin (h/2)}{h}

#5 – Take the limit as h approaches 0. The limit of a product is the product of the limits. All of the h/2 elements go to 0, basically leaving cos(x).

To figure this out I had to relearn some trigonometry, the basic theorem of derivatives, and the limit product rule. Math is hard; let’s go shopping!

Math: The Taylor Series

November 30th, 2009

In yesterday’s post Not-so Easy e, I briefly covered The Taylor Series and how it is applied to solving for the value of e^x, or for specifically finding the value of e. I didn’t spend a lot of time covering The Taylor Series, and skipped some steps on my path to deriving the value of e. I figured I should take some time to go back over Taylor, show why it was chosen, and how it fits in with e.

The Taylor Series has a rich history in mathematics, and it is used to solve a number of classic problems. The Taylor Series is defined in Taylor’s Theorem. If a function (we used f(x) = e^x yesterday) is differentiable n times on a closed interval [a,x] and n + 1 times over the open interval (a,x), and n \ge 0, the function can be precisely approximated by The Taylor Series.

f(a) + \dfrac{f'(a)}{1!}(x-a) + \dfrac{f''(a)}{2!}(x-a)^2 \cdot\cdot\cdot \dfrac{f^{(n)}(a)}{n!}(x-a)^n

Math note: The open interval (a,x) includes all values between a and x exclusively, and the closed interval [a,x] includes all values between a and x inclusively.

I am not going to go through the whole theorem here, but I will show in more detail how it is applied to e^x. Lets go through step by step.

#01 – Original equation: f(x) = e^x

#02 – First Derivative: f(x)' = e^x

#03 – Taylor: f(a) + \dfrac{f'(a)}{1!}(x-a) + \dfrac{f''(a)}{2!}(x-a)^2 \cdot\cdot\cdot \dfrac{f^{(n)}(a)}{n!}(x-a)^n

#04 – What value a should we use that is near x? We will use 0, which will produce a value of 1 when the function f(a) = e^a.

#05 – Substitute in e^x derivatives: 1 + \dfrac{1}{1!}(x-0) + \dfrac{1}{2!}(x-0)^2 \cdot\cdot\cdot \dfrac{1}{n!}(x-0)^n

#06 – Obvious: e^x = 1 + x + \dfrac{1}{2}x^2 \cdot\cdot\cdot \dfrac{1}{n!}x^n

#07 – Finished: e^x = \sum_{n=0}^{\infty}\dfrac{x^n}{n!}

I have stumbled over uses for The Taylor Series from time to time working as a software engineer. Its pretty easy to compute a value to an arbitrary precision using a loop. In the practical world an approximation is good enough, and even a loop that is run 100 times with most math should return almost immediately on modern hardware. Apparently this is how scientific calculators do a number of computations. Very cool.

Math: Not-so Easy e

November 29th, 2009

In mathematics, something of interest is the slope of a graphed equation at a give point on the line. The slope is defined as rise over run. For straight lines the slope is easy:

x = y

f(x) = x

The line is defined as f(n) = x, which just means if we set x to 0 then the result is 0, x to 1 then the result is 1. This straight line has a slope of 1/1, or 1. If it rises one unit, it runs one unit. This is pretty straight forward, but how do we determine the slope for a curved line like the one below?

exp

f(x) = a^x

The equation f(x) = a^x is generating the line where a is a constant raised by variable x. The important thing to take away is that we have a curve (in blue), and on a specific point we want to know the slope (in red). There are various ways to determine the slope. The simplest way in a simple case like the one illustrated is to just draw the curve on some graph paper, then guess the slope by drawing it like we see. This isn’t very fancy, and isn’t necessarily very accurate, but it does work well for simple situations. Luckily math comes to the rescue to give us a more accurate answer, and provides a method that works in more complex situations. Modern Calculus (invented independently by some guy named Issac Newton and another guy named Gottfried Wilhelm Leibniz) can find the slope using something called a derivative. I am not going to dig into derivatives here; I am going to cheat a little. I learned derivatives in high school calculus and used them in both high school and college math and science. I know I am blogging about a calculus class I am taking so that isn’t fair. For our discussion we only need to know that there is a transformation that will take an equation and (transformer sounds here) turn it into an equation for the slope at a given point. I will be providing that transformation shortly.

The point of this blog post really isn’t just about slope, its about a special constant known as e. This constant is one of the most important constants in math for a variety of reasons, almost as important as \pi. The constant e has two very unique qualities. First, the derivative of e^x is e^x! What we reallycare about here is that for the general function f(x) = a^x, the only value for a that produces the slope of 1 where x is 0 is e. That figure above is actually the graph of f(x) = e^x. So, what the hell is e? Its a constant value that starts 2.71828182845904523536*. The next question we should ask is, how the hell do we get this number?

An option that is provided by the online textbook of my class is to try to create an infinite series to estimate this value. This didn’t jump out at me as the obvious way to discover the value of e. I thought, “Why the heck is the prof pointing towards this crazy solution?” There was no explanation, so I went searching for why one would use an infinite series to solve for anything, let alone this. I found this interesting web page explaining that infinite series expansions are a rather useful tool in math. What I discovered is that the prof was suggesting the use of something called a Maclaurin Series to find the value of e.

We start with x^n, which we will use to generate our series. Using Maclaurin, the series is:

e^x = a_0 + a_1x + a_2x^2 + (etc)

We know that in the original formula f(x) = e^x that that value will be 1 when x is 0. When you put this in context of the series, a_0 = 1 if we start with 1 as x value.

When we differentiate the series, we get:

\dfrac{d e^x}{dx} = a_1 + 2a_2x + 3a_3x^2 + (etc)

This sets up an interesting equality. The derivative of e^x can be proven to equal e^x as stated above. So we can match up these two equations and see that a_0 = a_1, and 2a_2 = a_1, 3a_3 = a_2, a_1 = 1/2, etc. In general this boils down to each position being:

\dfrac{x^i}{i!}

So to sum the whole series we get:

\sum_{i=0}^{\infty}\dfrac{x^i}{i!}

Plugging 1 into x yields the sum to find e. You can see that is one of the common equations for e here.

Again, understanding a fundamental value of math requires some work. There are others ways to solver for e, but this is the one that seemed to make the most sense for me. I thought of going through the proof here for why \dfrac{d e^x}{dx} =  e^x, but it involved even more math external to the problem at hand and probably would have doubled the length of the post.

Book Review: The Crucible of Consciousness

November 28th, 2009

The Crucible of Consciousness

I just finished reading The Crucible of Consciousness: An Integrated Theory of Mind and Brain by Zoltan Torey. The book deals with the concept that we call “consciousness” or “the mind”: what it is, how it works, and how we evolved to have this amazing ability.

Torey approaches the subject based purely on the evidence at hand. While at times this read requires a dictionary for those like myself without a background in psychology or neurology, the book is accessible enough to the lay person to allow a trek into the mechanics of the brain. If one takes the time to plow through some of the more dense areas, the reward is a more thorough explanation later in each section and chapter.

Torey’s central theme is that human evolution slowly began developing brain structures to help with a number of tasks before consciousness came into play. These specialized brain regions helped with a number of tasks including simple naming communication, such as “lion” or “danger”. In time mankind developed speech-thought, which gave these enhanced areas of the brain the ability to form a framework with which to achieve self-reflection or consciousness. At times Torey glosses over the underlying science that supports his theory and instead references other papers and books. In this respect the book can seem slightly out of reach to the layman that has not read the other seminal works in cognitive science. At other times he dives into some detail, rewarding the reader with real insights into the science supporting his claims.

Near the end of the book Torey drifts away from his hard-science approach and speculates on a number of subjects related to consciousness. It was this part I enjoyed the least. While he clearly states that these are just speculations, I feel that he drifts far off target and perhaps frames some of the science incorrectly, particularly his characterizations around quantum mechanics.

All and all I really enjoyed this book. It offers a wonderful insight supported by real science as to what we are as conscious beings.

Math: The Golden Ratio

November 27th, 2009

The Golden Ratio is an interesting concept in mathematics. The ratio is found in a number of places throughout nature. For example, the ratio has been found in see shells and pineapples. It is common in architecture. But the Golden Ratio is rooted in mathematics, where it can be derived a number of ways including from a Golden Triangle. Someplace I had forgotten that it occurs is in the Fibonacci Sequence, which actually appears to be one of the most fundamental sources of the ratio. In case you had forgotten about the Fibonacci Sequence or Numbers, they start like this:

1, 1, 2, 3, 5, 8, 13, 21, 34, etc.

The pattern works like this: start at 1, and then for each number take the current number + the number before it to create a new number. So in the Fibonacci Sequence, every number is the sum of the two previous brother numbers. Pretty straight forward, huh?

Where it gets interesting is when you divide each number by its previous number. The results look like this for the first few numbers:

1 : 1/1 = 1
2 : 2/1 = 2
3 : 3/2 = 1.5
5 : 5/3 = 1.6*
8 : 8/5 = 1.6
13 : 13/8 = 1.625
21 : 21/13 = 1.61538461538462
34 : 34/ 21 = 1.61904761904762

You can view a longer version of this at a Google Doc Spreadsheet I created. You will need a Google account to view it.

See that number starting 1.618? That is the golden ratio (or an approximation). The Fibonacci Sequence division scheme I have presented here converges on the ratio. So how do we ever discover the “real value”? Well, there are several ways. One way is to use a limit. But I found that the lesson plan on this showed a very interesting, purely algebraic way of doing it.

The Golden Ratio value for each number, lets call it r for “ratio”, is based on the division of any number via its previous number. If we write this formula out it is r = f(n) / f(n -1), if n is the number and f is the function that creates a Fibonacci number. Another way of writing this is f(n) = r f(n-1). We can take any given n-1, put it into the function, then multiple it by r, and we will get the result of f(n). This part was pretty easy for me, too. The Fibonacci Sequence tells us that every number is the sum of its two previous brothers, so we can also state f(n) = f(n-2) + f(n-1). So far we have two identities:

#1: f(n) = r f(n-1)
#2: f(n) = f(n-2) + f(n-1)

The goal here is to solve for r. This is the tough part. This stumped me, but I got some help from Reddit Math. The key here is to make an educated guess that we can do some substitution here that will get us the quadratic equation (which we can easily solve). Looking at the equation, we can see that there might be a path to this type of equation, and if we have a quadratic equation then we can solve for r. So how do we get here from there?

The first thing to note is that r f(n-1) is the same value as it’s previous brother times r. In other words, r f(n-1) is the same as r ( r f(n - 1 -1)) or r^2 f(n-2). OK, now we are closer to a quadratic equation, since we have a power of two. The first equation can now be written as f(n) = r^2 f(n-1). Now we are rocking! We can now see in our soup of equations that we have all the parts of a quadratic equation, except one part. We still have an “n-1″ floating around and everything else is an “n-2″, and we need the same variable across everything to do a quadratic equation. But we already know f(n-1) is the same as r f(n-2)! Here are those two identities again, with both transformed:

#1: f(n) = r f(n-1)
#1a: f(n) = r^2 f(n-2)
#2: f(n) = f(n-2) + f(n-1)
#2a: f(n) = f(n-2) + r f(n-2)

We can now set 1a and 2a as equal, since they both equal f(n):

r^2 f(n-2) = r f(n-2) + f(n-2) or r^2 f(n-2) - r f(n-2) - f(n-2) = 0 or r^2 - r - 1 = 0

And that is a quadratic equation. Given a quadratic equation ax^2 + bx + c = 0, then one may solve it with:

x = \dfrac{-b \pm \sqrt{b^2 - 4ac}}{2a}

When we plug that in it looks like:

r = \dfrac{-1 \pm \sqrt{5}}{2}

Which equals 1.6180339887498948482045868343656 (approx).

Very cool.

Math: Relearning It

November 27th, 2009

In the following days and hopefully weeks or months I am going to do a series of posts on Math.  I am taking a course, Calculus with Applications, through MIT’s OpenCourseWare, and will be doing a number of exercises from that course and posting them here.

This course’s “Philosophy of Learning” is rather interesting.  Here is what the course textbook has to say:

Philosophy of Learning

  1. Amount learned is proportional to time put in.
  2. Best way to learn is to figure out ideas yourself or teach them to someone else.
  3. Second best is to do so with hints from others like your friends or us.
  4. Third best is to get the ideas from reading; but pause in your reading to think about them.
  5. Fourth best: unacceptable: don’t get them at all.
  6. The object of a lecture is not so much to inform you of important facts, but rather to stimulate you to try to learn about some concept.
  7. The object of the course is to empower you to use the concepts of calculus in any context.

Point #2 seemed to ring true for me.  When I have had to learn a particular subject deep enough so that I could teach others, I usually learned that subject well.  I don’t kid myself into thing that many, of any, people actually read this blog.  However, posting some of my lessons here will still fill that same roll.  I will still go through the motions of having to learn something enough to teach it.

Its going to be slow going.  Its been a while since I was in a math class, but I hope that as I get going the cobwebs and dust will be shaken from my brain and some of the old math I learned will come back.  And even if it doesn’t, I can still learn new things!

Being Thankful

November 25th, 2009

The term “I am thankful for blah” seems a little to abstract for how I feel this year.  I truly feel thankful to very specific people.

  • I am thankful to all the people who came before me and created this wonderful world we live in, including this wonderful country.
  • I am thankful to the generations of people that put up with various injustices knowing that eventually we would form a more perfect union.
  • I am thankful for all the scientists, engineers, doctors, teachers, artists, musicians and other professionals that contribute to our society in real and profound ways, and make my life better.
  • I am thankful for those who serve our society and sacrifice their time for us.
  • I am thankful to my employer for deciding to keep me employed during these hard times.
  • I am thankful to my friends, of which I am extraordinarily lucky to be able to count as many.
  • I am thankful to my wonderful family.
  • I am thankful to my cat for not being completely pissed at me for his new insulin injection.
  • I am thankful to my wife.

The Best Idea Anyone Ever Had

November 25th, 2009

Today is the 150th Anniversary of the publication of The Origin of Species by Means of Natural Selection.  This is almost certainly the most important book ever written, and Charles Darwin’s idea has been aptly described as “The Best Idea Anyone Ever Had.”

Darwin waited nearly 20 years to publish his book.  The prominent theory on why he waited is summed as this: he was afraid of the negative backlash.  Darwin knew how deep his idea would cut across old mysticism, and also knew that it would not just be a flesh wound.  The idea of evolution wasn’t a flimsy knife, it was a mighty sword.  I believe Darwin understood that the very fact that the theory was so strong would be the thing that would make it so dangerous (perhaps for him).

During the time leading up to the writing of the book Darwin had researched the subject in great depth, and from then to the time of publishing had collected an even larger body of evidence.  When he did publish he did so with a well thought out theory that seemed to answer all the arguments against it.  He published a wealth (literally, for mankind) of knowledge that he had ingeniously analyzed and scrutinized.

How prescient this man was about its theory, and its reception!  Today, 150 years later, evolution is considered a scientific fact, so strong is the evidence in the view of the experts.  Predictions laid down based on evolution via natural selection have come true over and over.  We find new fossils yearly, right where they should be in the strata, right where they should be evolutionarily between already-known species.  We have found the “missing links” between the lower primates and man.  We now have molecular and DNA evidence supporting evolution.  In fact, there isn’t a single scientific fact known to modern biologists that disagrees with evolution.  Yet the old mysticism clings to bronze-age creation myths instead of recognizing the most well proven scientific theory ever.  Darwin was right about the science, but he was also right about his theory’s social implications, and that some would reject his theory without ever giving it a chance.

As a free thinking human I think this day should be celebrated as a high holiday.  Darwin’s achievement surely ranks with the discover of fire, the first written word, and Newton’s, Einstein’s and Heisenberg’s respective works in physics.  Sitting on top of the theory of evolution is all of modern biology.  Countless lives have been saved because of scientists developing medicine based on this knowledge.  It is a triumph of the human intelligence, one note in a song that tells us that we can shape our species’s destiny with critical thinking and scientific reasoning.

The USO

November 11th, 2009

For Veteran’s Day I just donated again to the USO. You can too!

USO

Click Here.

C# 4.0 Goodness: The dynamic Type, The DLR, and The ExpandoObject Type

November 10th, 2009

I have been playing around with C# 4.0 Beta 2 and stumbled onto the ExpandoObject (in the System.Dynamic namespace, System.Core assembly).  The object is built to be used in the DRL (Dynamic Language Runtime).  The DLR is a way for strongly-typed code to play nicely with weakly-typed code like IronPython, and vice-versa.  In C# 4.0 the dynamic keyword is used to reference objects that are weakly typed, and the ExandoObject is a .Net class that pretends to be a weakly-typed object.

ExpandoObject allows for addition of member properties and methods are run-time similar to popular scripting languages like Javascript.  Yet ExpandoObject is a real .Net type, and implements a couple of  interesting .Net interfaces: IDictionary<string, object> and INotifyPropertyChanged.  A dictionary of members is provides through the dictionary interface, and property changed events are fired through the property changed interface.  The only way to really leverage the power of ExpandoObject is with the dynamic keyword/type, which skips compile-time checking.  Method and property calls are not shown in intellisense.  You don’t know if your code will execute property until run-time.  So to access the above mentioned interfaces, the ExpandoObject’s dynamic reference must be cast to the interface type.

Here is some very simple, sample code that illustrates the concepts discussed (runs in Visual Studio 2010 / .Net 4.0 Beta 2):


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;

namespace DynamcMethodExample
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic person = new ExpandoObject();
            person.Name = "John";
            person.Gender = 'm';
            person.Age = 10;

            Console.WriteLine(person);
            Console.WriteLine(person.Name);
            Console.WriteLine(person.Gender);
            Console.WriteLine(person.Age);
            Console.ReadKey();

            person.MakeTacos = (Func<string>)(() => "Tacos are ready!");
            Console.WriteLine(person.MakeTacos());
            Console.ReadKey();

            ((INotifyPropertyChanged)person).PropertyChanged += (s, e) =>
                {
                    IDictionary<string, object> personAsDictionary = (IDictionary<string, object>)person;
                    Console.WriteLine("Property {0} changed to {1}.", e.PropertyName, personAsDictionary[e.PropertyName]);
                };

            person.Name = "Jane";
            person.Gender = 'f';
            person.Age = 11;
            Console.ReadKey();

        }
    }
}