阿基米德螺线

Archimedean spiral

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

我遇到一个错误:

'numpy.ufunc' object does not support item assignment" and "can't assign to function call"

我想计算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()

错误消息是正确的,您正在尝试分配给一个函数!我认为您正在尝试计算一个 表示 值的 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))