Solving the diffusion equation finite difference technique

In summary, the conversation discussed solving the diffusion equation using the finite difference method in Matlab. The speaker ran into issues when changing the boundary condition and needed help understanding boundary value problems. The solution suggested using Neumann boundary conditions and provided a formula for updating the code to reflect this change.
  • #1
robby991
41
1
Hi everyone. I developed a Matlab program to solve the diffusion equation, a partial differential equation, using the finite difference method. I solved it first with boundary conditions of C = 0, at x=0 and C = 0, at x = 1. I now want to change the boundary condition C = Co (some constant) at x =1. The plot doesn't look right, I need help with the theory because I don't think I quite understand boundry value problems. The Matlab Code is fine. My Matlab code is as follows, and the resulting plot is attached:

Code:
numx = 101;   %number of grid points in x
numt = 2000;  %number of time steps to be iterated over 
dx = 1/(numx - 1);
dt = 0.00005;
Co = 3;
x = 0:dx:1;   %vector of x values, to be used for plotting

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

%specify initial conditions
t(1) = 0;      %t=0
C(1,1) = 0;    %C=0 at x=0  1st row, 1st column
C(1,numx) = Co; %C=0 at x=1  1st row, numx column
mu = 0.5;
sigma = 0.05;
for i=2:numx-1
   C(i,1) = exp(-(x(i)-mu)^2/(2*sigma^2)) / sqrt(2*pi*sigma^2);
end

%iterate difference equation - note that C(1,j) and C(numx,j) always remain 0
for j=1:numt
   t(j+1) = t(j) + dt;
   for i=2:numx-1
      C(i,j+1) = C(i,j) + (dt/dx^2)*(C(i+1,j) - 2*C(i,j) + C(i-1,j)); 
   end
end

figure(1);
hold on;
plot(x,C(:,101));
xlabel('x');
ylabel('c(x,t)');
 

Attachments

  • untitled.jpg
    untitled.jpg
    10.4 KB · Views: 524
Physics news on Phys.org
  • #2
title('Solution of Diffusion Equation with C(0)= 0, C(1) = 3');</code>Any help would be greatly appreciated. Thanks!A:The boundary condition C=Co at x=1 suggests that the diffusion equation is solved using Neumann boundary conditions, which are stated by the partial derivative of C with respect to x at x=1.In this case, the finite difference approximation of the partial derivative should be set equal to Co.The formula for a centered difference approximation of the first derivative is<code>dC/dx ~ (C(i+1)-C(i-1))/(2*dx).</code>For the Neumann boundary condition, this should be set equal to Co.<code>(C(numx)-C(numx-1))/(2*dx) = Co.</code>Solving this equation for C(numx) yields<code>C(numx) = Co*2*dx + C(numx-1).</code>This expression can be used to update your code, replacing the boundary condition<code>C(1,numx) = Co;</code>with <code>C(1,numx) = Co*2*dx + C(numx-1);</code>
 

FAQ: Solving the diffusion equation finite difference technique

1. What is the diffusion equation?

The diffusion equation is a mathematical model used to describe the movement of particles or substances from an area of high concentration to an area of low concentration. It is commonly used in various fields such as physics, chemistry, and biology to understand diffusion processes.

2. How is the diffusion equation solved using finite difference technique?

The finite difference technique is a numerical method used to solve differential equations, including the diffusion equation. It involves dividing the domain into a grid of points and approximating the derivatives at each point using the values of the function at neighboring points. These approximations are then used to create a system of linear equations that can be solved to obtain the solution of the diffusion equation at each point in the grid.

3. What are the advantages of using the finite difference technique to solve the diffusion equation?

One of the main advantages of the finite difference technique is its flexibility in handling complex boundary conditions and geometries. It also allows for a more accurate solution as the grid can be refined to increase the number of points. Additionally, the finite difference method is relatively easy to implement and can be applied to a wide range of diffusion problems.

4. Are there any limitations to using the finite difference technique for solving the diffusion equation?

While the finite difference technique is a powerful tool, it does have some limitations. One major limitation is that it is only applicable to linear diffusion problems. In cases where the diffusion coefficient or the boundary conditions vary significantly, other numerical methods such as finite element or finite volume may be more suitable.

5. How can the accuracy of the finite difference solution for the diffusion equation be improved?

The accuracy of the finite difference solution can be improved by increasing the number of grid points, which reduces the size of each grid cell and allows for a more precise approximation of the derivatives. Additionally, using higher-order finite difference schemes or incorporating adaptive grid refinement techniques can also improve the accuracy of the solution. It is also important to ensure that the chosen grid spacing is small enough to capture all the necessary features of the diffusion process.

Similar threads

Back
Top