So I want to write a short code to solve the diffusion equation and I want to be lazy and use the gradient function for the spacial differences, so for the second order derivative:(adsbygoogle = window.adsbygoogle || []).push({});

[tex]

\frac{f(i+1)-2*f(i)+f(i-1)}{h^{2}}=\textrm{gradient}(\textrm{gradient}(f,h),h)

[/tex]

So the code I wold use is:

[tex]

T=T+dt*D*\textrm{gradient}(\textrm{gradient}(f,h),h)

[/tex]

When I do this, I get erroneous results from my initial condition of a Gaussian for example. values start becoming negative which wrong. I don't know where I am going wrong.

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# MATLAB A lazy numerical solution to the diffusion equation

Have something to add?

**Physics Forums | Science Articles, Homework Help, Discussion**