# How to set Working Precision in NIntegrate?

I have a complicated numerical integration to do and I do not know how to deal with the fact that: "NIntegrate::slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small". I need some help to handle this Working Precision which I have no clue how big it should set. The problem is in the choice of the elements of vectorj: if I choose 0.01, for instance, I get the result. However, I need it also for 0.3,0.5,1,3. dWtilde1 comes from fitting after importing a list, so I am providing the fitting function.

LcNum=1;
LNum=1;

Wtilde1[x_] =
Piecewise[{{84.70844970934941 (1.358063302255057*^-6 +
0.000054264407745696725 x + 0.06120370445759003 x^2 -
0.03778051411806694 x^3 - 0.03480674514282874 x^4 +
0.08772020845229346 x^5 - 0.12984735652632778 x^6 +
0.1215430660500256 x^7 - 0.061742002368084664 x^8 +
0.013107545496998208 x^9), 0 <= x <= 1.0001}}];

dWtilde1[x1_] = Piecewise[{{D[Wtilde1[x1], x1], 0 <= x1 <= 1}}];
S1[x1_] =
Piecewise[{{-1 + dWtilde1[x1]/dWtilde1,
0 < x1 <= 1}, {-1, -1 <= x1 <= 0}}];

vectorj = Flatten[List[0.3,0.5,1,3]];
vector = Table[
Table[Table[
i, {i, 10^k, 10^(k + 1), If[k >= 1, 10^k, 10^(k - 1)]}], {k, -3,
4, 1}], {j, vectorj}];

Integral1tilde1[x_] =
Integrate[(I*B*S1[x1] - beta)*Cos[M*(x1 + LcBar)], {x1, -LcBar, x},
Assumptions -> x \[Element] Reals && -LcBar <= x <= 1 && LcBar > 0];
Integral2tilde1[x_] =
Integrate[(I*B*S1[x1] - beta)*Sin[M*(1 - x1)], {x1, x, 1},
Assumptions -> x \[Element] Reals && -LcBar <= x <= 1 && LcBar > 0];
M = Sqrt[alpha*(beta + I B)];
B = (-2 I beta Sinh[((1 - I)/2)*Sqrt[beta/2]])/((1 - I)*Sqrt[beta/2] *
Cosh[((1 - I)/2)*Sqrt[beta/2]] -
2 Sinh[((1 - I)/2)*Sqrt[beta/2]]);

Part1tilde1 = -(alpha/(M*(Cos[M (1 + LcBar)])))*Integral1tilde1[x]*
Sin[M*(1 - x)];
Part2tilde1 = -(alpha/(M*(Cos[M (1 + LcBar)])))*Integral2tilde1[x]*
Cos[M*(x + LcBar)];
htilde1[x_, alpha_] = (Part1tilde1 + Part2tilde1);

IntegrandOFFtilde1[beta_] =
Abs[-I (1 - (1 - I)/2*Sqrt[beta/2]*
Cosh[(1 - I)*z*
Sqrt[beta/2]]/(Sinh[(1 - I)/2*Sqrt[ beta/2]]))*(dWtilde1[
x]) + (Z0 I beta dWtilde1/
2)*(Sinh[(1 - I) Sqrt[
beta/2] z]/((1 - I) Sqrt[
beta/2] Cosh[(Sqrt[beta/2] (1 - I)/2)] -
2 Sinh[Sqrt[beta/2] (1 - I)/2]))*(S1[x] -
htilde1[x, alpha])]^2;

fExacttilde1 =
Table[Table[
Table[i/(4*(
NIntegrate[
IntegrandOFFtilde1[i] /. {Lc -> LcNum, L -> LNum, Z0 -> j,
alpha -> (gamaNum[LNum]/i)}, {x, -1, 1}, {z, -1/2,
1/2}])), {i, 10^k, 10^(k + 1),
If[k >= 1, 10^k, 10^(k - 1)]}], {k, -3, 4, 1}], {j, vectorj}];

• If you mean the warning NIntegrate::slwcon, then WorkingPrecision is just one possible reason. If you get an answer without any other error, it might be correct, since it passes the error estimation check. Generally you set WorkingPrecision -> 32 or whatever number. MachinePrecision is about 16, but does not track precision; I sometimes start with WorkingPrecision -> 16 and then double it, etc. If you cannot compute the coefficients of Wtilde1 to a higher precision, you can artificially promote them with SetPrecision[<expr>, 32].... – Michael E2 Jun 19 '17 at 14:21
• The fastest way to find this kind of information is by clicking: Help->Wolfram Documentation and then searching for WorkingPrecision (or any other thing you have a doubt). – ivbc Jun 19 '17 at 14:24
• ...But you can check whether your integrand has working precision issues: First set its precision (with SetPrecision) to say 100, then plug in numbers of precision 100 or higher, and finally measure the precision loss with Precision[]. Since MachinePrecision is about 16, if you lose more than half of this (or about 8), then you can assume you have WorkingPrecision problems. I usually set WorkingPrecision to be about 16 above the precision loss, when it appears to be the issue and simultaneously set PrecisionGoal -> 8. – Michael E2 Jun 19 '17 at 14:29
• Is this a minimal example (Do you really need the nested table structure to illustrate the problem?) – george2079 Jun 19 '17 at 14:36
• You have a bad definition for Wtilde1, it should be Wtilde1[x_] = ... – Bob Hanlon Jun 19 '17 at 14:36