If A Picture Paints A Thousand Words

And so begins the words of the a song by the Bread. The complexity we see in the world is a product of the interactions of simple building blocks. Everything we see is just a combination of a few elements in the periodic table. In turn every element is a combination of electrons, protons and neutrons.

A picture paints a thousands words but what paints a thousand pictures? Just like a Lego truck that is composed of a few Lego shapes, a “black and white” picture is also composed of a few levels of gray. The levels is just a set of 256 numbers starting from 0 up to 255. The number 0 represents black and the number 255 represents white. The numbers in between represent colors between extreme black and extreme white. The diagram below shows the levels of gray in a 16 by 16 matrix. The gradient goes from left to right, top to bottom.



Using just these 256 numbers, we can already draw interesting figures. Take for example this 8 by 8 matrix of numbers:

197   4  46  18 155 186 144 190 
111  30   6 131 235  99 112 227  
10   26 141 147 120  28  92 187 
241 100 118 149 178   5 158 213 
105  43 104  63 172 222  64 174 
130 194 243  56  59 166 134 133 
247 145  36  82 238 251  53 129 
171  29  51 163 199 214 113 195

When you plot this on an 8×8 grid, you get the following pattern:



Not all pictures need to be boring. Here’s an example of a picture that is not that boring:

17 23 26 20 13 20 20 22 21 17 17 25 20 11 15 15 9 10 18 13 14 12 13 39 61 63 71 72 69 71 74 72 74 75 72 76 75 75 75 80 77 78 79 79 80 80 80 80 81 81 82 82 83 83 83 83 84 84 84 84 85 86 86 86 87 87 87 88 88 88 89 89 89 89 90 90 90 90 90 91 90 90 91 91 92 92 92 92 94 94 95 95 95 95 95 95 96 96 96 96 97 97 97 97 98 98 98 99 99 99 100 100 101 101 101 101 102 102 103 103 104 104 104 105 105 105 105 105 106 106 106 107 107 108 108 108 110 110 110 110 111 111 112 112 113 113 113 114 114 115 115 115 116 116 117 117 117 118 118 119 119 119 120 120 121 121 122 122 123 124 124 125 125 125 126 127 127 127 128 128 129 129 129 129 131 131 132 132 133 134 ...

That is just the start of a 310×204 matrix saved in the file “clifton.txt”. If you plot that using the R command

clifton = read.table("clifton.txt")

#convert this data frame to a vector
vclifton = as.numeric(clifton) 

#create the 310x204 matrix
mclifton = matrix(vclifton,nrow=310,ncol=204)

#plot the image
image(mclifton[,ncol(mclifton):1],col=gray((0:255)/255))

you’ll get the following image



Histogram

If you look at the file “clifton.txt” and count the occurrence of each number, you will see that there are 9 occurrences of zero and 4 occurrences of 240. If you do this for all other numbers, you will get the following counts:

Level Frequency
0     9
1     16
2     31
3     34
4     49
:
:
237   115
238   113
239   18
240   4
:
:
254   0
255   0

If you plot this data as a bar chart, you will get what is known as the histogram of the picture:



The histogram above was obtained using the following R command:

hist(vclifton,breaks=256)

The histogram tells us the how much of each gray level contribute to the actual image.

Here’s another example of a picture that is relatively dark. In photography, the image below is called a “Low Key” image because it is mostly dark.



Low Key images tend to have the bulk of the colors at the right side of the scale. The next image is a “High Key” image because it is mostly white:

Histograms of High Key images tend to have the bulk of the colors at the right side of the scale. The histogram of the “clifton.txt” image tends to have a much better balance in that it contains most of the colors at the middle of the range.

It’s surprising how we can come up with complex images using just 256 levels of gray. We haven’t even considered colors yet! We also learned how histograms can be constructed by counting the frequency of each level and what histograms of High Key, Low Key and Optimum exposure look like.

Advertisements

New Year, New Heights and the Projectile

I spent my new year celebration at our friend’s place with 2 other colleagues. It was started with a dinner at about 9:30 pm and we bought some bottles of wine in addition to the spirits we already brought. At about 10:30, a street party was setup in the vicinity and about 5 other families joined in a potluck. Marijo’s brother started the fireworks at about that time with David and Gerard (my colleagues) taking turns setting them off. I did not attempt to set them off because I did a lot those things way back as a child and still lucky enough to have my fingers intact.

This is a video of our 2012 New Year’s Celebration:

As Gerard was flying off skyrockets (or kwitis as they are called in my language) David asked me “How high do you think these things go up?”. That was a really good question which got me to thinking. I said “Let’s compute!” So I took my iphone and launched its built-in stopwatch. Gerard flew about 3 skyrockets and I took the time it takes for them to fly until they blow up in the air. The average was found to be 2.3 seconds.

Independent of Mass

Legend has it that Galileo Galilei performed an experiment at the Leaning Tower of Pisa. He dropped objects of different masses to see which of them falls first. Starting from the heaviest to the lightest object, what he found was that, in a vacuum, they reach the ground at the same length of time when dropped from the same height. Using this result, we can compute for the distance travelled by any object under the influence of gravity. At heights near the earth’s surface, the acceleration of objects in the presence of gravity alone is given by

