% Forward time, Centered space scheme % for diffusion equation with fixed BC function utuxx_ftcx; D = 2.02; dt = 1; dx = 2; time = 400 % Time value to solve for length = 50 % (L) Physical length of the metal wire Ncells = length / dx + 1; % (m) Nsteps = time / dt + 1; L = D*dt/(dx)^2 u(Nsteps, Ncells) = 0; u(1,:) = 25; % initial condition (time=0) u(:,1) = 0; % left BC (x=0) u(:,Ncells) = 100; % right BC (x=L) for n = 1:(Nsteps-1) for k = 2:(Ncells-1) u(n+1,k) = L*u(n,k-1) + (1-2*L)*u(n,k) + L*u(n,k+1); end end %figure %surf(u(1:(Nsteps/12.5):Nsteps,1:(Ncells/12.5):Ncells)); %view(-154,26); %figure; plot(u(1:(Nsteps-1)/5:Nsteps,:)','.-');