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

Hi guys

Did somebody of You guys write already a finite difference scheme for
the 3d heat equation. I know how to write it by myself, but I'm kind
of in
a hurry, and would like to concentrate on the main problems. If
somebody
of You guys wrote already a small program for a 3d finite difference
scheme

Have a good one.

Tobi

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

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.

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

Hi Ayuda,

I tried your solution to different mesh sizes. The results come out to be different, which means there is no convergence. DO you think your matlab solution is 100% correct?

Thanks,

~ MZ

```How would you write a MATLAB code for the explicit finite differece eqaution? The diffusion equation (in 1-D) is dependent on a spatial variable (x) and a time variable (t). How would you write this in a loop in matlab? I get an error message for defining the equation

C(x)=w*C(x+1)+(1-2*w)*C(x)+w*C(x-1)
```

```Hi All,

I was trying to write my own code for Finite Difference Method (Explicit) implementation. I took heat-diffusion equation to implement it. Can any of you help me in generating the Matrix part of it?

Shahriar
```

```In respect to partial differential equation Burgers' Equation subject
to the following Boundary conditions and Initial conditions,
I seek an exact solution as well as an approximate solution using the
Forward-Difference Method, the Backward-Difference Method, and the
Crank-Nicolson Method.

Burgers' Equation

u + u*u = u 0 < x < 1, t > 0
t x xx
where the subscript on the 'u' refers to partial differentiation.

The Boundary conditions are
u(0, t) = u(1, t) = 0
and the Initial condition is
u(x, 0) = sin (pi*x)

I seek to use a maximum time T of 0.5,
step-lengths on the x-axis of 20, or 50, and
step-lengths on the t-axis of 10 or 20.

Any assistance is highly appreciated.

Sincerely,
Mavis
```

```In respect to partial differential equation Burgers' Equation subject
to the following Boundary conditions and Initial conditions,
I seek an exact solution as well as an approximate solution using the
Forward-Difference Method, the Backward-Difference Method, and the
Crank-Nicolson Method.

Burgers' Equation

u + u*u = u 0 < x < 1, t > 0
t x xx
where the subscript on the 'u' refers to partial differentiation.

The Boundary conditions are
u(0, t) = u(1, t) = 0
and the Initial condition is
u(x, 0) = sin (pi*x)

I seek to use a maximum time T of 0.5,
step-lengths on the x-axis of 20, or 50, and
step-lengths on the t-axis of 10 or 20.

Any assistance is highly appreciated.

Sincerely,
Mavis
```