analytics

Wednesday, June 8, 2011

A form of polynomial

I was looking at a function f(x) that passes through the points (0, a), (1, b), (2, c), (3, d). This is what I came up with:
   (1-x)*(1/2)(2-x)*(1/3)*(3-x)*a+
   (x) *(2-x)*(1/2)*(3-x)*b +
   (1/2)*x *(x-1) *(3-x)*c +
   (1/3)*x *(1/2)*(x-1)*(x-2)*d

  If I had considered 5 terms instead the fifth would be:
  (1/4)*x  *(1/2)*(1/3)*(x-1)*(x-2)*(x-3)   *(5-x) * e
  So it generalizes pretty well.
The reason the formula I found for going through four points works is if you look at the four lines of the formula, at x=0 all that preceeds A is equal to 1, and at least one of those is 0 at every x after that so A is being multiplied by 0. The same is true for each line for x=0,1,2,3 respectively A,B,C,D are being multiplied by 1 at x=i and 0 elsewhere so the ith term is all that doesn't cancel out.

Anyway a useful feature of this type of interpolation is that if you know the f(x) values at x=0,1,... you can easily put it into this form which is a very nice and simple form (practically factored) to look at f(x) in. .

Wednesday, June 1, 2011

Lateral Doppler-Like shift

So it's well known that if you are moving away or towards an emitter of electromagnetic radiation you will perceive what is called a red shift or a blue shift depending on whether you are moving away or towards respectively. The red shift means the frequency of the wave seems to decrease as you move quickly away and the blue shift means the frequency seems to increase as you move quickly towards. Think if you were in a boat at the ocean and moving into the waves each wave would reach the boat more quickly than if you were riding away from the source of the waves.
  So anyway today I was reading about relativity and I had this thought experiment:
A spaceship launched from the planet and accelerated to a significant fraction of the speed of light. Here a wave is being sent from the line A to B. Let's say it's a radio signal and he has a radio on his ship so he can hear some music playing. At a certain velocity it would seem to him because of relativity that he could hear the signal on his radio for 2 seconds but to us it looked like it took him four seconds to pass through the area where the waves were... this is known as the Twin's Paradox or Time dilation if you want to read on Wikipedia.
  But if we saw him pass through for 4 seconds and he only experienced 2 seconds of radio, it seems to me that he must have heard on his radio music at twice the frequency than was being sent out. I mean he heard what to us was 4 seconds of music but in 2 seconds so the singers must have sounded like chipmunks.
  So anyway that's why I call it a lateral Doppler-like ship because he's moving perpendicular to the waves and experiences a blue shift as if he were moving rapidly towards the waves.

A non random normallish curve

  I started thinking about the idea that a normal curve can be found if you flip a large set of coins many times and count how many heads you get each time. Then plot how many times you got each possible number of heads. You'll end up getting no heads or all heads on all the coins very few times and about half heads the most. This makes a normal curve.
  Well I started thinking what if you take the randomness out, and assume that you flipped every possible outcome of a series of heads and tails for say, 10 coins, exactly once. There would be 2^10 or 1024 possible outcomes, each one would show somewhere between 0 total heads or all 10 heads. I also figured that one can use the combinatoric formula called n choose r for telling how many ways there were to get each total number of heads. For instance, for 10 coins and you want to know how many ways there are to have 5 heads on the coins you can do 10 choose 5 or 10!/5!*(10-5)! = 252. Maple the math program actually took a few seconds and derived this formula:
This is a good check, it means if you add the 252 I just got for five with the formula for all the other numbers from 0 to 10 they add up to 2^10 or 1024  which should be the total number of ways to flip 10 coins.

  So the next thing was to graph the combinatoric part over the 2^10 whole as you vary r from 0 to 10:

Maple has a way of estimating factorials even in between integers where factorial isn't typically thought of existing but as you can see this leads to a nice normal looking curve. The height is different than the standard normal distribution, though, here is the formula and graph one typically uses:

You can see this formula has all kinds of e's and pi's whereas mine has only factorials and 2^n. But the overall shape is very similar.

Thursday, May 12, 2011

Gauss's Circle Problem Part 1

So anyway one way or another jumping from one wikipedia aritcle to the next I ended up on this:
http://en.wikipedia.org/wiki/Gauss%27s_circle_problem

And I didn't really think anything of any furthering of the idea or anything until the day after, when I was working on a project where I had to cut a wire screen into a circle which was a pretty big coincidence... I thought about this. Take a circle on a grid like in the Gauss Circle Problem, I chose a diameter of 23. I chose a prime because I thought it would make it easier to see the form of the formula but it didn't really matter in the end.
   Gauss's circle problem asks how many vertices of this grid are within the circle in this case of diameter 23. By vertices I mean how many corners of all the squares counting each only once. 
   So if you read through that wikipedia article you might be able to tell that what I'm about to try here is different than the way they've approached this problem, I'm actually trying to use a millenia old technique by Archimedes on a hundreds of years old problem. First notice the square that completely encloses this circle...
It contains the diameter of the circle squared or 23^2 = 529 vertices or grid points because the diameter of the circle is the same as the length of the side of the square and that squared counts all of them. 

So then I moved onto an octagon, A number above a line means that that many vertices are contained within the octagon on that line beneath it. See the bottom black line for instance covers 9 vertices and goes a bit beyond them.
So after quite a while of looking at how to generalize to get that number 417 up there and for all different starting diameters of circle which lead to a particular size of octagon I found this approach...
Note the green boxes, sometimes the diameter called s in the formulas below evenly divides 3, sometimes it divides 3 with a remainder of 1, sometimes it divides 3 with a remainder of 2. The one above 23 has a remainder of 2, so 3 evenly sized green squares from the top of the octagon to the bottom miss 2 rows of vertices and the 3 going across the middle of the octagon misses two columns of vertices, but besides that together they contain every vertex within the blue square, which we know is 529 = 23^2 vertices.
Ok so if the diameter of the circle divides 3 with a remainder of 2 like above, these two formulas:
Here s is the diameter so (23-2)/3 = 7 = x
Then plug 7 into the bottom to get 417 which is the number that I hand counted were inside the octagon earlier, that was tedious.  This formula tells you for this case without having to count.
To explain the top formula we know s=23 gets divided up into 3 parts but the way they have to be spaced makes two lines of vertices between so 23-2 = 21/3 = 7 = both the width and height of the lines of vertices within each green square. so there are 9*7^2 vertices within the green rectangles because there are nine green boxes and each has 49 because they're 7 on a side. But there are two columns and two rows between those so 4*23 more but where those cross you end up counting them each twice so -4.  All that equals 529 so so far so good, that is the total amount of vertices in the blue square. But the black octagon has the corners cut off....
   So second half in the second formula... x*(x+1)/2 = 7+6+5+4+3+2+1 = starting with the first diagonal of vertices outside one diagonal edge of the black octagon there are 7 vertices, then the next one further away from the octagon has 6, and so on... one corner alltogether has 28 vertices if you add it all up. So there are 4 of them cut off and they're all equal so 529 - 4*28 =  417. Which is the number inside of the black octagon.

I'm going to do at least a part 2 where I move on from octagons to a hexakaidecagon which is 16 sides, then I'll attempt to find a pattern as you keep doubling the number of sides, and hopefully the limit of that as you go to infinite sides will be a formula for exactly how many grid points are inside the circle. There are other exact formulas for this quantity but they use the floor function which is not as nice as an exact algebra equation for a variety of reasons. not too many of which I can name, I just overall floor is not as good somehow.

So the square had 529, the octagon had 417, it ends up the sixteen sided has 393 and I think the 32 sided has 377 which is the correct number within the circle. So usually you don't have to go all that far to get to the right number.

You might remember I said those equations above only work for a remainder of 2 when dividing a diameter by 3. I don't know yet whether when moving to the 16 sided if that breaks up into more cases after the 3 original ones or not, hopefully not.

Monday, May 2, 2011

Discrete summation with integrals

I was looking at the difference between an integral of a polynomial and the summation of that polynomial over the integers. Like starting with a polynomial say, x^3, over the interval 3..5 just at the integers is 3^3 + 4^3 + 5^3 = 216, but the integral over that same interval is only 136. I found the following relationship:

It's strange for a couple of reasons, why the fractions on the outside of the integral go in the pattern that they do and why does it seem to skip the 3rd derivative in every case? That makes it kind of hard to tell what the ... actually means.

Saturday, April 23, 2011

Streaming Content

