# Testing the accuracy of numerically computed derivatives - alternative method

So I have asked this question earlier today: Testing the accuracy of numerically computed derivatives,

This method works well for DifferenceOrder methods but upon reading https://reference.wolfram.com/language/tutorial/NDSolveMethodOfLines.html, I believe it would be more accurate to use DifferenceOrder -> "Pseudospectral" with an appropriate grid.

Implementing the code as posted in my question but with the following changes:

Subscript[Der, i_][yyy_] :=
Module[{xx},
xx = Length[yyy];
NDSolveFiniteDifferenceDerivative[
Derivative[i],
N[yyy],
DifferenceOrder -> "Pseudospectral"] @ "DifferentiationMatrix"
// Normal // DeveloperToPackedArray // SparseArray];

xi = 1.;
xf = -1;
yy = 100;
xgrid = Table[(a+b)/2 + (a-b)/2)*Cos[i \pi/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 = Exp[yy]*diff


I get a very accurate result for the difference though,when I try to plot it, by applying the method prompted in the answer to my previous question, I get an error message:

NDSolveFiniteDifferenceDerivative::spc: The coordinates {-0.498973, -0.495895,-0.49078, -0.483647, -0.474528, -0.463458, -0.450484, -0.435659, -0.419044, -0.400707, <<29>>, 0.419044, 0.435659, 0.450484, 0.463458, 0.474528, 0.483647, 0.49078, 0.495895, 0.498973, 0.5} for dimension 1 do not have spacing proportional to the Chebyshev points -Cos[Range[0,48] Pi/48]. Nonperiodic pseudospectral derivatives are only computed for this spacing.

It seems to not like my xgrid choice though for when I implement my code the results are really great for the derivative, so it would be great to show this for different yy values. Thanks in advance.

• Did you try the grid it suggested in the error message? Nov 18 '19 at 1:10

As mentioned in referenced help page you need to construct a special grid:

Subscript[Der, i_][yyy_] := Module[{xx}, xx = Length[yyy];
NDSolveFiniteDifferenceDerivative[Derivative[i], N[yyy],
DifferenceOrder -> "Pseudospectral"]@"DifferentiationMatrix" //
Normal // DeveloperToPackedArray // SparseArray];

xi = -1.;
xf = 1;
xgrid[yy_] := xi + (xf - xi)/2 (1 - Cos[π Range[0, yy - 1]/(yy - 1)]);

Der1[yy_] := Subscript[Der, 1][xgrid[yy]];

numerical[yy_] := Der1[yy].Exp[-xgrid[yy]^2];
exact[yy_] := -2*xgrid[yy]*Exp[-xgrid[yy]^2];
diff[yy_] := numerical[yy] - exact[yy];


And now plotting:

yyvals = {100, 300, 1000, 2000};

ListLinePlot[
Table[Transpose[{xgrid[yy], Abs[diff[yy]]}], {yy, yyvals}],
PlotRange -> All, PlotLegends -> StringTemplate["yy = "] /@ yyvals,
ScalingFunctions -> "Log", Frame -> True] • For stability at large values of yy, I would instead do xgrid[yy_] := Rescale[Haversine[π Range[0, yy - 1]/(yy - 1)], {0, 1}, {xi, xf}]`. Nov 18 '19 at 10:25
{"ft_lang_label":"__label__en","ft_lang_prob":0.76318055,"math_prob":0.872199,"size":1731,"snap":"2021-43-2021-49","text_gpt3_token_len":513,"char_repetition_ratio":0.104226984,"word_repetition_ratio":0.0,"special_character_ratio":0.33853263,"punctuation_ratio":0.23380281,"nsfw_num_words":1,"has_unicode_error":false,"math_prob_llama3":0.9951643,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-03T06:59:37Z\",\"WARC-Record-ID\":\"<urn:uuid:bf966d42-8507-4fb8-a042-4d39bb7f52c8>\",\"Content-Length\":\"141020\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:65a69bf9-2062-420b-b71c-bf7c7dd4fff4>\",\"WARC-Concurrent-To\":\"<urn:uuid:dbedf644-d6c2-4594-8b51-21ffb056f736>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/209815/testing-the-accuracy-of-numerically-computed-derivatives-alternative-method\",\"WARC-Payload-Digest\":\"sha1:VAHYNVBRYGSWTV64A5GS655K3WVMGFS6\",\"WARC-Block-Digest\":\"sha1:QG6NJWANAUBOEATYSEDHKWXDKTR57JGR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964362605.52_warc_CC-MAIN-20211203060849-20211203090849-00638.warc.gz\"}"}