I have a function U1 which contains a linear combination of trigonometric function, I wanted to Numerically integrate this function U1. Since each trigonometric functions associated with some unknown coefficients. I use coefficient rules to separate the unknown coefficients and associated functions. I later used Nintegrate to carry out numerical integration. But I am getting warnings," Numerical integration converging too slowly, suspect singularity" (NIntegrate::slwcon, NIntegrate::ncvb). how to overcome this?

L2 = 1;
fixedfree = Table[Sin[(2*i - 1)/(2*L2)*\[Pi]*x2], {i, 1, 3}];
fixedfixed = Table[Sin[(i*\[Pi]*x2)/L2], {i, 1, 3}];
barmodes = Flatten[{fixedfree, fixedfixed}];

U1 = Expand[
   Total[Table[b[i]*barmodes[[i]], {i, 1, Length[barmodes]}]]];
U1x = Expand[D[U1, {x2, 1}]];
in3 = Expand[(U1x)^2];
in4 = Expand[(U1)^2];
var2 = Table[b[i], {i, 1, Length[barmodes]}]

rules3 = CoefficientRules[in3, var2];
rules3[[All, 2]] = NIntegrate[rules3[[All, 2]], {x2, 0, L2}];
v2 = 0.5*a2*Y2*(FromCoefficientRules[rules3, var2])

rules4 = CoefficientRules[in4, var2];
rules4[[All, 2]] = NIntegrate[rules4[[All, 2]], {x2, 0, L2}];
t2 = 0.5*r*a2*q^2*(FromCoefficientRules[rules4, var2])
  • 4
    $\begingroup$ Using Integrate instead of NIntegrate instantly finds all your integrals exactly and with zero error or warning messages $\endgroup$
    – Bill
    Jan 9, 2019 at 17:49

3 Answers 3


The problem is that some of the integrals vanish (also suggested in the NIntegrate::slwcon warning), which makes it impossible to achieve an error goal determined exclusively by relative precision. This is what happens by default, because the setting for AccuracyGoal is Infinity. Use a finite AccuracyGoal that is appropriate for the magnitude of the integrand and the working precision. For machine precision and the integrands in rules4, which have maxima on the order of 10^0 == 1, use $MachinePrecision or 16.

NIntegrate[rules4[[All, 2]], {x2, 0, L2}, AccuracyGoal -> $MachinePrecision]
{0.5, 0., -5.55112*10^-17, 0.848826, -0.339531, 0.21827, 0.5, 
 5.55112*10^-17, 0.509296, 0.727565, -0.282942, 0.5, -0.121261, 
 0.565884, 0.694494, 0.5, -9.10214*10^-18, 6.7447*10^-17, 0.5, 
 2.22967*10^-16, 0.5}
  • $\begingroup$ I am still getting the same error, I just copy the same thing what you have mentioned in your answer. still getting the same error $\endgroup$
    – acoustics
    Jan 9, 2019 at 18:00
  • $\begingroup$ Well, choose an AccuracyGoal that is "appropriate for the magnitude of the integrand and the working precision" -- how large are the maxima of the integrands in rule3? $\endgroup$
    – Michael E2
    Jan 9, 2019 at 18:06
  • $\begingroup$ @acoustics You might find it easier, too, if you split your code up, so that the troublesome lines of code are isolated. (It would also help you improve your question, by writing a *minimal working example," omitting the annoying things that have nothing to do with the problem, like the plots.) $\endgroup$
    – Michael E2
    Jan 9, 2019 at 18:08
  • $\begingroup$ @acoustics Did you figure out how to adjust the AccuracyGoal for the integral of rules3? $\endgroup$
    – Michael E2
    Jan 11, 2019 at 22:33

You can avoid the error messages in a slightly modified way of your approach(Thanks to the hint of Michael E2):

First find the different functions

fkt = Cases[U1, _ Sin[s_] -> Sin[s] , Infinity](* time dependent*)

Second get the coefficients(symboliq or numeric)

coef = Map[Coefficient[U1, #] &, fkt]
(*{b[1], b[4], b[2], b[5], b[3], b[6]} *)
U1 == coef.fkt


Integrate[U1, {x2, 0, L2}] // AbsoluteTiming 
(*time 0.11*)
coef.NIntegrate[fkt, {x2, 0, L2},AccuracyGoal -> 10] // AbsoluteTiming

Also you can change functions with zero means on others, having non-zero means by adding a constant, e.g. and

rules4[[All, 2]] = NIntegrate[rules4[[All, 2]] + 1, {x2, 0, L2}] - NIntegrate[1, {x2, 0, L2}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.