允许函数获取向量而不仅仅是单个值

2024-09-30 22:20:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个程序,允许在极坐标和笛卡尔坐标/直角坐标之间进行转换。该代码适用于单个r和θ值,但我希望能够为r和θ输入包含多个值(且长度相同)的向量。谢谢你的帮助

import numpy as np

r=float(input('Enter a value for r:'))
theta=float(input('Enter a theta value in radians:'))

def polar2cart(r,theta):
    x=r*(np.cos(theta))
    y=r*(np.sin(theta))
    return x,y
x,y= polar2cart(r,theta)

Tags: 代码import程序numpyinputvalueasnp