I am trying to perform integration of a function in order to find the mean position. However when I perform the integrating with quad I get problems of dimensions not matching. When I run the function on its own it works without a problem. However, when the function is used by the quad integration scheme it gives the error of dimenions mismatch. I will post the completely functional code below and the error message and I hope someone can tell me whats going wrong and how I can fix it.
Please let me know if anything is unclear so I can add more information.
import numpy as np
import time
from scipy.sparse.linalg import eigsh
from scipy.sparse.linalg import spsolve
from scipy.sparse import diags
from scipy.sparse import identity
import scipy.sparse as sp
from scipy.integrate import quad
x0 = 15
v = 16
sigma2 = 5
tmax = 4
def my_gaussian(x, mu=x0, var=5):
return np.exp(-((x - mu)**2 / (2*var))+(1j*v*x/2))
L = 100
N = 3200
dx = 1/32
x = np.linspace(0, L, N)
func = lambda x: my_gaussian(x)*my_gaussian(x).conjugate()
C,e = quad(func, 0, L)
def task3(x):
psi_0 = (C**-(1/2))*my_gaussian(x)
H = (dx**-2)*diags([-1, 2, -1], [-1, 0, 1], shape=(N, N))
H = sp.lil_matrix(H)
H[0,0]=0.5*H[0,0]
H[N-1,N-1]=0.5*H[N-1,N-1]
lam, phi = eigsh(H, 400, which="SM")
a = phi.T.dot(psi_0)
psi = phi.dot(a*np.exp(-1j*lam*tmax))
return psi*psi.conjugate()
func = lambda x: task3(x)*x
N1,e = quad(func, 0, L)