I'm not sure where to begin with this idea, I guess I'll start by pointing out some interesting web pages for streaming content over the internet. In a couple hours I found all these different websites (some I'd heard of before)
  The first thing I noticed is in the networks section, every channel I could think of that we get of on our cable plan has a website that has all the content from the channel. Except online it seems you can watch any episode of the show that ever aired, start it whenever you want, pause and rewind and so on. So that got me thinking about cancelling the cable all together. I'd still need the internet part of the service but not the box or that service.  It seems the content providers are cutting out the middle man of the cable companies except it happens that the cable companies are also the ones providing the internet so not completely. 
   Not to mention all the other streaming content in all the other boxes. But I feel like it leaves something to be desired first googling for a show to find out where on the internet you can watch it and navigating through each website's different layout to eventually find the episode you want and finally playing it. 
  So that brings me to my idea, I think it would be nice to have a front end to all this content where you can type in the name of a show and it returns links to the video you want that you can click on and it takes you to the relevant website and starts playing the video. But then I started thinking of more features the front end could have like keeping a list of the shows you like and giving you a notification of which shows had released new episodes and providing links to where you could watch them. So no more having to web surf to every individual site and find the show you like and check if there are new episodes or having to know when they air on TV it would just give you a list of what new stuff was available. I'm still coming up with more features the front end could have but I think you get the idea.  
  Anyway there's so much content on the internet now but it'd be nice to design a cohesive front end to easily find what you want and perhaps some additional features like I mentioned. I think it would be nice if it worked with a remote. Basically to try and capture what is nice about watching it on cable but with all the nice features that internet streaming provides. 
  

Thursday, April 21, 2011

Traveling Salesman

I wrote a computer program that tries to find a Hamilton Circuit , which is given a bunch of "cities" try to find a path that visits every city and ends back up where you start. Each city is connected to some of the other cities with roads. So to start out with your map might look like this:
It's kind of a mess, right? You might be able to tell from looking at it the path that solves the problem but it's one of those things that is easier for a human than a computer. So I wrote this program that as step 1 randomly puts these cities on a circle evenly spaced all around. Like so:
You can see that 7 is not connected to it's immediate neighbors so what my algorithm does is it goes through point by point and tries to move each point towards the other points it's connected to, and away from the ones that it's not connected to. So imagine that these points are going around the circle in different directions trying to get away from the ones they're not connected too and towards the ones that they are connected to. 

It finally reaches equilibrium where it can't seem to do any better and that is this final graphic...
 When the program is finished the points aren't evenly spaced around the circle like above but it was too hard trying to get it to output exactly like the program does. So imagine they are in this order but a little bit randomly spaced around the circle. Anyway the thing to notice is now there is a path all the way through all the points that visits every one and ends up back at the beginning (it's drawn as a circle). You can sort of tell by looking at it that paths all the way across the circle are unfavorable.
The interesting thing is this program does this really fast, it only had to move each point on the circle a certain amount 50 times, which sounds like a lot if you're doing it by hand but it is nothing to the computer. 
 Here is the source code to the program: 

import math
def distance(posa, posb):
    delta = posa-posb
    delta2 = posb + 360-posa
    
    if delta2 < delta:
        delta = -delta2
    if abs(delta) == 180:
        return 0
    if delta <= 0:
        if delta < -180:
            return delta%180
        else:
            return delta
            
    else:
        if delta > 180:
            return delta%180
        else:
            
            return delta

def potential(point, graph, pos):
    sum = 0
    for i in range(0, len(graph[point])):
        sum+= distance(pos[graph[point][i]], pos[point])
    all = set(range(0, len(graph)))
    n = set(graph[point])
    nc = list(all - n)
    for i in range(0, len(nc)):
        d = distance(pos[nc[i]], pos[point])
        if d < 0:
            d = 180+d
        else:
            d = 180-d
        sum-= d
    return sum    
def move(steps, graph, pos):
    for s in range(0, steps):
        for i in range(0, len(graph)):
            p = potential(i, graph, pos)
            
            if p > 0:
                if (pos[i]+int(round(p/10.0))) <360:
                    pos[i] +=int(round(p/10.0))
                else:
                    pos[i] += int(round(p/10.0))
                    pos[i] -= 360
            if p < 0:
                if (pos[i]+int(round(p/10.0))) >= 0:
                    pos[i] +=int(round(p/10.0))
                else:
                    pos[i] += int(round(p/10.0))
                    pos[i] = 360+pos[i]

def main():
    graph = [[1,2,5,8,9],[0,2,7],[0,1,3, 9], [2,4, 5, 6], [3, 6, 7], [0,3, 6, 8], [3, 4, 5, 8], [1,4], [0,5,6,9], [0,2,8]]
    pos = [0, 36, 72, 108, 144, 180, 216, 252, 288, 324]
    move(50, graph, pos)
    print(pos) 
main()