Saturday, December 29, 2012

Reading - My Favorite

Reading, My Favorite

In the movie, "The Sound of Music", Julie Andrews sings "My favorite things". Since New Year is just around the corner, we should all sing our favorite things, in the hope that God will hear, and grant us some of our favorites. After all, God showered manna to the Jews as they crossed the desert, and they did not even have to sing for manna.

I am not too proud of my singing voice, so I will just list down my favorite things, and not sing them. I love to read fiction by Card, Goodkind, Jordan, and Potok, and computer-related biographies like Torvald's "Just for fun", Stallman's "Free as in freedom", and the nonfiction, "The soul of a new machine". Books, and now ebooks, inspire you, tell you of possibilities that you never thought of before, describe in technicolor how your heroes feel, and live, and love, and love their gods.

Go and read!

Wednesday, December 26, 2012

Backlighted Keyboard


Sometimes, I work at night on my laptop, but I do not want to turn the room lights on, because then, my wife won't be able to sleep with the lights on. So I just bought a backlighted keyboard from CDR-King, a Php480.00 purchase. I had to glue, using Magic Bond, footers for the keyboard, so that I can use it on top of the built-in keyboard without damaging the built-in keyboard.  The only drawback is that CDR-King's keyoard is PS/2 and not USB, so I had to get a PS/2-to-USB cable. The backlighting works as follows: the entire keyboard is backlighted, and the black-colored characters show through the semi-translucent keys. I would much prefer more translucent keys, to provide greater contrast between the keys and the characters printed on the keys, to provide better visibility of the characters.

Now I can work at night, without disturbing the wife. Maybe, this is bad for our domestic life, and so I should not work at night on a regular basis.

Sunday, December 9, 2012

Newton-Raphson Method, Part 2

Newton-Raphson Method, Part 2

Now we apply the procedure that we detailed in Part 1 to solve the equation:

f(x) = x^2 - 4*sin(x) = 0

First we write our own code for the functions sin() and cos(), since we are not sure of the accuracy of the built-in functions that are supplied with the math library.  We call our own versions mySin() and myCos(). Then we write the code for f() and call it fun(), and the code for f'() and call it deriv().

Here are the codes for mySin(), myCos(), fun(), and deriv().

*** funderiv.c ***
#define ABS(x) ((x) < 0.0? -(x):(x))
#define EPS  1.0e-12

// x - x^3/3! + x^5/5! - x^7/7! + ...

long double mySin(long double x)
{
  long double num = x;
  long double den = 1.0;
  long double term = x;
  long double sum = x;
  int j;
  for(j = 3; j < 1000; j += 2) {
    num = -num * x * x;
    den = den * (j-1) * j;
    term = num / den;
    sum = sum + term;
    if(ABS(term) < EPS) break;
  }
  return sum;
}

// 1 - x^2/2! + x^4/4! - x^6/6! + ...

long double myCos(long double x)
{
  long double num = 1.0;
  long double den = 1.0;
  long double term = 1.0;
  long double sum = 1.0;
  int j;
  for(j = 2; j < 1000; j += 2) {
    num = -num * x * x;
    den = den * (j-1) * j;
    term = num / den;
    sum = sum + term;
    if(ABS(term) < EPS) break;
  }
  return sum;
}

// f(x) =x^2 - 4.0*sin(x)

long double fun(long double x)
{
  return x*x - 4.0*mySin(x);
}

// f'(x) =2*x - 4*cos(x)

long double deriv(long double x)
{
  return 2.0*x - 4.0*myCos(x);
}

*** end ***

Compiling and running by using the shell script nrsolve.sh, we get the numerical results:

./nrsolve.sh funderiv.c 3.0
Starting value supplied: 3.000000
Iter=0, x=3.00000000000000, f(x)=8.43551996776053
Iter=1, x=2.15305769201338, f(x)=1.29477250528655
Iter=2, x=1.95403864200580, f(x)=0.10843855339463
Iter=3, x=1.93397153275207, f(x)=0.00115163152386
Iter=4, x=1.93375378855763, f(x)=0.00000013605495
Iter=5, x=1.93375376282702, f(x)=0.00000000000000
Solution: 1.93375376282702, error 0.00000000000100 after 5 iters


I hope that you find the procedure outlined here useful in your own work.  Enjoy!

Saturday, December 8, 2012

Newton-Raphson Method, Part 1


Newton-Raphson Method, Part 1

We give a procedure for solving a nonlinear equation in one unknown x, namely, we want to solve the equation f(x) = 0 for the unknown x, where f(x) is nonlinear in x.

For example, we want to solve the equation f(x) = x^2 - 4*sin(x) = 0 for some value of x > 0.

The Newton-Raphson (NR) method is an iterative procedure that is started with an initial guess x_0, and successively computes new guesses x_1, x_2, x_3, ... , x_k, ... , using the Newton-Raphson formula

