Python 积分矩阵元素TypeError:f()接受1个位置参数,但给出了3个
我正在研究一个一般PIB的线性变分问题,我经常遇到同样的问题,我知道这是一个相当简单的解决方案。有什么建议吗Python 积分矩阵元素TypeError:f()接受1个位置参数,但给出了3个,python,Python,我正在研究一个一般PIB的线性变分问题,我经常遇到同样的问题,我知道这是一个相当简单的解决方案。有什么建议吗 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
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)
问题是100%的矩阵元素的积分,但我不知道如何纠正它
谢谢 它为我运行:
print(H)
返回
[[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]]
尝试更新您的软件包
我正在使用PyCharm和Python 3.8
JP您的集成实际上正在工作,只需尝试打印
c1
和c
的返回值即可。问题是,cal\u Hmn
从不返回这些值,它只是调用这些函数,而没有说要返回值。由于不在这些位置返回,因此函数隐式返回None
,该值在numpy数组中变为nan
函数应写成:
def cal_Hmn(m,n):
if m == n:
return(c(m,n,L))
elif(m+n)%2 ==1:
return(c1(m,n,L))
else:
return(0)
是的,成功了,谢谢!。你知道为什么我得到的矩阵元素是“nan”,而不是返回积分的数字吗?减小积分跨度应该会有很大帮助