I am calculating approximate derivatives by using NDSolve`FiniteDifferenceDerivative
, so this works:
Subscript[Der, i_][yyy_] :=
Module[{xx},
xx = Length[yyy];
NDSolve`FiniteDifferenceDerivative[
Derivative[i],
N[yyy],
DifferenceOrder -> 2] @ "DifferentiationMatrix"
// Normal // Developer`ToPackedArray // SparseArray];
xi = 1.;
xf = -1;
yy = 100;
xgrid = Table[xi + i (xf - xi/yy), {i, 0, yy}];
(Der1 = Subscript[Der, 1][xgrid]) // MatrixForm;
numerical = Der1.Exp[-xgrid^2];
exact = -2*xgrid*Exp[-xgrid^2];
diff = numerical - exact;
diffError = yy^2*diff
ListLinePlot[yy^2 Abs[diff]]
I want to show my solution is accurate by demonstrating that the difference between the numerical solution and the exact solution goes to zero as $\mathtt{yy}^{-2}$. For this I want to plot $\mathtt{yy}^2 |\mathrm{numerical} - \mathrm{exact}|$ for different values of $\mathtt{yy}$ but am not sure how to do this.
The code gives reasonable values for the differences, though I am not sure how to plot them for different $\mathtt{yy}$ values.
I obtained the follow plot from the code shown above.