请修改以下代码使它输出正确的结果不能报错:import numpy as np import matplotlib.pyplot as plt def square_poten_well(x, N): L = 2 V0 = -1 mat_V = np.zeros((N, N)) for i, xx in enumerate(x): if abs(xx) <= L/2: mat_V[i, i] = V0 return mat_V def phi(k, x, N): return [np.exp(1.0jkx[i]) for i in range(N)] def Green_func(k, x, xp, N): G = np.ones((N, N), dtype=np.complex128) for i in range(N): for j in range(N): G[i, j] = -1.0j / k * np.exp(1.0j * k * abs(x[i] - xp[j])) return G def change_of_var(node, weight, a, b, N): nop = [(b-a) * node[i] / 2.0 + (a+b) / 2.0 for i in range(N)] wp = [(b-a) / 2.0 * weight[i] for i in range(N)] return nop, wp N = 298 # 节点的个数 a = -1.5 # 积分下限 b = 1.5 # 积分上限 k_vec = np.arange(0.5, 6.0) # 波数k的取值 x = np.linspace(a, b, N) dx = (b - a) / (N - 1) psi = np.zeros((len(k_vec), N)) for i, k in enumerate(k_vec): V = square_poten_well(x, N) phi_k = phi(k, x, N) G = Green_func(k, x, x, N) node, weight = np.polynomial.legendre.leggauss(N) node = np.flip(node, axis=0) weight = np.flip(weight, axis=0) xp, wp = change_of_var(node, weight, a, b, N) m = np.matmul(np.matmul(np.diag(phi_k), G), np.diag(phi_k.conj())) * dx psi_k = np.linalg.solve(V - k**2 * np.eye(N), np.matmul(m, phi_k)) psi[i] = np.abs(psi_k)**2 fig, ax = plt.subplots() for i, k in enumerate(k_vec): ax.plot(x, psi[i], label=f'k={k:.1f}') ax.set_xlabel('x') ax.set_ylabel('$|\psi|^2$') ax.legend() plt.show()
时间: 2024-04-26 10:25:25 浏览: 183
import numpy as np
import matplotlib.pyplot as plt
def square_poten_well(x, N):
L = 2
V0 = -1
mat_V = np.zeros((N, N))
for i, xx in enumerate(x):
if abs(xx) <= L/2:
mat_V[i, i] = V0
return mat_V
def phi(k, x, N):
return np.exp(1.0j * k * x)
def Green_func(k, x, xp, N):
G = np.ones((N, N), dtype=np.complex128)
for i in range(N):
for j in range(N):
G[i, j] = -1.0j / k * np.exp(1.0j * k * abs(x[i] - xp[j]))
return G
def change_of_var(node, weight, a, b, N):
nop = [(b-a) * node[i] / 2.0 + (a+b) / 2.0 for i in range(N)]
wp = [(b-a) / 2.0 * weight[i] for i in range(N)]
return nop, wp
N = 298 # 节点的个数
a = -1.5 # 积分下限
b = 1.5 # 积分上限
k_vec = np.arange(0.5, 6.0, 0.1) # 波数k的取值
x = np.linspace(a, b, N)
dx = (b - a) / (N - 1)
psi = np.zeros((len(k_vec), N))
for i, k in enumerate(k_vec):
V = square_poten_well(x, N)
phi_k = phi(k, x, N)
G = Green_func(k, x, x, N)
node, weight = np.polynomial.legendre.leggauss(N)
node = np.flip(node, axis=0)
weight = np.flip(weight, axis=0)
xp, wp = change_of_var(node, weight, a, b, N)
m = np.matmul(np.matmul(np.diag(phi_k), G), np.diag(phi_k.conj())) * dx
psi_k = np.linalg.solve(V - k**2 * np.eye(N), np.matmul(m, phi_k))
psi[i] = np.abs(psi_k)**2
fig, ax = plt.subplots()
for i, k in enumerate(k_vec):
ax.plot(x, psi[i], label=f'k={k:.1f}')
ax.set_xlabel('x')
ax.set_ylabel('$|\psi|^2$')
ax.legend()
plt.show()
阅读全文