1
$\begingroup$

As far as I know, if a set of functions f,g,h,... are all Riemann integrable then the definite integral of the sum f+g+h over the interval [1,x] should be equal to the sum of the separate integrals over [1,x] of f and g and h.

In my case, the integrals must be calculated using NIntegrate because Mathematica won't play nicely with the Floor function element in the functions.

But for whatever reason, the integral of the sum does not match the sum of the integrals:

f[x_] := Sum[Floor[u]*((k*u*Cos[Pi*(u + k*u - x)])/(u + k*u - x)), 
   {k, 1, Floor[x] - 1}]; 
g[x_] := Sum[Floor[u]*((k^2*u*Cos[Pi*(u + k*u - x)])/(u + k*u - x)), 
   {k, 1, Floor[x] - 1}]; 
h[x_] := Sum[Floor[u]*((k^2*u*Cos[k*Pi*u - Pi*x])/(k*u - x)), 
   {k, 1, Floor[x] - 1}]; 

Table[{x, NIntegrate[f[x] + g[x] - h[x], {u, 1, x}], 
  NIntegrate[f[x], {u, 1, x}] + NIntegrate[g[x], {u, 1, x}] - 
    NIntegrate[h[x], {u, 1, x}]}, {x, 2, 5, 1/2}]

{{2, 300.5118851, 442.9860989}, {5/2, 736.7297006, 743.1447953}, {3, 894.7316167, 904.577171}, {7/2, 1550.215739, 1571.798798}, {4, 1770.592955, 2035.875269}, {9/2, 2678.596999, 2687.159346}, {5, 2984.444006, 3038.680717}}

Is this just a result of using NIntegrate? If It's numerical error, is there a work-around to obtain more accurate results?

Or am I doing something completely wrong, either in my coding or in my maths?

$\endgroup$
13
  • $\begingroup$ I don't know about you, but NIntegrate gives me a bunch of "failed to converge" errors. It makes me think that is where to look for the problem. $\endgroup$
    – Michael E2
    Commented Jun 5 at 6:59
  • $\begingroup$ You integrate over "u" but you function are given as "f[x]". $\endgroup$ Commented Jun 5 at 7:28
  • $\begingroup$ Thanks @Daniel. Just FYI, the x vs u thing is deliberate; this is part of a bigger function with many variables, and the required result is its value for a given x. $\endgroup$ Commented Jun 5 at 7:56
  • 2
    $\begingroup$ Your math is wrong, the functions have singularities and are not integrable, at least in the case x=2, which I checked. $\endgroup$
    – yarchik
    Commented Jun 5 at 8:27
  • 1
    $\begingroup$ @yarchik No, I was agreeing with you. I suggested PV just looking at the denominators, and took it back in my 2nd comment after realizing the effect of floor[] on the asymptotes at integers. As you said, there's no point going into it much. $\endgroup$
    – Michael E2
    Commented Jun 5 at 18:39

1 Answer 1

1
$\begingroup$

NIntegrate approximates integrals by interpolation of the functions via Legendre or other polynomial smooth series by a series of refining intervals until the primitive numerical evaluation of integrated polynomials becomes stationary.

This process does not produce reliable approximations for functions with discontinuities in any order of derivatives, that is becoming visible at a certain resolution.

Generally, Riemann integrals have to be subdivided into sums of integrals over intervalls of smoothness of derivatives up to the order of the number of evaluation points.

$\endgroup$

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.