Skip to content

Instantly share code, notes, and snippets.

@pwl
Last active December 16, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwl/5353171 to your computer and use it in GitHub Desktop.
Save pwl/5353171 to your computer and use it in GitHub Desktop.
Zbieżność pochodnej z funkcji analitycznej i nieanalitycznej w przedostatnim punkcie siatki. Punkty oznaczają błąd bezwzględny (niebieski) i względny (fioletowy).
$MaxExtraPrecision = 1000;
der[f_, k_, rk_, h_] :=
NDSolve`FiniteDifferenceDerivative[Derivative[k], h Range[0, 100],
f /@ (h Range[0, 100]), DifferenceOrder -> rk];
abserr[f_, k_, rk_] :=
Abs[der[f, k, rk, #][[2]] -
Derivative[k][f][#]] & /@ (2^-Range[1, 30]);
relerr[f_, k_, rk_] :=
abserr[f, k, rk]/Abs@(Derivative[k][f] /@ (2^-Range[1, 30]));
graph[{f1_, f2_}, k_, rk_] :=
GraphicsRow[
ListPlot[N[Log[10, {abserr[#, k, rk], relerr[#, k, rk]}], 1000],
PlotLabel -> #["y"], AxesLabel -> {"n", "Error"}] & /@ {f1, f2},
PlotLabel ->
StringForm[
"Pochodna \!\(\*FractionBox[SuperscriptBox[\(d\), \(`1`\)], \
SuperscriptBox[\(dy\), \(`1`\)]]\) w punkcie \
\!\(\*SubscriptBox[\(y\), \(1\)]\)=\!\(\*SuperscriptBox[\(2\), \
\(-n\)]\) (dla \!\(\*SubscriptBox[\(y\), \(k\)]\)=kh, \
h=\!\(\*SuperscriptBox[\(2\), \(-n\)]\)), błąd metody to \
O(\!\(\*SuperscriptBox[\(h\), \(`2`\)]\))", k, rk],
ImageSize -> Large]
f = {Sin, Function[y, If[y == 0, 0, y^2 Log[y]]]};
graph[f, 1, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment