Skip to content
Advertisement

Integrating Matrix Elements TypeError: f() takes 1 positional argument but 3 were given

I’m working on a linear variational problem for a general PIB and I keep encountering the same problem, and I know its a rather simple solution. Any suggestions?

   import numpy as np
    
    import scipy.integrate as sp
    #Parameters
    alpha = 0.1
    L = 10.0
    N = 5
    x = np.linspace(0,L,1000)
    #Matrix elements linear combination components
    def f(x,n,L):
        return(np.sqrt(2./L)*np.sin(n*np.pi*x/L))
    def f1(x,m,L):
        return( np.sqrt(2./L)*np.sin(m*np.pi*x/L))
    
    #Linear Potential
    def V(x):
        return( alpha*x)
    #Matrix element functions to integrate
    def Int_1(x,n,m,L):
        return(f1(x,m,L)*f(x,n,L)+f1(x,m,L)*V(x)*f(x,n,L))
    def Int_2(x,m,n,L):
        return(f1(x,m,L)*V(x)*f(x,n,L))
    # Tring to integrate the matrix components
    def c(m,n,L):
        return(sp.quad(Int_1,0,L, args = (m,n,L), limit = 100)[0])
    def c1(m,n,L):
        return(sp.quad(Int_2,0,L, args = (m,n,L), limit = 100)[0])
    # Generating the matrix
    def cal_Hmn(m,n):
        if m == n:
            c(m,n,L)
        elif(m+n)%2 ==1:
            c1(m,n,L)
        else:
            return(0)
    
    #Filling the Matrix
    H = np.zeros((N,N), float)
    for i in range(N):
        for j in range(N):
            H[i,j] = cal_Hmn(i+1, j+1)

The problem is 100% in the integration of the matrix elements, but I cannot figure out how to rectify it.

Thanks!

Advertisement

Answer

It runs for me:

print(H)

returns

[[nan nan  0. nan  0.]
 [nan nan nan  0. nan]
 [ 0. nan nan nan  0.]
 [nan  0. nan nan nan]
 [ 0. nan  0. nan nan]]

Try updating your packages

I am using PyCharm and Python 3.8

enter image description here

JP

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement