I tried to compute

$$\int_0^{2\pi}\left(\sum_{k=0}^n\cos(k x)\right)\mathrm dx$$

with two different codes:

Integrate[Sum[Cos[k*x],{k,0,n}],{x,0,2*Pi},Assumptions->n\[Element]Integers && n>0]


Refine[Integrate[Sum[Cos[k*x],{k,0,n}],{x,0,2*Pi}],n\[Element]Integers && n>0]

but both failed. However if I make a table of values for n then the computation works normally. There is a way to fix this behavior?


The integral is so simple and the interchangeability of summation and integration is so obvious to a human but not always to a computer, especially with an undefined symbolic number of summation terms. I hope in future this will just work, but for you to get results try making tiny simple changes that simplify things dramatically.

Sum[Integrate[Cos[k*x], {x, 0, 2*Pi}], {k, 0, n}]

$2 \pi$

  • $\begingroup$ Yes, it is obvious in this case... but I want to know if I can fix this behavior for other cases. $\endgroup$ – Masacroso Apr 3 '17 at 10:45
  • $\begingroup$ @Masacroso Why if interchangeability of summation and integration always works? $\endgroup$ – Vitaliy Kaurov Apr 3 '17 at 10:46
  • 1
    $\begingroup$ I dont knew that changing the order it can fix this behavior, thank you. Probably what happens is that mathematica dont know that k is a natural number for the other computation order. $\endgroup$ – Masacroso Apr 3 '17 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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