I am trying to compute the $L_2$-norm of the solution $y(z,t)$ of a PDE with Gauss quadrature using the following code, where $z$ is space position and $t$ is time, then to construct it as a function of $t$ by interpolating.
Get["NumericalDifferentialEquationAnalysis`"];
np = 101; points = weights = Table[Null, {np}];
Do[points[[i]] = GaussianQuadratureWeights[np, 0, L][[i, 1]], {i, 1, np}]
Do[weights[[i]] = GaussianQuadratureWeights[np, 0, L][[i, 2]], {i, 1, np}]
GaussInt[f_(integrand), z_] := Sum[(f /. z -> points[[i]])*weights[[i]], {i, 1, np}]
GQL2norm = Table[{t, GaussInt[sol[z, t]^2, z]}, {t, 0, tm, 1}];
L2normInt = Interpolation[GQL2norm, InterpolationOrder -> 2, Method -> "Spline"];
Here, sol[z,t] is an InterpolatingFunction that was obtained from NDSolveValue.
The following PDE is for testing, although the computation using the above code for the specific example is indeed fast, it is really slow for my real problem.
tm = 10; L = 5;
sol = NDSolveValue[{\!\(
\*SubscriptBox[\(\[PartialD]\), \(t\)]\(u[z, t]\)\) == \!\(
\*SubscriptBox[\(\[PartialD]\), \(z, z\)]\(u[z, t]\)\), u[z, 0] == 0,
u[0, t] == Sin[t], u[5, t] == 0}, u, {t, 0, tm}, {z, 0, L}]
Plot the evolution of the $L_2$-norm:
Plot[L2normInt[t]^(1/2), {t, 0, tm}, PlotRange -> {{0, tm}, All}, Frame -> True]
Problem:
The code can give a correct result with a sufficient node points
np, but it runs for an extremely long time. Please help me to improve it.As we do not know the degree of polynomials for an
InterpolatingFunctionobtained fromNDSolve-type solver, is there a rule of thumb to estimate the an adequate value ofnp?
From wikipedia:
An $n$-point Gaussian quadrature rule is a quadrature rule constructed to yield an exact result for polynomials of degree $2n − 1$ or less by a suitable choice of the nodes $x_i$ and weights $w_i$ for $i = 1, ..., n.$
Thank you for any suggestion!


GaussianQuadratureWeightsis fromNumericalDifferentialEquationAnalysis\``, you should add the correspondingNeeds[……]to the sample. Also, asol` should be added for testing. – xzczd Aug 06 '22 at 03:47sol[z, t]^2should be a simple business normally....I cannot say what would be the particular problem with yoursol[t, z], though. – Michael E2 Aug 06 '22 at 04:27solis too large to add here. Let me find a simple example – Nobody Aug 06 '22 at 04:53InterpolatingFunctionobtained fromNDSolveValue. – Nobody Aug 06 '22 at 05:52