phi_n = np.pad(phi_n,((blk,blk),(blk,blk)))
时间: 2024-05-20 13:15:27 浏览: 8
这行代码的作用是对数组 phi_n 进行边界填充,使得数组在两个维度(行和列)上各增加 blk 个元素的填充。具体地,np.pad() 函数的第一个参数 phi_n 是要进行填充的数组,第二个参数 ((blk,blk),(blk,blk)) 是填充的方式,其中第一个元组表示在第一个维度(行)上填充 blk 个元素,第二个元组表示在第二个维度(列)上填充 blk 个元素。填充的元素默认为 0。
相关问题
请修改以下代码使它输出正确的结果不能报错: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()
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()
优化这段import numpy as np import matplotlib.pyplot as plt %config InlineBackend.figure_format='retina' def generate_signal(t_vec, A, phi, noise, freq): Omega = 2*np.pi*freq return A * np.sin(Omega*t_vec + phi) + noise * (2*np.random.random def lock_in_measurement(signal, t_vec, ref_freq): Omega = 2*np.pi*ref_freq ref_0 = 2*np.sin(Omega*t_vec) ref_1 = 2*np.cos(Omega*t_vec) # signal_0 = signal * ref_0 signal_1 = signal * ref_1 # X = np.mean(signal_0) Y = np.mean(signal_1) # A = np.sqrt(X**2+Y**2) phi = np.arctan2(Y,X) print("A=", A, "phi=", phi) # t_vec = np.linspace(0, 0.2, 1001) A = 1 phi = np.pi noise = 0.2 ref_freq = 17.77777 # signal = generate_signal(t_vec, A, phi, noise, ref_freq) # lock_in_measurement(signal, t_vec, ref_freq)
import numpy as np
import matplotlib.pyplot as plt
%config InlineBackend.figure_format='retina'
def generate_signal(t_vec, A, phi, noise, freq):
Omega = 2*np.pi*freq
return A * np.sin(Omega*t_vec + phi) + noise * (2*np.random.random)
def lock_in_measurement(signal, t_vec, ref_freq):
Omega = 2*np.pi*ref_freq
ref_0 = 2*np.sin(Omega*t_vec)
ref_1 = 2*np.cos(Omega*t_vec)
signal_0 = signal * ref_0
signal_1 = signal * ref_1
X = np.mean(signal_0)
Y = np.mean(signal_1)
A = np.sqrt(X**2+Y**2)
phi = np.arctan2(Y,X)
print("A=", A, "phi=", phi)
t_vec = np.linspace(0, 0.2, 1001)
A = 1
phi = np.pi
noise = 0.2
ref_freq = 17.77777
signal = generate_signal(t_vec, A, phi, noise, ref_freq)
lock_in_measurement(signal, t_vec, ref_freq)