### MATLAB >> 3d finite difference method for heat equation

by **ayuda** » Thu, 30 Oct 2003 06:14:48 GMT

Since you can write a code for solving a 3D heat equation by yourself, it

will be a good idea to try. But if your are so busy and concentrated in the

main problems :) here is one solution:

Let "u" be your initial 3D data, "dt" the time step, and "maxit" the number

of iterations. Then a solution to 3D heat equation

" u_t = \Delta u " with a predefined initial condition "u_0" would be:

==========================================

dt = 1/6; maxit = 50;

[m,n,p] = size(u);

% Initialize result

v = u;

for i = 1:maxit

vxx = (v([2:m m],[1:n],[1:p]) + v([1 1:m-1],[1:n],[1:p]) -...

2.*v([1:m],[1:n],[1:p]))./4;

vyy = (v([1:m],[2:n n],[1:p]) + v([1:m],[1 1:n-1],[1:p]) -...

2.*v([1:m],[1:n],[1:p]))./4;

vzz = (v([1:m],[1:n],[2:p p]) + v([1:m],[1:n],[1 1:p-1]) -...

2.*v([1:m],[1:n],[1:p]))./4;

% Discretization of Laplacian: Deltav = v_xx + v_yy + v_zz

Deltav = vxx + vyy + vzz;

% Update result

v = v + dt*Deltav;

end;

==========================================

Ayuda.