from pylab import * # ======================================================= # Diffusion equation: du/dt = D * d2u/dx2 # with Dirichlet bounday conditions # FTCS (Forward Time Centered Space), explicit scheme # ======================================================= # equation properties -------------------------- L = 1.0 D = 1.0 # analytical solution -------------------------- def uexacte(t,x): return ?? # space-sampling ------------------------------- nx = ?? dx = L/(nx-1.) x = linspace(0,L,nx) # time-sampling -------------------------------- tmax = ?? cfl = ?? dt = ?? nt = int(tmax/dt)+1 print('--------------------------------------------------') print("CFL={:5.2f} tmax ={:7.4f} --> {:d} iterations en temps".format(cfl,nt*dt,nt)) print('--------------------------------------------------') # initial condition ---------------------------- t=0. u = uexacte(0.,x) plot(x,u,'-o',label='Initial') # Time loop ---------------------------------- for it in range(1,nt+1): # FTCS scheme for inner points (1<=i<=nx-2) u = ?? # Boundary conditions (i=0 and i=nx-1) u = ?? # update time t = t + dt # Optional: plot intermediate some time steps intermediate = 0 if (intermediate): dtplot = 0.1 if (it % int(dtplot/dt) == 0): print("t={:5.2f}".format(t)) p = plot(x,uexacte(t,x)) plot(x,u,'--o',color=p[0].get_color()) # Plot last time step --------------------------- p = plot(x,uexacte(t,x),label='Analytical') plot(x,u,'--o',color=p[0].get_color(),label='Numerical') # Plot setup ------------------------------------ if (not intermediate): ylim([0,1.2*max(uexacte(t,x))]) xlabel('x') ylabel('u(t,x)') title('Explicit FTCS scheme for the heat equation with CFL={:5.2f}'.format(cfl)) legend(loc='upper center',ncol=3) show()