请将以下零散的代码块修改为一个完整的Python代码并将最后的结果输出改为psi随x的变化图像:function square_poten_well(x::Vector, N::Int) L = 2 V0 = -1 mat_V = zeros(N, N) for (i, xx) in enumerate(x) if abs(xx) <= L/2 mat_V[i, i] = V0 end end return mat_V end φ(k, x::Vector, N::Int) = [exp(1.0imkx[i]) for i in 1:N] function Green_func(k, x::Vector, xp::Vector, N::Int) G = ones(ComplexF64, N, N) for i in 1:N G[i, :] = [-1.0im / k * exp(1.0imkabs(x[i]-xp[j]) ) for j in 1:N] end return G end function change_of_var(node, weight, a, b, N) nop = [(b-a) * node[i] / 2.0 + (a+b) / 2.0 for i in 1:N] wp = [(b-a) / 2.0 * weight[i] for i in 1:N] return nop, wp end const N = 298 #节点的个数 const a = -1.5 #积分下限 const b = 1.5 #积分上限 const k_vec = 0.5:1:5.5 # 波数k的取值
时间: 2024-03-08 15:46:40 浏览: 69
以下是将零散的代码块修改为一个完整的Python代码并将最后的结果输出改为psi随x的变化图像的代码:
```python
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[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()
```
输出结果为psi随x的变化图像,其中不同颜色的曲线对应不同的波数k值。
阅读全文