Saturday, August 23, 2008

Plotting Graphs

Finally, I realise that I have found the right tool to plot graphs that I can use peacefully without having to worry about anything other than plotting the graph.

It started with my engineering studies when I wanted to show a couple of my friends that an FM wave formed by combining the baseband data signal with a sinusoidal carrier is really frequency modulated where the instantaneous frequency of the FM wave varies as per the baseband data signal. I wrote a program in C and plotted the graph with the help of the graphics library available with Turbo C. But soon, I came across Linux and I found that I couldn't compile those programs using gcc.

Then, I came across Java and learnt drawing stuff using Swing. But it was an involving task. I drew the grid by running a loop that drew Line2D.Double objects on the Graphics object. I plotted the points drawing Ellipse2D. So, I had to do something like the following to plot the dot exactly over a point (x,y).

// Plot the point
int thickness = 4;
Ellipse2D point = new Ellipse2D.Double(x - thickness/2, y - thickness/2,
thickness, thickness);
g2.fill(point);
This thing was inside a loop that calculated y for each x. Of course, I had to do the routine work of getting the screen dimensions, add a JPanel to the ContentPane, etc. This wasn't fun as my main objective was to study the curve.

Then, I came across Matplotlib, a plotting library for Python. Since, I was already familiar with MATLAB, I enjoyed it. The function names and usage were very similar to that of MATLAB commands. It gave me all the powerful things like plot(), axis(), xlabel(), ylabel(), etc. but I missed plot3() a lot. So, I could not plot 3D graphs.

I was already using GNU Octave for quite sometime as an alternative to bc. It helped me to calculate the binomial coefficients quickly using the nchoosek() command. GNU Octave is very similar to MATLAB and I enjoyed it. So, I checked whether it supported the plot() function. It did. I checked plot3(). It worked too. So, I started using this for my plotting work. Last week, I plotted many different families of curves along with their envelopes and I found the task really easy and simple with GNU Octave. Let me show its simplicity with an example. I wanted to plot the family of lines given by the function, y(x) = 2cx - c2 along with its envelope. See the code and its simplicity:

% Presentation
axis([-5, 5, 25, -10]);
grid("on")
hold on

% Plot the family of curves: y(x) = 2cx - c^2
x = [-5:0.1:5];
for c = [-10:0.5:10]
plot(x, 2 * c * x - c^2, 'b-')
endfor

% Plot its envelope: y(x) = x^2
plot(x, x .^ 2, 'r-');
I think I'm going to use this for a long time.

Thursday, August 07, 2008

My favourite quotes from books

Some favourite lines from my favourite books:

The Code Book by Simon Singh

Says Hellman: ... You have idea number 1, you get excited, and it flops. Then you have idea number 2, you get excited, and it flops. Then you have idea number 99, you get excited, and it flops. Only a fool would be excited by the 100th idea, but it might take 100 ideas before one really pays off. Unless you're foolish enough to be continually excited, you won't have the motivation, you won't have the energy to carry it through. God rewards the fools.

Applied Cryptography by Bruce Schneier

Those who claim to have an unbreakable cipher simply because they can't break it are either geniuses or fools. Unfortunately, there are more of the latter in the world.

The Music of the Primes by Marcus du Sautoy

But for Grothendieck this was not abstraction for abstraction's sake. In his view this was a revolution that was necessitated by the questions that mathematics was trying to answer. He wrote volume after volume describing this new language. Grothendieck's vision was messianic, and he began to attract a following of faithful young disciples. His output was huge, covering some ten thousand pages. When a visitor complained at the poor state of the library at the Institut, he replied, 'We don't read books here, we write them'.

Fermat's Last Theorem by Simon Singh

Wile's colleague Ken Ribet had no such qualms: 'It was a completely remarkable event. I mean, you go to a conference and there are some routine lectures, there are some good lectures and there are some very special lectures, but it's only once in a lifetime that you get a lecture where someone claims to solve a problem that has endured for 350 years. People were looking at each other and saying, "My God, you know we've just witnessed an historical event." ...

The Man Who Knew Infinity: A Life of the Genius Ramanujan by Robert Kanigel

Ramanujan had lost all his scholarships. He had failed in school. Even as a tutor of the subject he loved most, he'd been found wanting. He had nothing. And yet, viewed differently, he had everything. For now there was nothing to distract him from his notebooks - notebooks, crammed with theorems, that each day, each week, bulged wider.

The Golden Ratio: The Story of PHI, the World's Most Astonishing Number by Mario Livio

Mathematics appears at first glance to be just too effective. In Einstein's own words: "How is it possible that mathematics, a product of human thought that is independent of experience, fits so excellently the objects of physical reality?"