from numpy import* from Tkinter import * import time dt = 0.1 dx = 1 D = 1 alpha = D*dt/dx**2 nodes = 500 source_node = nodes/2 x = arange(0, nodes, 1) u_new = zeros(nodes) u = zeros(nodes) ##--Tkinter stuff-- t_start = time.time()#for referencing height = 300.0 width = 400.0 center = height//2 x_factor = width/(nodes-1) y_factor = 400 ##------------------ u[source_node] = 1 def paint(canvas, parent): for i in range (1, nodes-1): u_new[i] = u[i] + alpha*(u[i+1]-2.0*u[i]+u[i-1]) for i in range (0, nodes): u[i] = u_new[i] ##--Tkinter stuff-- xy = [] for i in range(0, nodes): xy.append((int)(i*x_factor)) xy.append((int)(u_new[i]*y_factor)+center) #time.sleep(0.001) c.coords("curve", *xy) parent.after_idle(paint,parent,canvas) ##------------------ #--Tkinter stuff:-- root = Tk() root.title("Animated solution to 1D diffusion equation") root.bind('q','exit') c = Canvas(width=width, height=height, bg='white') c.pack() c.create_line(tag = "curve", *zeros(2*width), fill='blue') root.after(100,paint,root,c) root.mainloop() ##------------------