Exemplo n.º 1
0
    return ln,


def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return ln,


fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'ro', animated='True')
ani = animate(fig,
              update,
              frames=np.linspace(0, 2 * np.pi, 128),
              init_func=init,
              blit=True,
              interval=1)
plt.show()


def update(frame):
    xdata.append(frame)
    ydata.append(Decay(frame))
    ln.set_data(xdata, ydata)
    return ln,


fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = plt.plot([], [], 'r', animated='True')
    randspin = spins[randrow][randcol]
    energy = 0
    if randrow == 0:
        energy += -randspin * spins[randrow + 1][randcol]
    if randrow == numx - 1:
        energy += -randspin * spins[randrow - 1][randcol]
    if randcol == 0:
        energy += -randspin * spins[randrow][randcol + 1]
    if randcol == numy - 1:
        energy += -randspin * spins[randrow][randcol - 1]
    if randrow != (numx - 1) and randrow != 0:
        if randcol != (numy - 1) and randcol != 0:
            energy += -randspin * spins[randrow + 1][randcol]
            energy += -randspin * spins[randrow - 1][randcol]
            energy += -randspin * spins[randrow][randcol + 1]
            energy += -randspin * spins[randrow][randcol - 1]
    if randspin == -1:
        p_down = np.exp(
            -energy / T) / (np.exp(-energy / T) + np.exp(energy / T))
        p_up = np.exp(energy / T) / (np.exp(-energy / T) + np.exp(energy / T))
    if randspin == 1:
        p_up = np.exp(-energy / T) / (np.exp(-energy / T) + np.exp(energy / T))
        p_down = np.exp(
            energy / T) / (np.exp(-energy / T) + np.exp(energy / T))
    spins[randrow][randcol] = np.random.choice([-1, 1], p=[p_down, p_up])
    Q.set_UVC(np.zeros((numx, numy)), spins, spins)
    return (Q)


anim = animate(fig, update, frames=1000, interval=10, repeat=False)
ax.set_xlabel('X (m)')
ax.set_ylabel('Y (m)')
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 2.5)
line1, = ax.plot([], [], color='b')
line2, = ax.plot([], [], color='g')
line3, = ax.plot([], [], color='r')
sca1 = ax.scatter([], [], color='b')
sca2 = ax.scatter([], [], color='g')
sca3 = ax.scatter([], [], color='r')

#set number of frames
num_frames = 600
step = round(len(x1) / num_frames)  #step size for the plotted lists


def animation(frame):
    '''Function run to draw each frame of the animation'''
    stop = frame * step  #the index at which to stop plotting for current frame
    line1.set_data(x1[0:stop:step], y1[0:stop:step])
    line2.set_data(x2[0:stop:step], y2[0:stop:step])
    line3.set_data(x3[0:stop:step], y3[0:stop:step])
    sca1.set_offsets([x1[stop], y1[stop]])
    sca2.set_offsets([x2[stop], y2[stop]])
    sca3.set_offsets([x3[stop], y3[stop]])
    return (line1, line2, line3, sca1, sca2, sca3)


#call the FuncAnimation function
anim = animate(fig, animation, frames=num_frames, interval=20)
Exemplo n.º 4
0
#fi = np.zeros(num_points)
#for i in range(0, 49):
#    fi[i] = i / 49
#for i in range(0, 49):
#    fi[i + 49] = (49 - i)/49

dfi = np.zeros(num_points)
dt = 0.01
dx = 0.01

#%% animation

x = np.arange(0, num_points)
fig, ax = plt.subplots(1, 1)
line, = ax.plot(x, fi)
ax.set_ylim(-1, 1)
ax.set_title('Wave Equation 1D Numerical Solution')


def update(frame):
    for i in range(1, num_points - 1):
        dfi[i] += (fi[i + 1] - 2 * fi[i] + fi[i - 1]) / dx**2 * dt
    for i in range(1, num_points - 1):
        fi[i] += dfi[i] * dt
    line.set_data(x, fi)
    return (line, )


anim = animate(fig, update, frames=100, interval=50)
Exemplo n.º 5
0
u = np.zeros(num)  #array to hold temperature values for each position
u[49] = 1  #set the middle temperature to 1
du = np.zeros(num)  #array to contain derivative values
x = np.linspace(0, L, num)  #array of x values for plotting
dx = 0.01  #length step
dt = 0.01  #time step


def heat_eqn(u):
    '''Numerical Heat Equation using Finite Differences.'''
    for i in range(1, num - 1):
        du[i] = a * (u[i + 1] + u[i - 1] - 2 * u[i]) / dx**2
    for i in range(1, num - 1):
        u[i] += du[i] * dt


fig, ax = plt.subplots(1, 1)  #create a figure, line, and axes object
line, = ax.plot([], [])
ax.set_title('1D Heat Equation with Fixed Boundaries')
ax.set_ylabel('Temperature')
ax.set_xlabel('Position along Rod')


