Python 3.6:将极坐标转换为笛卡尔坐标



对于极坐标(13,22.6°(,我应该得到(12,5(的笛卡尔坐标答案,但我没有。我的代码有什么问题?我知道角度 phi 应该以度数表示,我试图在代码中实现它,但它没有给我正确的答案。

import math
def pol2cart(rho, phi):
x = rho * math.cos(math.degrees(phi))
y = rho * math.sin(math.degrees(phi))
return(x, y)
rho=float(input("Enter the value of rho:"))
phi=float(input("Enter the value of phi in degrees:"))
print("For the polar coordinates x = ", rho, "and y = ", phi, "the cartesian coordinates are = ", pol2cart(rho, phi))

提前谢谢。

import math
def pol2cart(rho, phi):
x = rho * math.cos(math.radians(phi))
y = rho * math.sin(math.radians(phi))
return(x, y)
rho=float(input("Enter the value of rho:"))
phi=float(input("Enter the value of phi in degrees:"))
print("For the polar coordinates x = ", rho, "and y = ", phi, "the cartesian coordinates are = ", pol2cart(rho, phi))

使用 math.radians 将角度从度转换为弧度。由于您要求输入度数,但 math.cos 以弧度为单位输入角度。

import math
import cmath
def pol2cart(rho, phi):
x = math.radians(phi)
z = cmath.rect(rho,x)
return(z)
rho=float(input("Enter the value of rho:"))
phi=float(input("Enter the value of phi in degrees:"))
print("For the polar coordinates x = ", rho, "and y = ", phi, "the cartesian coordinates are = ", pol2cart(rho,phi))

最新更新