In order to better understand the MethodOfLines
flag, I tried comparing the results of NDSolve
for a driven heat equation with a manual implementation based on pdetoode
. Despite my best efforts, the absolute discrepancy at collocation points is at the part per million level which seems rather high. Can anyone suggest a strategy to bring pdetoode
into agreement with NDSolve
in this case?
Edit: Closer examination suggests that the discrepancy may be related to the temporal grid, which contain 10018
and 10007
points for pdesol
and odesol
respectively. Is it possible to force the number of temporal grid points into agreement?
T = 10;
f[t_] := Sin[t]^2;
numpoints = 30;
grid = Array[# &, numpoints, {0, 1}];
removeredundant = #[[2 ;; -2]] &;
xdifforder = 4;
ptoofunc = pdetoode[u[x, t], t, grid, xdifforder];
pde = Derivative[0, 1][u][x, t] == Derivative[2, 0][u][x, t];
ic = u[x, 0] == 0;
bc = {u[0, t] == f[t], u[1, t] == 0};
(* Definition of pdetoode isn't included in this post,
please find it in the link above. *)
ode = removeredundant@ptoofunc@pde;
odeic = removeredundant@ptoofunc@ic;
odebc = ptoofunc@bc;
method = {"MethodOfLines", "TemporalVariable" -> t,
"SpatialDiscretization" -> {"TensorProductGrid",
"MaxPoints" -> numpoints, "MinPoints" -> numpoints,
"DifferenceOrder" -> xdifforder}};
odesol = NDSolveValue[Join[ode, odeic, odebc],
u /@ grid, {t, 0, T}, MaxStepSize -> 0.001];
pdesol = NDSolveValue[Join[{pde}, {ic}, bc],
u, {x, 0, 1}, {t, 0, T}, Method -> method,
MaxStepSize -> {Automatic, 0.001}];
index = Round[numpoints 0.5];
xcoord = grid[[index]];
Plot[{odesol[[index]][t], pdesol[xcoord, t]}, {t, 0, T}, PlotStyle -> {, Dashed}]
Timing@Plot[odesol[[index]][t] - pdesol[grid[[index]], t], {t, 0, T}, PlotRange -> All]