from pylab import * # ======================================================= # Burgers equation: du/dt + u*du/dx = 0 # with perdiodic boundary conditions and Lax-Friedrich # scheme # ======================================================= # equation properties -------------------------- L = ?? # Intial condition ----------------------------- def uinit(x): return ?? # space-sampling ------------------------------- nx = ?? dx = L/nx x = linspace(0,L-dx,nx) # time-sampling -------------------------------- tmax = ?? cfl = ?? dt = ?? # small change in dt to match exactly the last time 'tmax' nt = int(tmax/dt); dt = tmax/nt; cfl = dt/dx print '--------------------------------------------------' print "CFL=%5.2f tmax =%7.4f --> %i iterations en temps"%(cfl,nt*dt,nt) print '--------------------------------------------------' # initial condition ---------------------------- t=0. u = uinit(x) plot(x,u,'-o',label='Initial') # Time loop ---------------------------------- for i in range(1,nt+1): # Lax-Friedrichs u = ??? # upwind # u = ?? # update time t=t+dt # Optional: plot some intermediate time steps intermediate = 1 if (intermediate): dtplot = 0.2 if (i % int(dtplot/dt) == 0): print "t=%5.2f"%t plot(x,u,'--o') # Plot last time step --------------------------- plot(x,u,'--o',label='Numerical') # Plot setup ------------------------------------ plot(x, ones(nx),color='k',ls='--') plot(x,zeros(nx),color='k',ls='--') plot(x,-ones(nx),color='k',ls='--') xlabel('x') ylabel('u(t,x)') ylim([-1.1,1.3]) title('Explicit scheme for the Burgers equation with CFL=%5.2f'%cfl) legend(loc='upper center',ncol=3) show()