Friday, October 28, 2011

Weekly LinkFest

1. A "Spherical Cow" from Abstruse Goose

2. Best Statistics Question Ever (via FlowingData)?

3. CometDocs a free-online document converter (H/T the Simple Dollar). (Yeah! the PDF -> ODF conversion is not too shabby!)

Tuesday, October 25, 2011

Passing parameters in Matlab or Octave: fzero and quad

Consider a simple function f(x) = 2x written in Matlab/Octave (myfunc.m).

function y = myfunc(x)
    y = 2*x

You could easily use the functions fzero and quad, to find a root of f(x) and the definite integral of f(x) between limits, say "a" and "b".

You would say something like:


where 1.0 is the guessed root. The answer that Matlab finds is indeed zero.



where a=0.0, and b=1.0 are the lower and upper limits. The quad function computes the correct answer (1.0).

Let us say that we had a slightly more general function f(x) = c*x, where "c" is a parameter (c=2, in the previous case). That is, we have the Matlab/Octave function:

function y = myfunc(x,c)
    y = c*x

Quite often we want to use fzero and quad on such a function for a particular setting of "c". That is we would like to be able to pass the parameter "c" to the function.

There are several ways to doing this. Some of them (like making "c" a global variable are ugly). The one I find most convenient is the following:

quad(@(x) myfunc(x,c), 0, 1.0)


quad(@(x) myfunc(x,2.0), 0,1.0)

fzero works in similar fashion. You could say:

fzero(@(x) myfunc(x,2.0), 1.0)

Just to reiterate, this method works in both Matlab and Octave, and you could easily generalize it to passing multiple parameters.

Friday, October 21, 2011

Presentations using Beamer


Finally, I decided to jump, and started using a LaTeX package called beamer to make presentations.

I had been reluctant to make the transition because (i) like all non-GUI programs/packages, there is a learning curve, (ii) I liked the platform independence of OpenOffice Impress, which (iii) especially when combined with oooLaTeX let me display mathematical formulae with the cleanliness of LaTeX, and (iv) my presentations, unlike my documents don't have as many cross-references and citations.

A part of me also liked the freeform nature of dropping images wherever I liked, and the ability to sketch up schematic diagrams on the fly.

I gave beamer a test drive, learning from these tutorials. Since I knew LaTeX before, I was up and running in about two hours.It took me surprisingly less time than I had expected.

In fact, I just presented slides made with beamer at the Society of Rheology's annual meeting in Cleveland.

In the future, I expect to use this tool a lot more.

Wednesday, October 19, 2011

Math Links

1. Two nice math videos (H/T Wild About Math)

2. The Cold Hit Problem (or are fingerprints unique?)

Saturday, October 15, 2011

Another Legend Falls

RIP Dennis Ritchie.

As Brian Kernighan, a colleague at Bell Labs, wrote, "The tools that Dennis built — and their direct descendants — run pretty much everything today,"

As a "tribute", I tried to collect some of his quotes from the Internet (Disclaimer: I have not assessed their veracity)
A language that doesn't have everything is actually easier to program in than some that do.

I can't recall any difficulty in making the C language definition completely open - any discussion on the matter tended to mention languages whose inventors tried to keep tight control, and consequent ill fate.

UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.

The only way to learn a new programming language is by writing programs in it.

Friday, October 14, 2011

Profiling Fortran and C programs

Given how easy it is to profile programs written in C and Fortran, I wonder why many people don't do it. Profiling lets you monitor the performance of your code.

One way to do it is to use GPROF.

Here is how you use it.

1. Compile your code (test.f90) with the -pg flag

gfortran -pg test.f90 (or gcc -pg test.c)

2. Run the program


This creates a report gmon.out, which is unfortunately not in readable format.

3. To read it, say

gprof a.out

You will see a reasonably detailed report on number of function calls and time spent in different parts of the program.

Friday, October 7, 2011

Floating Point Number Line

In the class I am teaching this semester, I was talking about the "discreteness" of the floating point number line, and the errors that pop up due to finite precision.

One of my students pointed out that he had heard that finite-precision was behind some of the initial failures of Patriot missiles.

So I went back and researched and found that on Feb 25, 1991 an incoming Iraqi Scud missile killed 28 soldiers at Dharan because the Patriot missile that had been fired to intercept it failed.

The source of the problem was that the onboard 24-bit computer measured time in integral units of 1/10 of a second (so 35, 36, and 37 instead of 3.5s, 3.6s and 3.7s), and the integer was multiplied by 1/10 on demand.

The problem is 1/10 is a non-terminating sequence (0.0001100110011....) when translated in base-2. This is similar to 1/3 being non-terminating in base-10 (0.333...). When chopped off after 24 bits, the truncation error is about 1/10^7 s.

While this may seem small, a battery operating for 100 hours on the missile would have accumulated a round-off error of 0.34 seconds. Given the speed of the Patriot missile (about 1700 m/s), it was off-target by about 600 meters.

Interestingly, the Israeli's warned the US Army about the problem two weeks ago before the accident. Their solution: reboot frequently. While they did not know how this solved the problem, frequent resetting made the clock time smaller (than 100 hours), which meant smaller accumulated round-off error.

Obviously, the problem could also have been avoided by using 1/8 or 1/16 as the unit of time, since these numbers can be represented exactly in base-2.

Images: xkcd and