5

What would be the correct way to tell Mathematica that

 Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}]

Should not be zero when n is an integer for all n in the above? Since when n=3 the answer should be Pi/2 and zero for all other n values.

But Mathematica does not seem to detect this, even when told that n is integer:

ClearAll[x,n]
Integrate[Sin[3 x] Sin[n x], {x, 0, Pi},Assumptions -> Element[n, Integers] && n > 0]

Mathematica graphics

 % /. n -> 3

Mathematica graphics

And if

Assuming[Element[n,Integers]&&n>0,Integrate[Sin[3 x] Sin[n x],{x,0,Pi}]]
(*0*)

While

n=3;
Integrate[Sin[3 x] Sin[n x],{x,0,Pi}]
(* Pi/2*)

The question is, is it the programmer responsibility to tell Mathematica that sin(3 x)*sin(n x) is special case when n=3? May be make a function around it, something like

foo[n_Integer]:=
   If[n==3,
      Integrate[Sin[3 x]^2,{x,0,Pi}],
      Integrate[Sin[3 x] Sin[n x],{x,0,Pi}]] (*0*)
   ];

But I do not think the above is a good way to do in in general.

I think a special rule is needed to tell Mathematica about orthogonality of trig functions, and then ask Integrate to use this rule? But how to do this?

The question is: Should Mathematica have been able to automatically handle the special case of n=3 above? If not, what would be the best way to add a rule to tell Integrate to do this?

Nasser
  • 143,286
  • 11
  • 154
  • 359

5 Answers5

3

For this particular example, you could try using FourierSinCoefficient:

FourierSinCoefficient[Sin[3 x], x, n, FourierParameters->{-1,1}]

1/2 π DiscreteDelta[-3 + n]

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
2

I believe it's a bug!

I0=Simplify[Integrate[Sin[3 x] Sin[n x], {x, 0, Pi} ] ](*-((3 Sin[n \[Pi]])/(-9 + n^2))*)
I1=Simplify[Integrate[Sin[3 x] Sin[n x], {x, 0, Pi} ],Assumptions ->Element[n, Integers]](*0*)
I2=Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}, Assumptions -> n != 3](*-((3 Sin[n \[Pi]])/(-9 + n^2))*)
I3=Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}, Assumptions -> n ==  3](*\[Pi]/2*)

The integrals I0,I2,I3 are evaluated as expected whereras I1 gives a wrong result! It looks like the Limit/Singularity n->3 isn't recognized by the Simplify-Command!

Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
2

One way to get the answer:

Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}, Assumptions -> Element[n, Integers] && n > 0]
Normal@Series[%, {n, 3, 0}]  (* evaluate at n -> 3 *)
(* Pi/2 *)

I suppose Mathematica should get the right answer, but there's way in which this shows the answer returned by Integrate contains the correct answer at n == 3. Algebraic notation is sometimes deficient, and for an analytic object like this result, the power series is probably the best mathematical representation of the object. The undefined points n == ±3 of the expression returned by Integrate are not even singularities of the series. (It is to address such a deficiency that the need for the Sinc function was felt.) Thus I think Series (also Limit) is a mathematically correct way to evaluate the result.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
2
f[n_] = Piecewise[{{Integrate[Sin[3 x]^2, {x, 0, Pi}], n == 3}},
  Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}]]

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
1

EDIT: Didn't understand the question at first

First, note that to get 0, which is the answer when n≠3, you need to use Assuming instead of Assumptions:

ClearAll[x, n]
Assuming[
 Element[n, Integers] && n > 0,
 Integrate[Sin[3 x] Sin[n x], {x, 0, Pi}]
 ]

Output:

0

Now the question is why Mathematica fails to notice the special case of n=3. I speculate that Mathematica's ability to recognize the need of a delta function is lacking here. For instance, if I type an integral that is equivalent to the delta function, I just get an error:

Integrate[Exp[-I k x], {k, -\[Infinity], \[Infinity]}]

Output:

During evaluation of In[1845]:= Integrate::idiv: Integral of E^(-I k x) does not converge on {-\[Infinity],\[Infinity]}.

Your case may be another specific case of Integrate not identifying opportunities for the delta function. I would perhaps report it as a bug.

Max
  • 1,050
  • 6
  • 14
  • thank you. But I think that is what I wrote. I wanted Mathematica not to give zero for all n. I am not sure how your solution handles this special case? – Nasser Feb 16 '18 at 06:39
  • Ah I misunderstood your question. That is strange that it fails to notice the n=3 case. This seems to qualify as a bug, perhaps submit it to support if you don't get a better answer on SE? You can report it here: http://www.wolfram.com/support/contact/email/?topic=Feedback – Max Feb 16 '18 at 07:07
  • Thanks. The thing is, I am not sure if it is a bug or not. Since this could be by design. i.e. it could be that Mathematica is not designed to detect special cases like this. But I am not sure, that is why I asked. – Nasser Feb 16 '18 at 07:09
  • Well I think it's fair to call anything that evaluates to an incorrect answer a 'bug', since presumably Mathematica would never be wrong by design. The correct answer would involve a delta function ∂(n-3). I'll update my answer. – Max Feb 16 '18 at 17:22