Facing problem to solve convection-diffusion equation
Show older comments
I want to solve simple diffusion equation MATLAB.
(del_C/del_t)=(D^2)(del^2_C/del_x^2)-U(del_C/del_x)
I.C: C(x,0)=C0; B.C: C(0,t)=C0, C(L,t)=C1
My program:
numr =50; %number of grid points in x direction
numt = 10000; %number of time steps
L = 0.01; %thickness of a flat plate (m)
dx = R/(numr - 1); %grid size in x direction
dt = 0.0005;
D = 1.0e-05; %species diffusivity
C0 = 1.0; %species initial concentration
Cinf = 0.1; %species surface concentration
x = 0:dx:L; %vector of x positions
C = zeros(numr,numt); %initialize the concentration C(x,t) matrix
%specify initial conditions along the radius(C=C0,x=0 to L,t=0)
t(1) = 0;
for i=1:numr
C(i,1)=C0;
end
% uniform boundary condition at the centre(C=C0,x=0,t=0 to tf)constant mass
% source term
for j = 1:numt
C(1,j)=C0;
end
% boundary condition at particle surface (C=Cinf,x=L,t>0)
% specis concentration is fixed at the boundary
for j=2:numt
C(numr,j)=Cinf;
end
%Stability criteria
%dx<(2*D)/u
%dt<(dx^2)/(2*D)
u=0.5;
s=D*dt/(2*dx^2);
p=u*dt/(4*dx);
%iteration based on forward differencing scheme
for j=2:numt
t(j) = t(j-1) + dt;
for i=2:numr-1
C(i,j+1)=C(i,j)+(s-p)*(C(i+1,j+1)+C(i+1,j))-(2.0*s)*(C(i,j+1)+C(i,j))+(s+p)*(C(i-1,j+1)+C(i-1,j));
end
end
plot(C,x)
xlabel('x direction')
ylabel('C')
Accepted Answer
More Answers (1)
Torsten
on 28 Jun 2018
On the right-hand side of the equation
C(i,j+1)=C(i,j)+(((s-p)*C(i+1,j+1)+(s-p)*C(i+1,j)...
you refer to elements C(.,j+1) that are not yet known. This is the general problem.
The "index exceeds matrix dimensions" error is caused by this reference to C(i,j+1) for j=numt since you allocated C as C=zeros(numr,numt).
Best wishes
Torsten.
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!