请修改以下代码输出正确结果不能报错:import matplotlib.pyplot as plt import numpy as np y = x[500:1001] V = potential(y) def f1(E): psi = np.zeros(len(y)) psi[0] = 1 psi[1] = (V[0] - E) * (dx**2) + 1 for i in range(1,len(y)-1): psi[i+1] = 2 * psi[i] * (dx**2) * (V[i] - E) - psi[i-1] + 2 * psi[i] return psi[-1] def f2(E): psi = np.zeros(len(y)) psi[0] = 0 psi[1] = 1 for i in range(1,len(y)-1): psi[i+1] = 2 * psi[i] * (dx**2) * (V[i] - E) - psi[i-1] + 2 * psi[i] return psi[-1] p = np.zeros(300) z = np.linspace(0, 300, 301) for E in range(0,300): p[E] = f1(E) / 1 y = np.zeros(300) for E in range(0,300): y[E] = f2(E) / 1 with plt.style.context(['science', 'ieee']): plt.plot([z[0],z[300]],[0,0],'y') plt.plot(p,'b') plt.plot(y,'r') plt.ylim(-100,100) plt.xlim(0,100) from scipy.optimize import fsolve E = fsolve(f1, 5) print(E)
时间: 2023-10-30 08:07:21 浏览: 114
matplotlib.pyplot绘图显示控制方法
5星 · 资源好评率100%
import matplotlib.pyplot as plt
import numpy as np
def potential(x):
# Your potential function here
return x
y = np.linspace(0, 1000, 1001)
V = potential(y)
def f1(E):
dx = y[1] - y[0]
psi = np.zeros(len(y))
psi[0] = 1
psi[1] = (V[0] - E) * (dx**2) + 1
for i in range(1,len(y)-1):
psi[i+1] = 2 * psi[i] * (dx**2) * (V[i] - E) - psi[i-1] + 2 * psi[i]
return psi[-1]
def f2(E):
dx = y[1] - y[0]
psi = np.zeros(len(y))
psi[0] = 0
psi[1] = 1
for i in range(1,len(y)-1):
psi[i+1] = 2 * psi[i] * (dx**2) * (V[i] - E) - psi[i-1] + 2 * psi[i]
return psi[-1]
p = np.zeros(300)
z = np.linspace(0, 300, 301)
for E in range(0,300):
p[E] = f1(E) / 1
y = np.zeros(300)
for E in range(0,300):
y[E] = f2(E) / 1
with plt.style.context(['science', 'ieee']):
plt.plot([z[0],z[300]],[0,0],'y')
plt.plot(p,'b')
plt.plot(y,'r')
plt.ylim(-100,100)
plt.xlim(0,100)
from scipy.optimize import fsolve
E = fsolve(f1, 5)
print(E)
阅读全文