CODE: function [flowErrMax] = flow1derr1a(L,T,n,K,a,c,f) %%%%%%%%%%%%%%%%%%%%%%%
ID: 3602900 • Letter: C
Question
CODE:
function [flowErrMax] = flow1derr1a(L,T,n,K,a,c,f)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is a continuous model for the concentration of a pollutant.
% du/dt = - a*du/dx - c*u
% subject to the initial condition
% u(x,0) = f/c + sin(2*pi*x),
% and lower boundary condition
% u(0,t) = f/c + exp(-c*t)*sin(2*pi*(0-a)*t).
% Its analytic solution is given by:
% u(x,t) = f/c + exp(-c*t)*sin(2*pi*(x-c*t)).
%
% The correspoding discrete finite difference model is
% u(i,k+1)= f*dt + a*(dt/dx)*u(i-1,k)+(1-a*(dt/dx)-dt*c)*u(i,k),
% with initial condition
% u(i,1) = f/c + sin(2*pi*x(i)),
% and lower boundary condtion
% u(1,k) = f/c - exp(-c*time(k))*sin(2*pi*a*time(k)),
% where a is the stream velocity, assuming to be constant, and c is
% decay rate, also being a positive constant.
%
% Inputs:
% L = length of the stream, say L = 1.0;
% T = duration of time, e.g., T = 20.;
% K = number of time steps, e.g., K = 200;
% n = number of space steps, e.g., n = 10.;
% a = vel = velocity of the stream, a =.1;
% c = decay = decay rate of the pollutant, c = .1;
% f = nonhomogeneous term, say f =0. or 1.
%
% Outputs:
% flowErrMax = 4 x 4 matrix of max(abs(u(i,k)-uexac(i,k)),i=1..n+1,k=1..K+1)
% is the maximum error of the approximation
% flowErrMax(k)/flowErrMax(k-1), k>= 2
%
% Sample parameter slection for
% [flowErrMax] = flow1derr1a(L,T,n,K,a,c,f):
% [flowErrMax] = flow1derr1a(1,20,20,200,0.1,0.1,0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1:4
[errmax,u,uexac,x,time] = flow1derr1(L,T,n,K,a,c,f);
subplot(4,2,2*k-1)
mesh(x,time,uexac')
xlabel('x') % label for the 1st axis
ylabel('time') % label for the 2nd axis
zlabel('Pollutant') % label for the 3rd axis
title('Exact Solution')
subplot(4,2,2*k)
mesh(x,time,u')
xlabel('x') % label for the 1st axis
ylabel('time') % label for the 2nd axis
zlabel('Pollutant') % label for the 3rd axis
title('FOFD Solution')
flowErrMax(k,1) = K;
flowErrMax(k,2) = n;
flowErrMax(k,3) = errmax;
K = 2*K;
n = 2*n;
end
flowErrMax(1,4) = 1;
for k = 2:4
flowErrMax(k,4) = flowErrMax(k,3)/flowErrMax(k-1,3);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [errmax,u,uexac,x,time] = flow1derr1(L,T,n,K,a,c,f)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialize
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dt = T/K;
dx = L/n;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set up initial conditions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i = 1:n+1
x(i) = (i-1)*dx;
u(i,1) = f/c + sin(pi*x(i)*2);
uexac(i,1) = u(i,1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set up lower boundary conditions (Upstream Concentration)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:K+1
time(k) = (k-1)*dt;
u(1,k) = f/c + sin(-pi*a*time(k)*2)*exp(-c*time(k));
uexac(i,k) = u(1,k);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Execute the finite difference algorithm.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:K % Time Loop
for i=2:n+1; % Space Loop
u(i,k+1) = dt*f + (1 - a*dt/dx - c*dt)*u(i,k) + a*dt/dx*u(i-1,k);
uexac(i,k+1) = f/c + exp(-c*time(k+1))*sin(2*pi*(x(i)-c*time(k+1)));
error(i,k+1) = abs(u(i,k+1)-uexac(i,k+1));
end
end
errmax = max(max(error(1:n+1,2:K+1)));
end
Explanation / Answer
% This is a continuous model for the concentration of a pollutant.
% du/dt = - a*du/dx - c*u
% subject to the initial condition
% u(x,0) = f/c + sin(2*pi*x),
% and lower boundary condition
% u(0,t) = f/c + exp(-c*t)*sin(2*pi*(0-a)*t).
% Its analytic solution is given by:
% u(x,t) = f/c + exp(-c*t)*sin(2*pi*(x-c*t)).
%
% The correspoding discrete finite difference model is
% u(i,k+1)= f*dt + a*(dt/dx)*u(i-1,k)+(1-a*(dt/dx)-dt*c)*u(i,k),
% with initial condition
% u(i,1) = f/c + sin(2*pi*x(i)),
% and lower boundary condtion
% u(1,k) = f/c - exp(-c*time(k))*sin(2*pi*a*time(k)),
% where a is the stream velocity, assuming to be constant, and c is
% decay rate, also being a positive constant.
%
% Inputs:
% L = length of the stream, say L = 1.0;
% T = duration of time, e.g., T = 20.;
% K = number of time steps, e.g., K = 200;
% n = number of space steps, e.g., n = 10.;
% a = vel = velocity of the stream, a =.1;
% c = decay = decay rate of the pollutant, c = .1;
% f = nonhomogeneous term, say f =0. or 1.
%
% Outputs:
% flowErrMax = 4 x 4 matrix of max(abs(u(i,k)-uexac(i,k)),i=1..n+1,k=1..K+1)
% is the maximum error of the approximation
% flowErrMax(k)/flowErrMax(k-1), k>= 2
%
% Sample parameter slection for
% [flowErrMax] = flow1derr1a(L,T,n,K,a,c,f):
% [flowErrMax] = flow1derr1a(1,20,20,200,0.1,0.1,0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1:4
[errmax,u,uexac,x,time] = flow1derr1(L,T,n,K,a,c,f);
subplot(4,2,2*k-1)
mesh(x,time,uexac')
xlabel('x') % label for the 1st axis
ylabel('time') % label for the 2nd axis
zlabel('Pollutant') % label for the 3rd axis
title('Exact Solution')
subplot(4,2,2*k)
mesh(x,time,u')
xlabel('x') % label for the 1st axis
ylabel('time') % label for the 2nd axis
zlabel('Pollutant') % label for the 3rd axis
title('FOFD Solution')
flowErrMax(k,1) = K;
flowErrMax(k,2) = n;
flowErrMax(k,3) = errmax;
K = 2*K;
n = 2*n;
end
flowErrMax(1,4) = 1;
for k = 2:4
flowErrMax(k,4) = flowErrMax(k,3)/flowErrMax(k-1,3);
end
endfor k=1:K % Time Loop
for i=2:n+1; % Space Loop
u(i,k+1) = dt*f + (1 - a*dt/dx - c*dt)*u(i,k) + a*dt/dx*u(i-1,k);
uexac(i,k+1) = f/c + exp(-c*time(k+1))*sin(2*pi*(x(i)-c*time(k+1)));
error(i,k+1) = abs(u(i,k+1)-uexac(i,k+1));
end
end
errmax = max(max(error(1:n+1,2:K+1)));
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.