2016-09-23 508 views
0

我在整個轉換過程中將笛卡兒轉換爲極座標,然後返回到笛卡爾以確認我的初始轉換成功。將笛卡爾座標轉換爲極座標 - Matlab

但是,由於某些原因,當我從第三象限轉換回極座標的笛卡爾座標時,我的xy值是錯誤的。

就拿我這部分代碼,例如:

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 

所有其他xy轉換落在其他三個象限內把xy回到正確的順序。

的這部分代碼,但是,並把xy回到正確的順序:

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 

回答

0

你並不需要檢查的特殊情況。以下代碼正確地將笛卡爾轉換爲任意象限中的點的極座標。

x = -2.075548439; 
y = -2.481775416; 

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

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

而且更容易的是,'help cart2pol','help pol2cart',除非是他的任務。 – Jeon

+0

謝謝,這工作得很好,並極大地縮短了我的劇本!我唯一需要做的就是從360中減去負角度,一旦複製並粘貼到Excel中以獲得與我的其餘數據匹配的正角度。 – user1574598

相關問題