gistlib
main.m% Define parameters L = 1; % Length of the domain nx = 100; % Number of spatial points dx = L/nx; x = linspace(0, L, nx); T = 1; % Total time dt = 0.001; % Time step size nt = T/dt; D = 0.1; % Diffusion coefficient u = 1; % Velocity % Initial condition C = zeros(1,nx); C(1:round(nx/4)) = 1; figure; for t = 1:nt C_new = C; for i = 2:nx-1 C_new(i) = C(i) + D*dt*(C(i+1)-2*C(i)+C(i-1))/dx^2 - u*dt*(C(i)-C(i-1))/dx; end C = C_new; plot(x, C); axis([0 L 0 1.2]); title(['Time = ', num2str(t*dt)]); drawnow; end 559 chars31 lines
% Define parameters L = 1; % Length of the domain nx = 100; % Number of spatial points dx = L/nx; x = linspace(0, L, nx); T = 1; % Total time dt = 0.001; % Time step size nt = T/dt; D = 0.1; % Diffusion coefficient u = 1; % Velocity % Initial condition C = zeros(1,nx); C(1:round(nx/4)) = 1; figure; for t = 1:nt C_new = C; for i = 2:nx-1 C_new(i) = C(i) + D*dt*(C(i+1)-2*C(i)+C(i-1))/dx^2 - u*dt*(C(i)-C(i-1))/dx; end C = C_new; plot(x, C); axis([0 L 0 1.2]); title(['Time = ', num2str(t*dt)]); drawnow; end
gistlibby LogSnag