I am trying to calculate the derivative of interpolated data but it behaves differently from the analytic solution. Detailed code that I have used follows:
\[Gamma]w = 70.0*10^-3;(*SR in N/m*)\[Rho] = 1000; c = 3*10^8; g = 9.8;
we1 = 7*10^-6;(*beam waist*)n = 1.33; P0 = 4.0; rng = 50*10^-6;
lc = Sqrt[\[Gamma]w/(\[Rho]*g)]; P0 = 4; we1 =
7*10^-6; P1 = (P0/(\[Gamma]w*c*Pi))*((n - 1)/(n + 1));
f[r_] := 7*P1*BesselK[0, r/lc];
lst2 = Table[{r, f[r]}, {r, we1, rng, rng/1000}]; h2 =
Interpolation[lst2, InterpolationOrder -> 1];
pA = Plot[{h2[r]}, {r, we1, rng}, PlotStyle -> {Blue},
PlotRange -> All, Frame -> True];
pIn = Plot[{f[r]}, {r, we1, rng}, PlotStyle -> {Red},
PlotRange -> All, Frame -> True];
Show[{pA, pIn}](*height*);
A[r_] := h2''[r] + 1/r*h2'[r];(*Interpolation*)
A2 = D[f[r], {r, 2}] + 1/r*D[f[r], r];(*Analytic*)A3 = D[A2, {r,
1}];
plots = Plot[{A'[r], A3}, {r, we1, rng/1}, PlotRange -> All,
Frame -> True, PlotRange -> All, Frame -> True,
PlotStyle -> {{Red, Thick}, {Green, Thick}},
GridLines -> {{we1, 2 we1}, {0}},
GridLinesStyle -> Directive[Red, Dashed],
PlotLegends -> {"Interpolation (A'[r])", "Analytic (A3)"}]
[![plots][1]][1]
InterpolationOrder
. But generally, what you are trying to do is numerically unstable. Better to compute the derivatives straight from the data instead of going through an interpolation. $\endgroup$InterpolationOrder -> 1
and let Mathematica take care of the details. In this way it will interpolate linearly between data points, thus effectively using the finite-differences method of numerical differentiation. In general, to compute $n^{\text{th}}$ derivatives you should use an interpolation of oder $n$ (but not higher). $\endgroup$