x_{k+1} = x_k - f(x_k) / f'(x_k)

Under appropriate conditions on f(), in an interval [a,b] the sequence {x_k} will converge to a solution in that interval.

The problem with the NR method is that for each new function f(), we need to write a new program to compute the three entities f(x), f'(x), and {x_k}. Here we give a procedure that uses a fixed main module, main.c, that reads the initial value x_0 and uses externally defined functions f(x) and f'(x) in an external module funderiv.c, to compute the series {x_k} and provide reasonable stopping conditions. We also provide a glue script, nrsolve.sh, that compiles main.c and funderiv.c to give the executable nrsolve, and then runs nrsolve, in order to produce the sequence {x_k}.

We can pre-debug both the script nrsolve.sh and the main module main.c, and make sure that these are both working properly.  Thus the user only needs to write and debug the module funderiv.c that contains f(x) and f'(x), to produce a working NR-solution for that function f(x).

Here is our script nrsolve.sh

*** nrsolve.sh ***
#!/bin/bash

if [ $# -ne 2 ]; then
  echo "Run program as:\n  nrsolve.sh funderiv.c x_0"
  exit 1
fi

gcc main.c $1 -lm -o nrsolve

if [ $? ]; then
  ./nrsolve $2
fi
*** end ***


This script produces the executable nrsolve from the two modules main.c and funderiv.c (which may actually have another name), and then if compilation is successful, runs the resulting nrsolve executable, using the provided starting value x_0.

And here is our main.c module

*** main.c ***
#include <stdio.h>
#include <stdlib.h>
#define EPS 1.0e-12L
#define MAXITER 1000
#define ABS(x) ((x) < 0.0? -(x):(x))

extern long double fun(long double x);
extern long double deriv(long double x);
long double sol;
int j;

void newtonraphson(void)
{
  long double new;
  long double fsol;
  for( j = 0; j < MAXITER ; ++j, sol = new) {
    fsol = fun(sol);
    fprintf(stderr, "x=%.14Lf f(x)=%.14Lf\n", sol, fsol);
    if(ABS(fsol) < EPS) break;
    new = sol - fsol / deriv(sol);
  }
}

int main(int argc, char *argv[])
{
  long double a;
  if( argc != 2) exit(1);
  sscanf(argv[1], "%Lf", &sol);
  fprintf(stderr, "Starting value supplied: %Lf\n", sol);
  newtonraphson();
  fprintf(stderr, "Solution: %.14Lf, error %.14Lf after %d iters\n", sol, EPS, j);
  exit(0);
}
*** end ***


To complete our NR solution for the function f(x) = x^2 - 4*sin(x) = 0, see Newton-Raphson Method, Part 2

Thursday, December 6, 2012

Nov 3: CenPEG Briefs Bayan-U.S.A.


On November 3, 2012 a CenPEG team consisting of Bobby Tuazon, Evita Jimenez, and Pablo Manalastas gave a briefing to Bayan-U.S.A. in San Francisco, California. The topics discussed were (1) Political Dynasty in the Philipines, (2) Briefing on the Current Political Situation, and (3) Making Automated Elections More Transparent. An open forum followed after the talks.

It is nice to know that Filipino immigrants to the U.S. still think fondly of their native land, and actively support the nationalist cause.

My Canon EOS 450D Repaired



During my October 26 - November 11, 2012 visit to the U.S., I brought my Canon EOS 450D digital camera from Manila (86 degrees Fahrenheit) to Michigan State University at East Lansing (34 degrees Fahrenheit). On the second day of our visit, the auto focus (AF) mechanism on the 450D failed, and the only way I could take pictures is by manual focusing.

I consulted a friend, Michael Sy, who advised me that a ribbon connector inside the lens assembly that connects the AF to the body is probably broken and needs to be replaced. I did not quite believe him at that time, since I thought that moving a digital camera from a very hot place to a very cold place could affect the electronics.  I was secretly hoping that when we move to California, which has a milder climate, the AF mechanism will work again. I was wrong. We were already in California on November 1, and still the AF does not work.

We returned to Manila on November 12, and shortly after I showed the 450D to the Canon Repair Center at Cyberzone, SM North EDSA.  For P1,500.00 and after one hour of repair work on the 18-55mm kit lens, Canon replaced the ribbon connector, and my lens is working again.  Thank you Michael, and I'm sorry for not believing you right away. 


Bach - Marcello Adagio BWV 974



Dear Karen (my cello-playing daughter),

In the novel "Fifty Shades of Grey", Anastasia Steele listens to the haunting piano music of her sadistic lover Christian Grey, in the morning after they make love (f-ck, using his words). The music is Bach-Marcello Adagio BWV974. I found two versions in YouTube, one on the piano by James Rhodes, and the other on the cello by Orfeo Mandozzi:

http://www.youtube.com/watch?v=2FroBGNMeB8

It seems to me a beautiful cello piece that you might one day play for us.

Dad