I had a try at numerically solving the heat equation with a source term: $$ \frac{{\partial x}}{{\partial t}} = D\frac{{{\partial ^2}x}}{{\partial {z^2}}} + c $$
Manipulate[Module[{d, c, solN, pars, L0, pde, ic, bc, x, u, t}, L0 = 1;
pde = D[u[x, t], t] == d D[u[x, t], {x, 2}] + c ;
bc = {D[u[x, t], x] == 0 /. x -> 0, u[L0, t] == 0};
ic = u[x, 0] == 1 - x;(*made up IC*)pars = {d -> d0, c -> c0};
solN = Quiet@
NDSolve[Evaluate[{pde, ic, bc} /. pars],
u, {x, 0, L0}, {t, 0, t0}];
Quiet@Plot[Evaluate[u[x, t0] /. solN], {x, 0, L0},
PlotRange -> {Automatic, {0, 1.5}}, GridLines -> Automatic,
GridLinesStyle -> LightGray, PlotStyle -> Red,
AxesLabel -> {"x", "u(x,t)"}, BaseStyle -> 12]], {{d0, 0.1, "D"},
0.001, 1, 0.001, Appearance -> "Labeled"}, {{c0, 0, "c"}, 0, 10,
0.01, Appearance -> "Labeled"}, {{t0, 0, "time"}, 0, maxTime, 0.1,
Appearance -> "Labeled"}, {{maxTime, 200}, None},
TrackedSymbols :> {d0, c0, t0}]
I messed up apparently because the bc2 is not doing what I want. Even if c=0 with time there is no diffusion.
Answer It requires a NeumannValue, to impose a basal flux=0:
Manipulate[
Module[{d, c, solN, pars, L0, pde, ic, bc, x, u, t, \[CapitalGamma]},
L0 = 1;
pde = D[u[x, t], t] ==
d D[u[x, t], {x, 2}] + c + NeumannValue[0, x == 0];
bc = u[L0, t] == 0;
ic = u[x, 0] == 1 - x;(*made up IC*)pars = {d -> d0, c -> c0};
solN = Quiet@
NDSolve[Evaluate[{pde, ic, bc} /. pars],
u, {x, 0, L0}, {t, 0, t0}];
Quiet@Plot[Evaluate[u[x, t0] /. solN], {x, 0, L0},
PlotRange -> {Automatic, {0, 1.5}}, GridLines -> Automatic,
GridLinesStyle -> LightGray, PlotStyle -> Red,
AxesLabel -> {"x", "u(x,t)"}, BaseStyle -> 12]], {{d0, 0.1, "D"},
0.001, 1, 0.001, Appearance -> "Labeled"}, {{c0, 0, "c"}, 0, 10,
0.01, Appearance -> "Labeled"}, {{t0, 0, "time"}, 0, maxTime, 0.1,
Appearance -> "Labeled"}, {{maxTime, 200}, None},
TrackedSymbols :> {d0, c0, t0}]
How to plot u[x,t] at x==0 as a function of t?
Tried:
Manipulate[
Module[{d, c, solN, pars, L0, pde, ic, bc, x, u, t, \[CapitalGamma]},
L0 = 5;
pde = D[u[x, t], t] ==
d D[u[x, t], {x, 2}] + c + NeumannValue[0, x == 0];
bc = u[L0, t] == 0;
ic = u[x, 0] == (L0 - x)/L0;(*made up IC*)pars = {d -> d0, c -> c0};
solN = Quiet@
NDSolve[Evaluate[{pde, ic, bc} /. pars],
u, {x, 0, L0}, {t, 0, t0}];
Quiet@Plot[Evaluate[u[0, t0] /. solN], {t0, 0, 300},
PlotRange -> {Automatic, {0, 1.5}}, GridLines -> Automatic,
GridLinesStyle -> LightGray, PlotStyle -> Red,
AxesLabel -> {"t", "u(0,t)"}, BaseStyle -> 12]], {{d0, 0.01, "D"},
0.001, 1, 0.001, Appearance -> "Labeled"}, {{c0, 0, "c"}, 0, 10,
0.01, Appearance -> "Labeled"}, {{t0, 0, "time"}, 0, maxTime, 0.1,
Appearance -> "Labeled"}, {{maxTime, 300}, None},
TrackedSymbols :> {d0, c0, t0}]
Write out the data for u[0,t]
Manipulate[
Module[{d, c, solN, pars, L0, pde, ic, bc, x, u, t, \[CapitalGamma]},
L0 = 5;
pde = D[u[x, t], t] ==
d D[u[x, t], {x, 2}] + c + NeumannValue[0, x == 0];
bc = u[L0, t] == 0;
ic = u[x, 0] == (L0 - x)/L0;(*made up IC*)pars = {d -> d0, c -> c0};
solN = NDSolve[Evaluate[{pde, ic, bc} /. pars],
u, {x, 0, L0}, {t, 0, t0}];
(*data is GLOBAL so to access it from outside*)
data = Table[{t, Evaluate[u[0, t] /. solN]}, {t, 0, t0, 0.1}];
Quiet@Plot[Evaluate[u[0, t] /. solN], {t, 0, t0},
PlotRange -> {Automatic, {0, 1.5}}, GridLines -> Automatic,
GridLinesStyle -> LightGray, PlotStyle -> Red,
AxesLabel -> {"t", "u(0,t)"}, BaseStyle -> 12]], {{d0, 0.01, "D"},
0.001, 1, 0.001, Appearance -> "Labeled"}, {{c0, 0, "c"}, 0, 10,
0.01, Appearance -> "Labeled"}, {{t0, 1, "time"}, 0.1, maxTime, 0.1,
Appearance -> "Labeled"}, {{maxTime, 300}, None},
TrackedSymbols :> {d0, c0, t0}]
Export[NotebookDirectory[] <> "data_table_test.csv", Flatten /@ data]
NDSolve
? what have you tried? $\endgroup$NDSolve
should be able to handle it without difficulty. $\endgroup$because the bc2 is not doing what I want.
It not clear to me what you mean here. But Mathematica can solve this analytically and it has no problem. Below is an illustration. May be you could explain more that you think the problem is? btw, your Latex does not match the PDE. may be you can correct that. $\endgroup$