user1574598
user1574598

Reputation: 3881

Converting Cartesian Coordinates To Polar Coordinates - Matlab

I'm having a problem throughout the conversion process of converting Cartesian to Polar, then back to Cartesian in order to confirm that my initial conversion was successful.

But, for some reason when I am converting back to Cartesian coordinates with Polar coordinates from the third quadrant, my x and y values are the wrong way around.

Take this part of my code for instance:

x = -2.075548439;
y = -2.481775416;

if x < 0 && y < 0 % QUAD 3

    radius = sqrt( (x^2) + (y^2) );
    theta = atand( (y*-1)/(x*-1) );
    theta = (270 - theta);
end

x = radius * cosd(theta); 
y = radius * sind(theta);

% answer: x = -2.481775416, and y = -2.075548439

All other x and y conversions that fall within the other three quadrants put x and y back in the correct order.

This part of the code, however, does put x and y back in the correct order:

x = 3.130287009;
y = -0.50613326;

if x > 0 && y < 0 % QUAD 4

    radius = sqrt( (x^2) + (y^2) );
    theta = atand( (y*-1)/(x) );
    theta = (360 - theta);
end

x = radius * cosd(theta); 
y = radius * sind(theta);

% answer: x = 3.130287009, and y = -0.50613326

Upvotes: 0

Views: 1212

Answers (1)

Prakhar
Prakhar

Reputation: 325

You don't need to check for special cases. The following code correctly converts from Cartesian to Polar for points in any quadrant.

x = -2.075548439;
y = -2.481775416;

radius = sqrt( (x^2) + (y^2) );
theta = atan2d( y, x );

x = radius * cosd(theta); 
y = radius * sind(theta);

Upvotes: 0

Related Questions