function[r, theta] = rect_to_polar(x, y)
// convert the rectangular coords (x, y) to polar coordinates 
//  
//     r     =  magnitude of the vector
//     theta =  angle of the theta with respect to the x-axis,
//                    positive in the counter-clockwise direction,
//                    in units of radians.  
//                    The value of theta ranges from 0 to 2pi

  // we must define PI
  pi = 3.14159;

  // first, calculate the magnitude of the vector
  r = sqrt(x*x + y*y);

  // next, the angle.  We have to deal with the case of x = 0 carefully;
  //   if both x and y are zero, return an angle of 0.
  if (x == 0) 
    if (y > 0)
      theta = pi/2;
    elseif (y < 0)
      theta = 3*pi/2;
    else
      theta = 0;
    end
  else
    theta = atan(y/x);
  end

  // now we have to find the proper quadrant, and adjust the angle
  //   theta if necessary
  if ((x > 0) & (y > 0))
     
    // quadrant 1: no adjustment needed

  elseif ((x > 0) & (y < 0)) 
   
    // quadrant 4: we must add 2pi to the angle returned by the 'atan' function
    theta = theta + 2*pi;

  elseif ((x < 0) & (y > 0))

    // quadrant 2: must add pi to the angle returned by 'atan' function
    theta = theta + pi;

  elseif ((x < 0) & (y < 0))

    // quadrant 3: must add pi to angle returned by 'atan' function
    theta = theta + pi;

  end

endfunction
