Python 阿基米德螺线

Python 阿基米德螺线,python,numpy,polar-coordinates,spiral,Python,Numpy,Polar Coordinates,Spiral,我试图定义阿基米德螺旋线:当我试图定义切线向量与轨道的倾角(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 =

我试图定义阿基米德螺旋线:当我试图定义切线向量与轨道的倾角(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#这是数组的第一个值

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等。您需要的是计算表示trig函数的值,然后使用逆trig函数来获得angle:

## 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,但代码不是这样做的!分母周围需要parens,如下所示:

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

我不认为
np.tan[incl]
np.sin[np.incl]
做你认为它做的事我试着像np.sin(np.incl)那样写它,但我得到了一个错误:无法分配给函数调用那是因为
np.sin(np.incl)
是一个返回值的函数调用。你不能给它赋值。我试图编辑你的问题来修正代码的缩进,但我不得不放弃……太乱了!你对空行的使用也很不传统。你能自己解决这些问题吗?另一句话,
for
循环de>number可能做了一些与您的意图不同的事情,可能您想在
for
循环之前放置
x=[]
y=[]
,不是吗?好的,谢谢您的解释,我知道这一点,但重点是我想在其他函数中使用这个角度(incl)包含sin(incl)和cos(incl),当我试图使用incl值时,给了我一个错误:太多的值无法解包,这很好…只需使用上面计算的
sin_incl
cos_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=[]对于范围(01100)中的数字:x=[]y=[]x.append(20)#append()将元素添加到列表的末尾。y.append(20)#10或20例如,对于范围(n)中的i,x&y从10或20开始: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]]您需要
用代码编辑您的原始问题…注释中不可读。:)