Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have been trying to solve a value error (setting array element to sequence) but anything I try wont correct the issue. The code revolves around using bessel functions of the first kind and comparing them against the jv(v,z) function but the line of a[k] = B1(theta[k]) is causing my error. I have only been coding in Python 3 for about 6 weeks so I am still fairly new to this.

EDITED INDENTATION

import numpy as np
import scipy
import pylab as plt
from numpy import pi
from scipy import integrate
from scipy import special
from numpy import cos
from numpy import sin

x = 1.0
def B1(theta):
  j1 = cos(theta-x*sin(theta))
  return (j1)

theta = np.linspace(0.0, 20.0, 1000)
a = np.linspace(0.0, 20.0, 1000)
b = a
r = np.linspace(0.0e-6, 1.0e-6, 1000)
x = np.linspace(-1.0, 1.0, 1000)
y = x
Ij = (1.0/pi)*integrate.simps(B1(theta), theta)

for k in range(0, len(theta)):
  if k>0: 
    a[k] = B1(theta[k])
    b[k] = scipy.special.jv(1, theta[k])
  else:
    a[k] = 1.0
    b[k] = 1.0
plt.plot(theta, a,'r-')
plt.plot(theta, b,'k-')
plt.show() 

Any help by anyone would be greatly appreciated as I have been struggling over this for several hours now.

Edit: For reference my trace log is as follows File "Ag1P1.py", line 32, in a[k] = B1(theta[k]) ValueError: setting an array element with a sequence.

I think now my error lies in where I use the following

     x = 1.0
    def B1(theta):
      j1 = cos(theta-x*sin(theta))
      return(j1)

I'm uncertain as to why but any values I input as such;

      a[k] = B1(theta[k])

produce this error. Yet I do not know of a solution to go about correcting this.

share|improve this question
2  
Posting the full traceback would help, but from the error it's apparent that you should check the lines where you set an array element (a[k] = and b[k] =). Check the RHS of these assignments to see if the expression is in fact a sequence (array) and not a number. – Lev Levitsky Nov 16 '15 at 19:33
1  
Why do you do b = a? Now b refers to the same object that a refers to. This is probably not the cause of your problem, but surely not what you intend to do. – bourbaki4481472 Nov 16 '15 at 21:37
    
It's just a personal preference with setting up a second array to be used in the program. It's worked for me in the past like that so I just stick with it. – Krios101 Nov 16 '15 at 22:47
    
@Krios101, you should not be doing that. Once you do a[k] = something and then b[k] = something_else, you are actually overwriting whatever you put into a[k]. – bourbaki4481472 Nov 17 '15 at 5:24

As I recall, Bessel functions are defined for positive parameters only. Perhaps you are passing a negative parameter causing a ValueError. I would suggest adding an assert like this as a means of debugging.

for k in range(0, len(theta)):
  if k>0: 
    assert theta[k] > 0
    a[k] = B1(theta[k])
    b[k] = scipy.special.jv(1, theta[k])
  else:
    a[k] = 1.0
    b[k] = 1.0

Also, you should post your stacktrace (what does the error say exactly) and this will help me and others debug.

share|improve this answer
    
Here is the traceback error I got from my terminal Traceback (most recent call last): File "Ag1P1.py", line 29, in <module> a[k] = B1(theta[k]) ValueError: setting an array element with a sequence. I've tried printingt the value types before and when B1 is called at a[k] it stated the value returned (j1) was the array value which confused me earlier. And is the assert function similar to an exit condintion for the if statement? – Krios101 Nov 16 '15 at 20:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.