4

I am trying to compute the integral

$$ F(i, j) = \int_0^1 \phi_i(x) \phi_j(x) dx, \quad \mbox{where}~~ \phi_k(x) = \sqrt{2} \sin\Big(\frac{2 k - 1}{2} \pi x\Big) $$

The integer $k \geq 1$. The answer should be $F(i,j) = \delta_{ij}$ so that $F(i,i) = 1$ for all integers $i \geq 1$ and $F(i,j) = 0$ for positive integers $i \neq j$.

The answer I get from Mathematica is wrong:

In[27]:= EigFun[j_, x_] = Sqrt[2] * Sin[Pi * x * (2*j - 1)/2]

Out[27]= Sqrt[2] Sin[1/2 (-1 + 2 j) [Pi] x]

In[29]:= ExpVal[i_, j_] = Integrate[EigFun[i, x] * EigFun[j, x], {x, 0, 1}]

Out[29]= ( Sin[(i - j) [Pi]]/(i - j) - Sin[(-1 + i + j) [Pi]]/(-1 + i + j))/[Pi]

In[30]:= FullSimplify[%29, Assumptions -> {i >= 1 && j >= 1 && i [Element] Integers && j [Element] Integers}]

Out[30]= 0

Is there an easy fix for this? Or what is the explanation for this issue?

Drew Brady
  • 141
  • 2
  • Please add code as text instead of an image. – creidhne Aug 12 '23 at 22:06
  • I have done so. – Drew Brady Aug 12 '23 at 22:29
  • 1
    There is no easy fix (except providing explicit numbers for i and j before integration). If you want to get KroneckerDelta[i,j] as result, you could send an email to support@wolfram.com and complain about a misleading result for Integrate[Sqrt[2]* Sin[(1/2)*(2*i - 1)*Pi*x]* Sqrt[2]*Sin[(1/2)* (2*j - 1)*Pi*x], {x, 0, 1}, Assumptions -> i \[Element] Integers && j \[Element] Integers, GenerateConditions -> True]. I am sure WRI is aware of these subtle issues. BTW: ChatGPT gets it wrong, too ... – Rolf Mertig Aug 12 '23 at 22:31
  • Can you explain why "there is no easy fix" ... this not really a strange/complicated integral? – Drew Brady Aug 12 '23 at 22:32
  • Well, maybe there is. Please call Wolfram. I just did not find an "easy fix" . Maybe you try other systems? Like Maple, or Sympy or so? – Rolf Mertig Aug 12 '23 at 22:36
  • 2
    Strongly related: https://mathematica.stackexchange.com/q/19833/1871 – xzczd Aug 13 '23 at 01:45
  • Limits of indefinites have to be taken explicitely $$Limit[ Sin[(i - j) [Pi]]/(i - j), i -> j] -> \pi$$ – Roland F Aug 13 '23 at 09:54

2 Answers2

6

The integral behaves reasonably well if one takes limits.

eij = Integrate[EigFun[i, x]*EigFun[j, x], {x, 0, 1}];
limsameij = Limit[eij, j -> i]
Simplify[limsameij, Assumptions -> Element[i, Integers]]

(* Out[324]= 1 + Sin[2 i [Pi]]/((-1 + 2 i) [Pi])

Out[325]= 1 *)

limdiffij = Limit[eij, j -> i + k, Assumptions -> k >= 1] Simplify[limdiffij, Assumptions -> Element[{i, k}, Integers] && k >= 1]

(* Out[326]= ( Sin[k [Pi]]/k - Sin[(-1 + 2 i + k) [Pi]]/(-1 + 2 i + k))/[Pi]

Out[327]= 0 *)

Daniel Lichtblau
  • 58,970
  • 2
  • 101
  • 199
4
Clear["Global`*"]

EigFun[j_, x_] = Sqrt[2]*Sin[Pi*x*(2*j - 1)/2];

ExpVal[i_Integer?Positive, j_Integer?Positive] = 
 Evaluate@Assuming[{i, j} ∈ PositiveIntegers,
   Piecewise[{
     {Integrate[EigFun[i, x]*EigFun[j, x], {x, 0, 1}], i != j},
     {Integrate[EigFun[i, x]^2, {x, 0, 1}], i == j}}]]

(* Piecewise[{{0, i != j}, {1, i == j}}, 0] *)

or, more compactly,

ExpVal[i_Integer?Positive, j_Integer?Positive] = 
 Evaluate@Assuming[{i, j} ∈ PositiveIntegers,
   Piecewise[{
       {Integrate[EigFun[i, x]*EigFun[j, x], {x, 0, 1}], i != j},
       {Integrate[EigFun[i, x]^2, {x, 0, 1}], i == j}}] // 
     Simplify]

(* Piecewise[{{0, i != j}}, 1] *)

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
  • 2
    Sure, of course it works if you explicitly tell it that the indices are equal (or not). But the question is: if one is to trust integral results on Mathematica, one would hope for at least an error message rather than Mathematica confidently returning 0 (in other cases, one may not actually be able to easily calculate the integral by hand whereas Mathematica could...) – Drew Brady Aug 13 '23 at 02:11