@Guoguo0605
2016-04-17T16:28:43.000000Z
字数 1720
阅读 1293
code
trajectory_of_cannon_shell
from pylab import *
from matplotlib import animation
import time
from math import *
N_A = []
N_B = []
N_B.append(0.0)
NN_A = []
NN_B = []
t = []
tau_A = 1.0
tau_B = 0.
dt = 0
n = 0
def initialize(N_A, N_B, t, _tau_A, _tau_B, _dt, _n):
global tau_A, tau_B, dt, n, time
N_A.append(100)
tau_B = 0.
dt = 0.01
time = 5.0
t.append(0)
n = int(time / dt)
for i in range(n):
t.append(dt*(i+1))
return 0
initialize(N_A, N_B, t, tau_A, tau_B, dt, n)
nnn = arange(0.5,7,0.5)
for ratio in nnn:
tau_B = float(tau_A / ratio)
# print tau_A,tau_B
for i in range(n):
tmp_A = N_A[i]-N_A[i]/tau_A*dt
tmp_B = N_B[i]+dt*(N_A[i]/tau_A-N_B[i]/tau_B)
N_A.append(tmp_A)
N_B.append(tmp_B)
NN_A.append(N_A)
NN_B.append(N_B)
# print len(NN_A[ratio-1])
# print len(t)
N_A=[100,]
N_B=[0,]
#for i in range(10):
# print len(NN_A[i])
fig = figure()
ax1 = fig.add_subplot(1,1,1,xlim=(0, time), ylim=(-3, 100))
# ax1 = fig.add_subplot(2,1,1,xlim=(0, 2), ylim=(-4, 4))
# ax2 = fig.add_subplot(2,1,2,xlim=(0, 2), ylim=(-4, 4))
line, = ax1.plot([], [], color='red', linestyle="-",label="$Nuclei\quad A$")
#lineTh, = ax1.plot([], [], '-b')
line2, = ax1.plot([], [], color='blue', linestyle="-",label="$Nuclei\quad B$")
xlabel('$Time(s)$', fontsize = 16)
ylabel('$Number of Nuclei$',fontsize = 16)
ttt = text(time*0.5, 80,'',fontsize = 18)
#line2Th, = ax1.plot([], [], '-r')
legend(loc='upper left', frameon=False)
def init():
line.set_data([], [])
line2.set_data([], [])
ttt.set_text('')
return line,line2, ttt
# animation function. this is called sequentially
def animate(i):
x = t
y = NN_A[i]
line.set_data(x, y)
x2 = t
y2 = NN_B[i]
line2.set_data(x2, y2)
ttt.set_text( r'$\tau A=1.00$' +'\n' + r'$\tau B=%f$' % float(tau_A / i))
return line,line2,ttt
fram = len(nnn)
anim1=animation.FuncAnimation(fig, animate, init_func=init, frames=fram, interval=300, blit=True)
#savefig("chapter.png")
# anim1.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
show()