【问题标题】:Archimedean spiral阿基米德螺线
【发布时间】:2016-02-18 15:45:14
【问题描述】:

我正在尝试定义阿基米德螺旋线:当我尝试定义切线向量与轨道的倾角 (incl) 时(即:tan(incl))

我收到一个错误:

'numpy.ufunc' 对象不支持项目分配" 和“不能分配给函数调用”

当我想计算cos(incl)sin(incl) 时出现同样的错误。 任何建议和帮助。

我的代码是:

T = 100    
N = 10000    
dt = float(T)/N 

D = 2     
DII = 10

    a = 2.     
    v = 0.23    
    omega = 0.2    
    r0 = v/omega    
    t = np.linspace(0,T,N+1)    
    r = v*t    
    theta = a + r/r0    
    theta = omega*t

    x = r * np.cos(omega*t)     
    y = r * np.sin(omega*t) 

    dxdr = np.cos(theta) - (r/r0)*np.sin(theta)    
    dydr = np.sin(theta) + (r/r0)*np.cos(theta)    
    dydx = (r0*np.sin(theta) + r*np.cos(theta))/r0*np.cos(theta) - r*np.sin(theta)

    np.tan[incl] = dydx    
    incl = np.arctan((dydx))

    ### Calculate cos(incl) ,sin(incl) :    
    np.sin[np.incl] = np.tan(np.incl)/np.sqrt(1 + np.tan(np.incl)*2)    
    np.cos[incl] = 1/np.sqrt(1 + np.tan(incl)*2)

 p1, = plt.plot(xx, yy)

i= 0 # this is the first value of the array

Bx = np.array([np.cos(i), -np.sin(i)])                    
By = np.array([np.sin(i), np.cos(i)])                     
n = 1000    
seed(2)

finalpositions = []

for number in range(0, 10):

  x = []    
  y = []    
  x.append(0)     
  y.append(0)

  for i in range(n):

      s = np.random.normal(0, 1, 2) 

      deltaX = Bx[0]*np.sqrt(2*DII*dt)*s[0] + Bx[1]*np.sqrt(2*D*dt)*s[1]          
      deltaY = By[0]*np.sqrt(2*DII*dt)*s[0] + By[1]*np.sqrt(2*D*dt)*s[1]

      x.append(x[-1] + deltaX)    
      y.append(y[-1] + deltaY)

  finalpositions.append([x[-1], y[-1]])

 p2, = plt.plot(finalpositions[:,0],finalpositions[:,1],'*')

plt.show()

【问题讨论】:

  • 我不认为 np.tan[incl]np.sin[np.incl] 做你认为它做的事情
  • 我曾尝试像 np.sin(np.incl) 那样写,但出现错误:无法分配给函数调用
  • 这是因为np.sin(np.incl) 是一个返回 值的函数调用。你不能给它分配任何东西
  • 我试图编辑您的问题以修复代码的缩进,但我不得不放弃......这太乱了!您对空行的使用也很不合常规。你能自己解决这些问题吗?
  • 单独说明一下,number 上的 for 循环可能与您的意图不同,也许您想将 x=[]y=[] 放在 for 循环之前,你不会吗?

标签: python numpy polar-coordinates spiral


【解决方案1】:

错误信息是正确的,您正在尝试分配给一个函数!我认为您正在尝试计算一个 表示 值的 sin、cos 或 tan 的值,但这并不意味着您需要分配给 np.sin 等。您想要的是计算表示三角函数的值,然后使用反三角函数得到角度:

## np.tan[incl]= dydx  ## np.tan is a function, so you cannot index it like an array, and you should not assign to it.

incl = np.arctan((dydx))  ## this is all you need to get "incl"

### Calculate cos(incl) ,sin(incl) :
## NOTE: you already have the angle you need!!  No need for a complicated formulate to compute the sin or cos!
sin_incl = np.sin(incl)
cos_incl = np.cos(incl)

编辑:还有一条评论...np 是一个包含许多数字方法的模块。当您计算incl 时,它不是np 的一部分!所以没有必要像np.incl那样引用它。只需使用incl

EDIT2:我发现的另一个问题是这一行:

dydx = (r0*np.sin(theta) + r*np.cos(theta))/r0*np.cos(theta) - r*np.sin(theta)

要计算 dydx,您只需将 dydr 除以 dxdr,但这不是您的代码所做的!你需要像这样围绕分母的括号:

dydx = (r0*np.sin(theta) + r*np.cos(theta))/(r0*np.cos(theta) - r*np.sin(theta))

【讨论】:

  • 好的,谢谢你的解释我知道这一点,但关键是我想在其他函数中使用这个角度(包括)包含 sin(incl) 和 cos(incl),当我试图使用incl values 给了我一个错误:解包的值太多
  • 没关系...只需使用上面计算的sin_inclcos_incl 值。一旦知道incl,该值的所有三角函数都已确定,您不必为它们求解。
  • Bx= np.array([np.cos(incl), -np.sin(incl)]) By= np.array([np.sin(incl), np.cos(incl) )])
  • @gariepyn= 1000 seed(2) finalpositions=[] for number in range(0, 100): x=[] y=[] x.append(20) #append() 添加元素到列表的末尾。 y.append(20)#10 或 20 例如 x & y 从 10 或 20 开始,对于范围 (n) 中的 i:s = np.random.normal(0, 1, 2) deltaX= Bx[0] *np.sqrt(2*DIIdt)*s[0] + Bx[1]*np.sqrt(2*Ddt)*s[1] deltaY= By[0]*np .sqrt(2*DIIdt)*s[0] + By[1]*np.sqrt(2*Ddt)*s[1] x.append(x[-1] + deltaX) y.append(y[-1] + deltaY) finalpositions.append([x[-1], y[-1]])
  • 您需要使用代码edit 您的原始问题...它在 cmets 中不可读。 :)
猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 2019-07-29
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 2013-03-04
  • 1970-01-01
相关资源
最近更新 更多