File Exchange

image thumbnail

Diffusion in 1D and 2D

version 1.0 (3.44 KB) by

The parabolic diffusion equation is simulated in both 1D and 2D



View License

The diffusion equation is simulated using finite differencing methods (both implicit and explicit) in both 1D and 2D domains. In both cases central difference is used for spatial derivatives and an upwind in time.

Comments and Ratings (11)


ronan (view profile)

I have a question, Since the postion and time are scaled (I suppose)

how can I convert this code results (x and t) to a micrometer scale and seconds? with a colloid diffusion coefficient like
20e-12m^2/s and a square wave of 50 micrometers?

Mehdi Amiri

I found the reason for this Problem. As the used time step of dt=0.01 is too big (or dx is too small), dt/(dx)^2 <= 1/2 as a criterion for stability is not satisfied.

There is something wrong with your explicit scheme i think. I´m quite new to this topic, so excuse me if I´m wrong. But haven´t you forget to loop over i and j? I tried to fix it, but yet the plot looks very strange, maybe the code doesn´t converge? I would be thankful for a solution of this Problem

anupal baruah

please how can i increase the simalation areas(from 2 to 500)on the X and Y axis and also the time(2sec to 4hrs) of the 2d diffusion equation.

Xin Jin


Steven (view profile)

Those modifications are for the Diffusion_2D.m


Steven (view profile)

These are quite nice visualizations. I found it nice to increase the time step to 100 to see more progression.

For better visualization, I recommend adding the command 'clf' immediately at the beginning of the loop as well as adding these line just before and after drawnow:

pause(0.01) % or 0.1 for slower update

Suraj Shankar

Suraj Shankar (view profile)

The code is meant to be pedagogical in nature and has been made in line with the 12-steps to Navier-Stokes practical module, for which I would like to credit Lorena Barba and her online course on CFD.


Tatsuo (view profile)

MATLAB Release
MATLAB 7.10 (R2010a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video