1

I try to use finite element to solve 2D diffusion equation:

numx = 101;   % number of grid points in x
numy = 101; 
numt = 1001;  % number of time steps to be iterated over 
dx = 1/(numx - 1);
dy = 1/(numy - 1);
dt = 1/(10*(numt - 1));


x = 0:dx:1;   %vector of x values, to be used for plotting
y = 0:dy:1;   %vector of y values, to be used for plotting

P = zeros(numx,numy,numt);   %initialize everything to zero

% Initial Condition
mu = 0.5;
sigma = 0.05;
for i=2:numx-1
    for j=2:numy-1
        P(i,j,1) = exp(-( (x(i)-mu)^2/(2*sigma^2) + (y(j)-mu)^2/(2*sigma^2))/(2*sigma^2)) / sqrt(2*pi*sigma^2);           
 end

 end

% Diffusion Equation
for k=1:numt
   for i=2:numx-1
       for j=2:numy-1
           P(i,j,k+1) = P(i,j,k) + .5*(dt/dx^2)*( P(i+1,j,k) - 2*P(i,j,k) + P(i-1,j,k) ) + .5*(dt/dy^2)*( P(i,j+1,k) - 2*P(i,j,k) + P(i,j-1,k) ); 
       end
   end
end

figure(1)
surf(P(:,:,30))

The initial condition of P is Gaussian distributed.

However, when increasing k (k=30 in my code), P blows up. However, P should decrease since it is a solution of diffusion equation.

I have no idea about the problem in P(i,j,k+1), how to fix this problem?

Thanks

sleeve chen
  • 221
  • 1
  • 11
  • This problem may have a closed form solution. If that can't be made to work, it might be a sign that your FEA solution has no chance of succeeding. Make sure the boundary and initial conditions make sense. – duffymo Nov 14 '19 at 17:58

1 Answers1

0

The value of P(1, :, :), P(101, :, :), P(:, 1, :) and P(:, 101 , :) is always zero in your code. It means P is always zero at the boundary of the 2-D box. It doesn't obey the diffusion law.

First, you should give an initial condition for the boundary.

% Initial Condition
mu = 0.5;
sigma = 0.05;
for i=1:numx
    for j=1:numy
        P(i,j,1) = exp(-( (x(i)-mu)^2/(2*sigma^2) + (y(j)-mu)^2/(2*sigma^2))/(2*sigma^2)) / sqrt(2*pi*sigma^2);           
     end
 end

And then calculate the value of P at the boundary in the diffusion process.

% Diffusion Equation
for k=1:numt
   for i=2:numx-1
       for j=2:numy-1
           P(i,j,k+1) = P(i,j,k) + .5*(dt/dx^2)*( P(i+1,j,k) - 2*P(i,j,k) + P(i-1,j,k) ) + .5*(dt/dy^2)*( P(i,j+1,k) - 2*P(i,j,k) + P(i,j-1,k) ); 
       end
   end

   % calculate the value of P at the boudary
   P(1, :, k + 1) = 2 * P(2, :, k + 1) - P(3, : , K + 1);
   P(numx, :, k + 1) = 2 * P(numx - 1, :, k + 1) - P(numx - 2, : , K + 1);
   P(:, 1, k + 1) = 2 * P(:, 2, k + 1) - P(:, 3 , K + 1);
   P(:, numy, k + 1) = 2 * P(:, numy - 1, k + 1) - P(:, numy - 2 , K + 1);
end