NotEinstein
NotEinstein

Reputation: 59

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.

Upvotes: 0

Views: 7063

Answers (2)

Aaron Mc
Aaron Mc

Reputation: 1

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

Upvotes: 0

Siong Thye Goh
Siong Thye Goh

Reputation: 3586

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.

Upvotes: 2

Related Questions