Method of lines for diffusion equation

2 replies · 2K views · last reply
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
 
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