Archimedean螺旋

问题描述 投票:1回答:1

[我正在尝试定义阿基米德螺旋:当我试图定义切线向量相对于轨道的倾斜角(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()
python numpy polar-coordinates spiral
1个回答
0
投票
表示值的正弦,余弦或正切,但这并不意味着您需要分配给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

not
部分!因此,无需像np.incl一样引用它。只需使用incl

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

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

要计算dydx,您只需要将dydr除以dxdr,但这就是

not
您的代码所要做的!您需要像这样在分母周围加括号:

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

© www.soinside.com 2019 - 2024. All rights reserved.