# Diffusion equation

I have take time to be continuous to solve in NDSolve and space to be discrete, I have to put condition on the density function at every equation. This is how I am implementing this in my code.

eqn :=  Table[ Subscript[u, i]'[t] ==
Subscript[u, i + 1][t] - 2 Subscript[u, i][t] +
Subscript[u, i - 1][t] +Subscript[u, i + 1][t] - 2 Subscript[u, i][t] + Subscript[u, i - 1][t] +HeavisideTheta[Subscript[u, i][t] - 0.047] , {i, 1, Xmax - 1}]

iv = Table[
If[(i >= 10) && (i <= 30), Subscript[u, i][0] == 1/21,
Subscript[u, i][0] == 0], {i, 0, Xmax}];

s10 = NDSolve[{eqn, iv, bcs}, var, {t, 0, Tmax}];

This code is not working heaviside function is used to provide cut off on the function u.

• I would not use subscripts if I were you. They tend to cause more problems than worth it. Commented Jul 19, 2018 at 6:13
• Then what is the alternative, there are n differential equation, how to handle that Commented Jul 19, 2018 at 6:34

Xmax = 50; Tmax = 1; eqn =
Table[Subscript[u, i]'[t] ==
2*(Subscript[u, i + 1][t] - 2 Subscript[u, i][t] +
Subscript[u, i - 1][t]) +
HeavisideTheta[Subscript[u, i][t] - 0.047], {i, 1, Xmax - 1}];

bcs = {Subscript[u, 0][t] == 0, Subscript[u, Xmax][t] == 0};
iv = Table[
If[(i >= 10) && (i <= 30), Subscript[u, i][0] == 1/21,
Subscript[u, i][0] == 0], {i, 0, Xmax}];

s = NDSolveValue[{eqn, iv, bcs},
Table[Subscript[u, i][t], {i, 1, Xmax - 1}], {t, 0, Tmax}];
{Plot[s, {t, 0, Tmax}, AxesLabel -> {"t", u}],
ListLinePlot[Table[s, {t, 0, 1, .1}], AxesLabel -> {x, u}]}

• Is there any other method to put cut off apart from heaviside theta function because it is taking too much time when i increase Tmax and lower the cut off of the order say 0.00047 Commented Jul 19, 2018 at 10:08
• Specify the parameters and how much the calculation occupies. I find the code very fast, others can be slower. Commented Jul 19, 2018 at 17:58