%%%%%%%%%%%%%%%%%%%%%%%%% %diffuse.m % %solving a time-space problem for diffusion %RK4 in time %three-point centered difference in space % %Greg Gerbi %24 April, 2012 %%%%%%%%%%%%%%%%%%%%%%%%% dx = 0.01; x = (-10:dx:10).'; lenx = length(x); dt = 0.1; t = (0:dt:1000).'; lent = length(t); K = 1e-6; %Dirichlet boundary condition CB = 0; %Initial conditions C = zeros(size(x)); C(lenx./2) = 1; for jn = 2:lent; d2Cdx2 = zeros(size(x)); d2Cdx2(2:end-1) = (1./dx.^2) .* (C(1:end-2) - C(2:end-1) + C(3:end)); %Dirichlet conditions d2Cdx2(1) = CB; d2Cdx2(end) = CB; end