Method of lines for diffusion equation

Click For Summary
SUMMARY

The discussion focuses on solving a diffusion equation using the method of lines in MATLAB. The user implements functions to define initial conditions and the equation itself, but encounters unexpected results when visualizing the output with mesh plots. Key insights include the necessity of discretizing spatial derivatives with a second-order central difference approximation and the recommendation to use a stiff equation solver for time integration due to the nature of the resulting ordinary differential equations (ODEs).

PREREQUISITES
  • Understanding of diffusion equations and their mathematical formulation
  • Familiarity with MATLAB programming and function definitions
  • Knowledge of numerical methods, specifically the method of lines
  • Experience with solving stiff ordinary differential equations (ODEs)
NEXT STEPS
  • Research the method of lines and its application in solving partial differential equations (PDEs)
  • Learn about second-order central difference approximations for spatial discretization
  • Explore MATLAB's stiff ODE solvers, such as ode15s and ode23s
  • Investigate visualization techniques in MATLAB for analyzing numerical solutions
USEFUL FOR

Researchers, engineers, and students working on numerical methods for solving diffusion equations, particularly those using MATLAB for simulations and analysis.

k0st123
Messages
2
Reaction score
0
Hello,

I am trying to solve the following simple diffusion equation using the method of lines:

du/dt=D(du2/dx2)function k=func1()
k = [0, 0, 0, 0, 0, 0, 0.4380, 0, 1e5]; %k(9) initial value of the molecules
end %k(7) D/dx2

function k=func2()
c = [0 0 0 0 0 0 0 0];
end

function cx = eqn(t,c,k)

fb = [ 2*k(7)*c(2)-2*k(7)*c(1)-k(7)*k(9); % du1/dt
k(7)*c(3)-2*k(7)*c(2)+k(7)*c(1); % du2/dt
k(7)*c(4)-2*k(7)*c(3)+k(7)*c(2); % du3/dt
k(7)*c(5)-2*k(7)*c(4)+k(7)*c(3); % du4/dt
k(7)*c(6)-2*k(7)*c(5)+k(7)*c(4); % du5/dt
k(7)*c(7)-2*k(7)*c(6)+k(7)*c(5); % du6/dt
k(7)*c(8)-2*k(7)*c(7)+k(7)*c(6); % du7/dt
-2*k(7)*c(8)+k(7)*c(7)]; % du8/dt

endk = func1;

c = func2;

ts1 = [1 1000];

[A,B] = ode45(@(t,y)eqn(t,y,k),ts1, c);

I am using mesh for A and B but the results I get seem unexpected.
Could anyone please point me to what the problem could be? Are my equations right??

Thanks

k0st123
 
Physics news on Phys.org
Your function definitions looks weird. Do they even work( I suppose it is matlab)? Also I do not know what the method of lines is( wiki is no help) so please give a link describing the discretization function.
 
Strum said:
Your function definitions looks weird. Do they even work( I suppose it is matlab)? Also I do not know what the method of lines is( wiki is no help) so please give a link describing the discretization function.
In the method of lines, you descretize the spatial derivatives, but not the time derivatives. So you end up with a set of first order coupled ODEs in time. For a problem like this, the spatial derivative would be descretized using a 2nd order central difference approximation. In this kind of problem, the ODEs will be stiff, so you typically use a stiff equation solver to do the time integration.

Chet
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K