Python sequence error
Question: I have written a code to solve various integrals using the midpoint method. I
had it working for some other functions, however when trying to compute this
particular function's integral (you will see it within the code), I am running
into this error: "setting an array element with a sequence." If someone could
point out what is causing this issue, it would be much appreciated.

Edit: I have marked the line where the error is occurring.

Here is my code:

    
    
    from matplotlib.pylab import *
    
    
    N = 1000
    xi = 1.0
    xf = 4.0
    dx = (xf - xi)/N
    x = zeros(N+1)
    F = zeros(N+1)
    k = m = 1
    Z = 2*sqrt((2*m)/2)
    A = 1
    x[0] = xi
    F[0] = 0.0
    for i in range(1,N+1):
        x[i] = x[i-1] + dx
        xmid = (x[i] + x[i-1])/2.0
        F[i] = F[i-1] + dx*(Z*sqrt(k*A**4 - k*x**4)) #error here
    print 'F at', xf, ' = ', F[N]
    plot(F,x,'b')
    xlabel('F')
    ylabel('x')
    show()
    


Answer: At runtime on the first iteration of the loop the problematic line has the
addition of an array and a scalar which broadcast to a array:

    
    
    F[i-1] = 0.0
    dx*(Z*sqrt(k*A**4 - k*x**4)) = array([ 0.   ,    nan,  0.006, ...,  0.006,  0.006,  0.006])
    

So at runtime it's trying assign an array to the index, which isn't well-
defined for the one dimensional `F` array.

    
    
    F[i] = array([ 0.   ,    nan,  0.006, ...,  0.006,  0.006,  0.006])
    

Check your formula, but I'm guessing you probably intended to do something
like this instead:

    
    
    dx*(Z*sqrt(k*A**4 - k*x[i]**4)) 
    