\displaystyle \frac{d^2h}{dt^2}= \frac{dv}{dt} = -g

where h is the vertical distance (height), v is the velocity and g is the acceleration due to gravity given by g = 9.8 \text{m/s}^2.

The equation above is a simple differential equation which we can solve for the velocity at any time t by integrating both sides:

\displaystyle  \int^{v}_{v_i} dv = \int^{t}_{t_i} -g dt

which by the Fundamental Theorem of Calculus gives us

\displaystyle v - v_i = -g(t - t_i)
\displaystyle v = -gt + v_i + t_i

If we take initial time of the rocket launch to be zero, that is, t_i=0, we have

\displaystyle v = \frac{dh}{dt}=  -gt + v_i

We don’t know the initial velocity v_i of the rocket. However, at the maximum height attained by the rocket, the velocity is zero. Using this fact, we can solve for the initial velocity:

\displaystyle 0 = -gt_{\text{max}} + v_i
\displaystyle v_i = gt_{\text{max}}

where t_{\text{max}} is the time the rocket reached maximum height.

At t_{\text{max}}=2.3 seconds, the initial velocity is therefore v_i = 9.8 * 2.3 = 22.5 m/s.

Maximum height

We can solve for the height as a function of time by integrating both sides of the velocity equation:

\displaystyle \int^{h}_{h_i} dh = \int^{t}_{t_i} -gt dt  + v_i dt
\displaystyle h - h_i = -\frac{gt^2}{2} + v_i t \big|^t_{t_i}

Since the initial height is zero , that is , h_i = 0 and the initial time is zero t_i = 0, we have

\displaystyle h = -\frac{gt^2}{2} + v_i t

Since at t=2.3 seconds, the rocket reaches the maximum height, we have

\displaystyle h_{\text{max}} = -\frac{9.8 \times (2.3)^2}{2} + 22.5 * 2.3 = 25.829 \text{ meters}

Does this make sense? The average height of a building storey is about 3 meters. This is about 8 or 9 storeys high which I think makes sense.

It’s good that my new year not only started with a warm welcome from friends but it also appealed to my physics curiosity.

Thank you to Marijo Condes and family for letting us spend a memorable New Year’s Eve.

My Topological Trip To Baguio

Last weekend, we went up to Baguio, the summer capital of the Philippines, to escape the summer heat. As we were going up the long and winding road to Baguio, I find myself frequently looking at my watch, impatient to get over the long trip. As I looked at the time, I also took note of the place where we were during that time. Suddenly, a question popped in my head: “Will it be possible that we’d be in this same place at this time when we return to Manila?”. We can visualize this problem using the diagram below:


In the figure above, the blue dot is the location along the way from Manila to Baguio which we occupied where the clock time was the same when going up as when going down. It turns out that such a place exists. However, where it is located is arbitrary. To see this, suppose that we leave Manila at 6 am and arrive in Baguio at 6 pm. (On the average, it takes about 5-6 hours to reach Baguio from Manila. In this case, we will be taking our time enjoying the scenery). On the day that we go back, we will leave Baguio at 6 am and arrive in Manila at 6 pm. To find the place and time in question, use 2 cars, one starting in Manila going to Baguio and the other car starting from Baguio and going to Manila. When they meet each other along the way, that’s precisely the place and time we are looking!.

Topology

The problem above is an illustration of the power of Topology, a branch of mathematics that studies the properties of mathematical objects that are invariant under stretching without tearing or gluing. For a topologist, there is no difference between a doughnut and a cup because we can always deform the doughnut to look like a cup.


The most obvious topological property of the doughnut is the single hole it has. Any transformation that stretches or deforms it will always retain that hole. Which means it is always possible to find a transformation that will transform your doughnut to a cup. Of course, don’t eat the cup!

Topology is sometimes called “Rubber Sheet Geometry” because it studies objects and its properties that are invariant under stretching. In the diagram below, we start with a flat sheet of rubber with a straight line in it. Stretch this rubber in any which way. You will notice that the line, even though it will not look straight anymore, will still be one piece of line and not two. In other words, the line retains its property of connectedness. It will still be in one piece and not suddenly become two pieces.


Brouwer’s Fixed Point Theorem

The Baguio trip example I gave is an instance of what is known as the Brouwer’s Fixed Point Theorem. In its simplest form, it states that any continuous function f from a closed interval to itself has a fixed point. Let [a,b] be the closed interval in 1 dimension. This means that f(a) and f(b) lie also in [a,b]. Furthermore, whatever number f(a) is mapped to, it is always greater than or equal to a. Also, whatever number f(b) is mapped to, it is always less than or equal to b.


Define the function g(x) = f(x) - x. We can easily see that g(a) \ge 0 and g(b) \le 0. Since g is a continuous function, then there is a point \xi such that g(\xi) = 0 (by the Intermediate Value Theorem), that is, f(\xi) = \xi. The number \xi is called a fixed point of f.


