4
$\begingroup$

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?

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

2 Answers 2

6
$\begingroup$

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 *)
$\endgroup$
4
$\begingroup$
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] *)
$\endgroup$
1
  • 2
    $\begingroup$ 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...) $\endgroup$
    – Drew Brady
    Aug 13, 2023 at 2:11

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.