


convert the rectangular coords (x, y) to polar coordinates
USAGE: [r,theta]=rectToPolarGood(1, sqrt(3))
r =
2.0000
theta =
1.0472
>> theta*180/pi
ans =
60.0000
=================================================================
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

function[r, theta] = rectToPolarGood(x, y) % convert the rectangular coords (x, y) to polar coordinates %USAGE: [r,theta]=rectToPolarGood(1, sqrt(3)) %r = % 2.0000 %theta = % 1.0472 %>> theta*180/pi %ans = % 60.0000 %================================================================= % 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 % 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