def update(step):  #animation update function
    line.set_data(x, u)
    heat_eqn(u)
    return (line, )


anim = animate(fig, update, frames=num_steps, interval=20)
plt.show()
ax.set_title('A sample animation: 3D Random Walk')
ax.set_xlim3d(-20, 20)
ax.set_ylim3d(-20, 20)
ax.set_zlim3d(-20, 20)

Nstep = 1000
frame_rate = 1/24 * 1000
num_frames = Nstep
fstep = round(Nstep/num_frames)

def animation(frame):
    line.set_xdata(x[0:frame*fstep])
    line.set_ydata(y[0:frame*fstep])
    line.set_3d_properties(z[0:frame*fstep])
    return(line,)
    
anim = animate(fig1, animation, frames=num_frames, interval=frame_rate)

N_trials = 100
d_average = np.zeros(Nstep)
for i in range(N_trials):
    x,y,z = rw_in_3D(0,0,0,10000)
    for j in range(Nstep):
        d_average[j] += np.sqrt(x[j]**2+y[j]**2+z[j]**2)
d_average = d_average/N_trials
t = np.linspace(0,Nstep,Nstep)
fig = plt.figure('Average distance')
fig.suptitle('Average distance')
plt.xlabel('step')
plt.ylabel('average distance')
plt.plot(t,d_average)
psi_r = np.linspace(xmin, xmax, num)  #array for real psi values
psi_r = np.exp(-psi_r**2 / (2 * sigma**2)) * np.cos(-10 * psi_r)
psi_im = np.linspace(xmin, xmax, num)  #array for imaginary psi values
psi_im = np.exp(-psi_im**2 / (2 * sigma**2)) * np.sin(-10 * psi_im)
dpsi_r = np.zeros(num)  #array for real psi derivative
dpsi_im = np.zeros(num)  #array for imaginary psi derivative
x = np.linspace(xmin, xmax, num)  #x values to plot against
dx = 0.01
dt = 0.00001


def psi_step():
    for i in range(1, num - 1):
        dpsi_r[i] = -(psi_im[i + 1] + psi_im[i - 1] - 2 * psi_im[i]) / dx**2
        dpsi_im[i] = (psi_r[i + 1] + psi_r[i - 1] - 2 * psi_r[i]) / dx**2
    for i in range(0, num):
        psi_r[i] += dpsi_r[i] * dt
        psi_im[i] += dpsi_im[i] * dt


fig, ax = plt.subplots(1, 1)
line, = ax.plot(x, psi_r**2 + psi_im**2)


def update(frame):
    psi_step()
    line.set_data(x, psi_r**2 + psi_im**2)


anim = animate(fig, update, frames=num_steps, repeat=False, interval=20)
Exemplo n.º 8
0
            if i == j:
                continue
            if i in coll:
                continue
            sep = np.linalg.norm(r[i] - r[j])
            if sep < 0.01:
                print('hit')
                print(i, j)
                v[i] -= np.dot(
                    v[i] - v[j], r[i] -
                    r[j]) / np.linalg.norm(r[i] - r[j])**2 * (r[i] - r[j])
                v[j] -= np.dot(
                    v[j] - v[i], r[j] -
                    r[i]) / np.linalg.norm(r[j] - r[i])**2 * (r[j] - r[i])
                coll.append(j)
                while sep < 0.01:
                    r[i] += v[i] * 0.001
                    r[j] += v[j] * 0.001
                    sep = np.linalg.norm(r[i] - r[j])

    ax.scatter(r[:, 0], r[:, 1], color='b')


frame_rate = int(1 / 30 * 1000)
anim = animate(fig,
               animation,
               frames=num_steps,
               repeat=True,
               fargs=[r],
               interval=frame_rate)
    xplot.append(x)
    yplot.append(y)

plt.style.use('default')
fig, (ax, ax2) = plt.subplots(1, 2)
ax.set_xlabel('X Displacement (m)')
ax.set_ylabel('Y Displacement (m)')
ax.set_title('2D Diffusion - Hole in Container')
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
sca = ax.scatter([], [])
ax.set_aspect('equal')
num_frames = num_steps - 1
t, num = [], []
line, = ax2.plot(t, num)
ax2.set_xlabel('Time Step')
ax2.set_ylabel('Number of Particles')
ax2.set_title('Number of Particles vs. Time')


def animation(frame):
    ax.collections[0].remove()
    ax.scatter(xplot[frame], yplot[frame], color='r')
    t.append(frame)
    num.append(len(xplot[frame]))
    ax2.lines[0].remove()
    line, = ax2.plot(t, num, color='b')


anim = animate(fig, animation, frames=num_frames, interval=50, repeat=False)