this is code of 2D reaction diffusion equation using ADI method the code works well when i use the exponential
this is the code :
r = 1;
D = 1;
h = 0.0025;
Sx = 40.0;
Lx = -20.0;
Sy = 0.5;
Ly = -0.5;
x0 = Lx;
r = 0.5;
D = 0.1;
h = 0.025;
Sx = 10.0;
Sy = 6.0;
x0 = 0;
x1 = Sx;
y0 = 0;
y1 = Sy;
x = x0:h:x1;
y = y0:h:y1;
[X,Y] = meshgrid(x,y);
J1 = length(x);
J2 = length(y);
J = J1*J2;
u = zeros(J,1);
u1 = zeros(J,1);
cornertopleft = 1;
cornerbottomleft = J2;
cornertopright = (J1-1)*J2+1;
cornerbottomright = J;
sideleft = 2:J2-1;
sidetop = J2+1:J2:J2*(J1-2)+1;
sidebottom = 2*J2:J2:J2*(J1-1);
sideright = J2*(J1-1)+2:J-1;
side = [cornertopleft, cornertopright, cornerbottomleft, cornerbottomright, ...
sideleft, sidetop, sidebottom, sideright];
interiorY = setdiff(1:J, side);
[ix,iy] = ind2sub([J2,J1],interiorY);
interiorX = sub2ind([J1,J2],iy,ix);
LX = sparse(interiorX,interiorX,-2,J,J);
LX = LX + sparse(interiorX,interiorX+1,1,J,J);
LX = LX + sparse(interiorX,interiorX-1,1,J,J);
LY = sparse(interiorY,interiorY,-2,J,J);
LY = LY + sparse(interiorY,interiorY+1,1,J,J);
LY = LY + sparse(interiorY,interiorY-1,1,J,J);
u = (0.5 - 0.5 * tanh(1 / (2 * sqrt(6)) * X)).^2;
t0 = 0;
tfinal = 20;
t = t0;
tp = t0;
k = .1;
AX = (speye(J) - k/h^2*D/2*LX);
AY = (speye(J) - k/h^2*D/2*LY);
figure(2); clf;
image(x,y,255*reshape(u,J2,J1));
while t < tfinal
drawnow;
b = ( u + k/2*r*u.*(1-u) + k/2/h^2*D*LY*u );
b = reshape(reshape(b,J2,J1)',J,1);
u12 = AX\b;
LXu12 = LX*u12;
u12 = reshape(reshape(u12,J1,J2)',J,1);
LXu12 = reshape(reshape(LXu12,J1,J2)',J,1);
u = AY\( u12 + k/2*r*u12.*(1-u12) + k/2/h^2*D*LXu12 );
u(sideleft) = u(sideleft+J2);
u(sideright) = u(sideright-J2);
u(sidetop) = u(sidetop+1);
u(sidebottom) = u(sidebottom-1);
if ( fix(10*t) > fix(10*tp) )
image(x,y,255*reshape(u,J2,J1));
end
tp = t;
t = t + k;
fprintf("t = %.5f\n",t);
end
the problem is that with initial condition:u = exp( - X(:).^2/0.5 - Y(:).^2/2 ); the code is working without error while when i take the initial data = (0.5 - 0.5 * tanh(1 / (2 * sqrt(6)) * X)).^2 it gives me :error using * Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
Error in untitled3 (line 69) b = ( u + k/2ru.(1-u) + k/2/h^2DLYu ); i dont know how to fix this problem?