1

I am trying to define the archimedean spiral: when I'm trying to define the inclination angle (incl) of the tangent vector to the orbit ( i.e: tan(incl))

I'm getting an error:

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

the same error when I want to calculate cos(incl), and sin(incl). Any suggestions and helps.

My code is:

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()
smci
  • 32,567
  • 20
  • 113
  • 146
Olives99
  • 64
  • 1
  • 10
  • I don't think `np.tan[incl]` or `np.sin[np.incl]` does what you think it does – OneCricketeer Feb 18 '16 at 15:52
  • I have tried to write it like np.sin(np.incl), but i got an error: can't assign to function call – Olives99 Feb 18 '16 at 15:57
  • That's because `np.sin(np.incl)` is a function call that *returns* a value. You can't assign anything to it – OneCricketeer Feb 18 '16 at 16:10
  • 1
    I've tried to edit your question to fix the indentation of the code but I had to give up... it is too much of a mess! Your usage of blank lines is quite unconventional too. Could you please fix these issues yourself? – gboffi Feb 19 '16 at 11:44
  • A separate remark, the `for` loop on `number` is possibly doing something different from your intention, maybe you want to put `x=[]` and `y=[]` just before the `for` loop, won't you? – gboffi Feb 19 '16 at 11:46
  • I tried to remove uneeded blank lines, and fix spaces before/after operators and parentheses. But please fix the indentation of the lines from `a = 2. ...` onwards, and use 2 spaces per indentation level in your for-loops. Also, you are referencing `np.incl` instead of `incl` in the line `np.sin[np.incl] = ...`, seems like a mistake. – smci Apr 29 '20 at 03:06
  • Hey by the way, [Matplotlib allows polar-coordinates `subplot(..., polar=True)`](https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.subplot.html), so maybe you can avoid most of this coordinate work. – smci Apr 29 '20 at 03:10

1 Answers1

0

The error message is correct, you are trying to assign to a function! I think you're trying to compute a value that represents the sin, cos or tan of a value, but that doesn't mean you need to assign to np.sin, etc. What you want is to calculate the value which represents the trig function, and then use the inverse trig function to get the 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)

EDIT: One additional comment...np is a module that contains lots of numeric methods. When you calculate incl, it is not part of np! So there is no need to reference it like np.incl. Just use incl.

EDIT2: Another problem I found is this line:

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

To calculate dydx, you're just dividing dydr by dxdr, but that's not what your code does! You need parens around the denominator like this:

dydx = (r0*np.sin(theta) + r*np.cos(theta))/(r0*np.cos(theta) - r*np.sin(theta))
gariepy
  • 3,576
  • 6
  • 21
  • 34
  • Okay thanks for your explanation I knew this, but the point is I want to use this angles (incl) in other function contain sin(incl) and cos(incl), when I am trying to use incl values gives me an error : too many values to unpack – Olives99 Feb 18 '16 at 16:02
  • That's fine...just use the `sin_incl`, and `cos_incl` values calculated above. Once you know `incl`, all the trig functions of that value are determined, you don't have to solve for them. – gariepy Feb 18 '16 at 16:04
  • Bx= np.array([np.cos(incl), -np.sin(incl)]) By= np.array([np.sin(incl), np.cos(incl)]) – Olives99 Feb 18 '16 at 16:14
  • @gariepyn= 1000 seed(2) finalpositions=[] for number in range(0, 100): x=[] y=[] x.append(20) #append() add elements to the end of the list. y.append(20)#10 or 20 for example for x & y to start from 10 or 20 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]]) – Olives99 Feb 18 '16 at 16:16
  • You need to `edit` your original question with the code...it is not readable in the comments. :) – gariepy Feb 18 '16 at 16:16
  • I'm trying to post a code like the first time but I don't know so far how can I add code in my comment – Olives99 Feb 18 '16 at 16:17
  • Look at the bottom of your original question, you will see three words `share edit flag`...click on `edit` – gariepy Feb 18 '16 at 16:18
  • @AL-Zetoun: I see it now. – gariepy Feb 18 '16 at 16:22
  • It looks like this still isn't all of the code...I don't see where `D` , or `DII` or `dt` are defined, and it looks like you are doing several loops but not using the loop variables for anything. Also, when you assign `Bx= np.array([np.cos(i), -np.sin(i)]) `, are you trying to create a two-column array with the cosine values in the first column, and the -sin values in the second column? – gariepy Feb 18 '16 at 16:26
  • sorry i added it now – Olives99 Feb 18 '16 at 16:30
  • ok, well, still several issues, we'll just go in order. You can't use `Bx= np.array([np.cos(i), -np.sin(i)]) ` like you do...I think you are trying to use `i` as an index, but it isn't defined in your code, and indexing uses square brackets, so that is going to throw an error. Even if you add a loop,it doesn't make sense unless you initialize Bx and you say `Bx[i,:]= np.array([cos_incl[i], -sin_incl[i]]) ` – gariepy Feb 18 '16 at 16:38