For example, consider the cosine function defined in [-1,1],

\displaystyle f(x) = \cos(x), -1 \le x \le 1

then by Brouwer’s Fixed Point Theorem, there is a point x_0 \in [-1,1] such that

\displaystyle \cos(x_0) = x_0


As can be seen from the diagram above, this point is the intersection of the graph of the cosine function and the line f(x) = x and is equal to 0.73909.

The Baguio trip has filled me with fond memories of my university days especially on Topology. I’ll be sharing them in my subsequent posts.

Mt. Mayon’s Mathematical Beauty and Elegance

Mayon volcano refused to show its glory last weekend. It was like a beautiful but shy girl that hides her face behind a veil of clouds. You know the beauty that it is hiding but it keeps you in suspense and wants you go to back there and try your luck again to see it in all its splendor.

Fortunately, Mayon is a perfect cone volcano and it does not take a lot of effort to imagine how it looks like. I was inspired by Mayon to generate its shape using some computer algebra tools at my disposal. The first tool I used is the tool I always bring with me, the iPhone. The iPhone has a neat computer algebra system called Spacetime. It can do a lot of things you would expect from a Computer Algebra System among which is plotting surfaces. To plot the surface of Mayon, I used a coordinate system that is suited to its symmetrical shape. This coordinate system is the cylindrical coordinate system.

Cylindrical Coordinate System

Any point in three dimensions can be specified by three numbers. In the rectangular coordinate system, which we are very familiar with, these three numbers correspond to the x, y and z coordinates of the point. Shown below is how we specify a point using the rectangular coordinate system.



The rectangular coordinate system is not always the most convenient system to use. For example, to specify a circle in rectangular coordinates, you need to specify it as

\displaystyle x^2 + y^2 = r^2

where r is the radius of the circle.

In fact, we can specify a circle even simpler by using polar coordinates. We only need to specify the radius and the angle. For a circle of radius 1, the equation is

\displaystyle r=1

How simple can it get?

In generating the cone, we use a coordinate system that naturally fits the geometry of the cone. This coordinate system is called the cylindrical coordinate system. Imagine a point p in space and a cylinder that contains this point. We can specify the position of this point in the cylinder by three numbers. The first number specifies the height of this point from the xy plane. The second number specifies the distance of this point from the z axis. The last number specifies the angle \phi that this point makes from the x axis. Below is a diagram of the cylindrical coordinate system.


Specifying the Cone Using Cylindrical Coordinates

Since the cone has circular symmetry around the z axis, we only need to specify the value of z with respect to r. Any plane that passes along the z axis intersects the cone through a line that lies on the cone. The diagram below shows the line of intersection of a cone of height h and whose base has radius b.


The equation of this line has the form

\displaystyle z=mr + k

where m is the slope and k is the z intercept. When r=0, we have z=k. When z=0 we have m= - h/b. Therefore, we can specify z as:

\displaystyle z = - \frac{h}{b} r + h.

Mayon has a base approximately 4 times its height. Using h=4 and b=8, we can then specify the cone as

\displaystyle z = - 0.5 r + 4.
\displaystyle 0 \le r \le b
\displaystyle 0 \le \phi \le 2\pi

Using the function CylindricalPlot3D of spacetime, we get the plot of the cone as shown below.



Surface Plot Using Rectangular Coordinates

The plot of the cone using cylindrical coordinates is elegant. It used the circular symmetry of the cone around the z axis. We can still plot the cone using rectangular coordinates but it is quite messy. To do this, I used another tool called R. It’s a statistical computing tool but it has a great 3D plotting engine. Here is the R code I used to generate the plot:

# interval is the distance between points in the x or y axis
interval=2

# noisemean is the mean of the gaussian noise we want to introduce into the plot to
# simulate rugged mountain
noisemean=0

# noisedev is the standard deviation
noisedev=1

# addnoise is a boolean. It specifies if we want to add the noise
addnoise=T

# generate the x and y vectors below using the interval between points
x=seq(-10,10,interval)
y=seq(-10,10,interval)

# generate the matrix of squares for the y axis
l=length(x)
yy=y^2 %*% t(rep(1,l))

# generate the matrix of squares for the x axis, which turns out to be just the transpose of yy
xx=t(yy)

# compute the zz coordinate: zz = -0.5* r +4
# but first let's compute r^2, let's call it rr

rr=xx + yy
zz = -0.5 * sqrt(rr) + 4

# plot only values greater than or equal to  0
myfunc=function(n){
        if(n<0){
                0
        } else {
                n
        }
}

mayon= mapply(myfunc,zz)

# add some noise
if(addnoise){
        noise=0.1*matrix(rnorm(l^2,noisemean,noisedev),l,l)
        mayon=mayon+ noise
}

# plot using rgl
library(rgl)
persp3d(x,y,mayon,col="yellowgreen",aspect=c(4,4,1))

Below is the result of running the above code.


This how I see mayon through the lens of mathematics. But I definitely want to go back there and see its imposing grandeur.