% Exercise 6. % Solution of the diffusion transport equation % % - mu*u_xx + b*u_x = K, u(0)=0, u(L)=1. % % by a linear finite elements, constant stepsize. L = 1; mu = 0.1; b = 1; K = 1/100; % Number of elements, and the stepsize. M = 10; h = L/M; phi = 0; % Numerical diffusion. N = M-1; Ad = gallery('tridiag',N,-1,2,-1)/h; At = gallery('tridiag',N,-1,0,1)/2; A = mu*(1+phi)*Ad+b*At; F = h*K*ones(N,1); F(end) = F(end) + mu*(1+phi)/h - b/2; % Boundary conditions. u = A\F; u = [0;u;1]; x = linspace(0,L,M+1); % Exact solution tt = linspace(0,L,1001); ue = (1-K*L/b)*exp(-b*(L-tt)/mu).*(1-exp(-b*tt/mu))/(1-exp(-b*L/mu))+K*tt/b; plot(tt,ue,x,u,'-or') legend('Exact','Numerical',2)