# 循环功能仅返回最后一个值

``````def HHmodel(I,length, area):

v = []
m = []
h = []
n = []
a= []
dt = 0.05
t = np.linspace(0,100,length)

#constants
ENa=50 #miliVolt
EK=-77  #miliVolt
El=-54 #miliVolt
g_Na=120 *(1+0.1*j)#mScm-2
g_K=36 *(1+0.1*j)#mScm-2
g_l=0.03 *(1+0.1*j)#mScm-2

def alphaN(v):
return 0.01*(v+50)/(1-np.exp(-(v+50)/10))

def betaN(v):
return 0.125*np.exp(-(v+60)/80)

def alphaM(v):
return 0.1*(v+35)/(1-np.exp(-(v+35)/10))

def betaM(v):
return 4.0*np.exp(-0.0556*(v+60))

def alphaH(v):
return 0.07*np.exp(-0.05*(v+60))

def betaH(v):
return 1/(1+np.exp(-(0.1)*(v+30)))

#Initialize the voltage and the channels :
v.append(-60)
m0 = alphaM(v[0])/(alphaM(v[0])+betaM(v[0]))
n0 = alphaN(v[0])/(alphaN(v[0])+betaN(v[0]))
h0 = alphaH(v[0])/(alphaH(v[0])+betaH(v[0]))

#t.append(0)
m.append(m0)
n.append(n0)
h.append(h0)

#solving ODE using Euler's method:
for i in range(1,len(t)):
m.append(m[i-1] + dt*((alphaM(v[i-1])*(1-m[i-1]))-betaM(v[i-1])*m[i-1]))
n.append(n[i-1] + dt*((alphaN(v[i-1])*(1-n[i-1]))-betaN(v[i-1])*n[i-1]))
h.append(h[i-1] + dt*((alphaH(v[i-1])*(1-h[i-1]))-betaH(v[i-1])*h[i-1]))
gNa = g_Na * h[i-1]*(m[i-1])**3
gK=g_K*n[i-1]**4
gl=g_l
INa = gNa*(v[i-1]-ENa)
IK = gK*(v[i-1]-EK)
Il=gl*(v[i-1]-El)
v.append(v[i-1]+(dt)*((1/Cm)*(I[i-1]-(INa+IK+Il))))
a.append(area)
#v.append(v[i-1]+(dt)*((1/Cm)*(I-(INa+IK+Il))))
return v,t,a
``````

``````for j in range(0, 10):
barcode = np.zeros(length)
noisyI = np.random.normal(0,9,length)
area = 1.0 + 0.01*j
v,t,area = HHmodel(noisyI,length,area)
``````

BR

• 呐爱 回复

You assign to the variables `v`,`t` and `area` every loop, so the value gets overwritten. If you would like them to be lists, you can append to a list in the `for` loop instead, like this:

``````v, t, area =[], [], []

for j in range(0, 10):
# calculate stuff
res = HHmodel(noisyI,length,area)
v.append(res[0])
t.append(res[1])
area.append(res[2])
``````