Message Boards Message Boards

0
|
2549 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Noisy numerical finite difference calculated by NDSolve`FiniteDifferenceDer

Posted 5 years ago

I met a problem when playing with the numerical difference provided by mathematica. My aim is to test the accuracy of the high-order numerical derivative. The code is below Nx = 512; u0 = 0; uN = 5.0; du = (uN - u0)/Nx; ugrid = du Range[0, Nx]; C0 = Sin[Pi #/uN] & /@ ugrid; order = 3; der = 6; exact1 = D[Sin[u Pi/uN], {u, der}]; exact2 = Table[ exact1, {u, ugrid[[1]], ugrid[[-1]], ugrid[[2]] - ugrid[[1]]}]; num = NDSolve`FiniteDifferenceDerivative[Derivative[der], ugrid, C0, "DifferenceOrder" -> order]; ListPlot[{Transpose[{ugrid, exact2}], Transpose[{ugrid, num}]}] The result looks like this enter image description here

However, when I place the code in a dynamic module to play with the order of derivatives and number of points

Manipulate[Nx=2^pow;u0=0;uN=5;du=(uN-u0)/Nx;
ugrid=du Range[0,Nx];
C0=Sin[Pi #/uN]& /@ugrid;
exact1=D[Sin[Pi u/uN],{u,der}];
exact2=Table[exact1,{u,ugrid[[1]],ugrid[[-1]],ugrid[[2]]-ugrid[[1]]}];
num=NDSolve`FiniteDifferenceDerivative[Derivative[der],ugrid,C0,"DifferenceOrder"->order];
ListPlot[{Transpose[{ugrid,exact2}],Transpose[{ugrid,num}]}],{{pow,9,Dynamic["Points: "<>ToString[2^pow]]},4,15,1},{{order,3,Dynamic["Order: "<>ToString[order]]},1,15,1},{{der,6,Dynamic["Deriv: "<>ToString[der]]},1,18,1},ContinuousAction->False,LabelStyle->"Text"]

The result looks pretty good enter image description here

I'm not sure what the dynamic module does such that they generate different results with the normal one.

POSTED BY: Rui Ma
2 Replies
Posted 5 years ago

I got an answer from stackexchange. The results can be imrpoved by changing uN = 5.0 to uN = 5`10. I didn't know that the nuemrical digits can make a such a big difference!

POSTED BY: Rui Ma
Posted 5 years ago

I got an answer from stackexchange. The difference lies on the fact that uN = 5.0 in the first example while uN = 5 in the second. I didn't exepct 5.0 and 5 make such a big difference in the numerics. I wonder if there is a way to improve the results for uN = 5.0.

POSTED BY: Rui Ma
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract