# A lazy numerical solution to the diffusion equation

• MATLAB
Homework Helper
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:
$$\frac{f(i+1)-2*f(i)+f(i-1)}{h^{2}}=\textrm{gradient}(\textrm{gradient}(f,h),h)$$

So the code I wold use is:

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

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.

Staff Emeritus
Homework Helper
Gold Member
2021 Award
I think you will need to be more specific about what you are doing and what result you are trying to obtain relative to what you get. Also, posting your code would be helpful.

Homework Helper
My goal is solving the diffusion equation with arbitrary initial conditions and arbitrary boundary conditions.

One problem I noted was I wasn't using the CFL condition on time. Once I did that it worked fine. I have yet to check it works with the formulation above yet.

Staff Emeritus