简体   繁体   中英

Python 3.6: Converting polar coordinates to Cartesian coordinates

For the polar coordinates (13, 22.6°) I should get the cartesian coordinate answer of (12,5), but I don't. What is wrong with my code? I know the angle phi should be expressed in degrees and I tried to implement that in the code but it is not giving me the right answer.

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))

Thanks in advance.

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))

Use math.radians to convert the angles from degrees to radians. Since you are asking for input in degree but math.cos take in angles in radians.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM