Following the idea here by @xzczd, I tried:
a = 1/10; t0 = 0; tend = 1/50; xL = 0; xR = 1;
{eq, ic, bc} = {CaputoD[y[t, x], {t, a}] == D[y[t, x], {x, 2}],
y[t0, x] == Sin[Pi x], {y[t, xL] == 0, y[t, xR] == 0}};
nt = 15; range = Range[0, nt - 1];
coef[x_] = c[x] /@ range;
poly[x_][t_] = coef[x] . t^range;
domaint = {t0, tend};
CGLGrid[xl_, xr_, n_Integer /; n > 1] :=
1/2 (xl + xr + (xl - xr) Cos[(\[Pi] Range[0, n - 1])/(n - 1)])
gridt = CGLGrid[##, nt] & @@ domaint;
nx = 25; domainx = {xL, xR};
difforder = 2;
gridx = Array[# &, nx, domainx];
(*Definition of pdetoode isn't included in this post, please find it in the link above.*)
ptoofunc = pdetoode[y[t, x], t, gridx, difforder];
del = #[[2 ;; -2]] &;
{ode, odeic} = del /@ ptoofunc@{eq, ic};
odebc = ptoofunc@bc;
{approxode, approxic, approxbc} = {ode, odeic, odebc} /.
y -> poly; // AbsoluteTiming
var = coef /@ gridx // Flatten;
sys = Flatten@{Table[approxode, {t, Rest@gridt}],
Table[approxbc, {t, gridt}], approxic};
{barray, marray} = CoefficientArrays[sys, var];
csol = LinearSolve[marray, -N[barray, 32]];
Block[{c}, Evaluate@var = csol;
ParametricPlot3D[
Table[{t, x, poly[x][t]}, {x, gridx}] // Evaluate, {t, t0, tend},
PlotRange -> All, BoxRatios -> {1, 1, 0.4}]]
nsol = Block[{c}, Evaluate@var = csol;
ListInterpolation[Table[poly[x][t], {t, gridt}, {x, gridx}], {gridt, gridx}]];
The plot gives
Let's compare with the analytical solution
sol3 = DSolve[{CaputoD[y[t, x], {t, a}] == D[y[t, x], {x, 2}],
y[0, x] == Sin[Pi x], {y[t, 0] == 0, y[t, 1] == 0}}, y, {t, x}];
g[t_] := Integrate[Evaluate[y[t, x] /. sol3[[1]]]^2, {x, 0, 1}]
The plot gives Plot[{NIntegrate[nsol[t, x]^2, {x, 0, 1}], g[t]}, {t, t0, tend}]
My question is about the numerical issues in the above plot:
- How to handle the oscillation near $0$?
- How to get a smooth curve as in the analytical solution?
I tried to increase the number of points, e.g. nt=25 and nx=45, but the plot of lines become very different (oscillating near t=1/50) and second plot improves but still oscillating







g[t_] = Integrate[Evaluate[y[t, x] /. sol3[[1]]]^2, {x, 0, 1}]; Plot[{NIntegrate[nsol[t, x]^2, {x, 0, 1}, Method -> {Automatic, SymbolicProcessing -> 0}], g[t]}, {t, t0, tend}, PlotRange -> All, Mesh -> {gridt}, MeshStyle -> Red]We'll see the values at grid are accurate. Since my primary method doesn't involve technique like self-adaptive step choosing, I'm afraid it's not suitable to achieve what you want. (In principle a rather dense grid should help, but that's too expensive. ) – xzczd Feb 10 '24 at 04:45g[t]and not solution himself? – Alex Trounev Feb 10 '24 at 10:26