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.
a[k] =
andb[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:33b = a
? Nowb
refers to the same object thata
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:37a[k] = something
and thenb[k] = something_else
, you are actually overwriting whatever you put intoa[k]
. – bourbaki4481472 Nov 17 '15 at 5:24