5
$\begingroup$

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?

$\endgroup$
3
  • $\begingroup$ Based on one answer already posted, what I have often found is that MMA does not pay attention to Assumtions inside Intégrate, but does to Assuming. I mean, I get the correct result for assumptions by using Assuming instead. $\endgroup$ Commented Feb 16, 2018 at 9:40
  • 1
    $\begingroup$ Some of this is explained here $\endgroup$ Commented Feb 16, 2018 at 15:53
  • 2
    $\begingroup$ See also this MathGroup thread $\endgroup$ Commented Feb 16, 2018 at 15:58

5 Answers 5

2
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

For this particular example, you could try using FourierSinCoefficient:

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

1/2 π DiscreteDelta[-3 + n]

$\endgroup$
2
$\begingroup$

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!

$\endgroup$
2
  • $\begingroup$ I understand that a singularity cannot be simplified. However, doing Limit does solve the problem in I1. $\endgroup$ Commented Feb 16, 2018 at 11:02
  • $\begingroup$ Yes Limit[I0,n->3] shows that the solution I0(!) is ok. Nevertheless I1 is wrong. $\endgroup$ Commented Feb 16, 2018 at 11:16
2
$\begingroup$
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

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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? $\endgroup$
    – Nasser
    Commented Feb 16, 2018 at 6:39
  • $\begingroup$ 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: wolfram.com/support/contact/email/?topic=Feedback $\endgroup$
    – Max
    Commented Feb 16, 2018 at 7:07
  • $\begingroup$ 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. $\endgroup$
    – Nasser
    Commented Feb 16, 2018 at 7:09
  • $\begingroup$ 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. $\endgroup$
    – Max
    Commented Feb 16, 2018 at 17:22

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.