Issue evaluating integral: seems to lead to incorrect answer

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?

• Please add code as text instead of an image. Aug 12, 2023 at 22:06
• I have done so. Aug 12, 2023 at 22:29
• 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 [email protected] 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 ... Aug 12, 2023 at 22:31
• Can you explain why "there is no easy fix" ... this not really a strange/complicated integral? Aug 12, 2023 at 22:32
• Strongly related: mathematica.stackexchange.com/q/19833/1871 Aug 13, 2023 at 1:45

2 Answers

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 *)

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] *)
`
• 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...) Aug 13, 2023 at 2:11