1
$\begingroup$

When I use the Mathematica to obtain the integral of the symbolic expression, the output result seems not to be applied for all the cases. For example I use the code

Integrate[Cos[m Pi x] Cos[n Pi x], {x, 0, 1},
Assumptions -> m \[Element] Integers && n \[Element] Integers]

to calculate the integral $$\int\cos (m\pi x)\cos (n \pi x)\,\mathrm{d}x$$

The output is $$\frac{m \sin (\pi m) \cos (\pi n)-n \cos (\pi m) \sin (\pi n)}{\pi m^2-\pi n^2}$$

I define a function with respect to the variables $m$ and $n$

f[m_, n_] := (
  m Cos[n \[Pi]] Sin[m \[Pi]] - n Cos[m \[Pi]] Sin[n \[Pi]])/(
  m^2 \[Pi] - n^2 \[Pi]);

Obviously, the function $f(m,n)$ is only applicable for the cases $m\neq n$. Some assumptions are made during the calculation by Mathematica. Now I want to obatin the unified expressions for the integral, for example, The result can be a conditional expression.

$\endgroup$
2
  • $\begingroup$ In fact the function f(m,n) is applicable for the cases m=n, if one takes the limit n->m instead of assignment. Try enhance the definition: f[m_, m_]:=Limit[f[m, n], n -> m]. $\endgroup$
    – Mher
    Commented Dec 12, 2017 at 9:10
  • $\begingroup$ @Mher Thanks for your comments. It works for this example. The example shown in this post is a minimal work example. I want to apply the function(The function in my real work is much complex than this example) to a large list and I found that the Limit is very time-consuming. Are there any alternative methods? $\endgroup$
    – Ice0cean
    Commented Dec 12, 2017 at 9:21

1 Answer 1

0
$\begingroup$

What about the folowing defition

f[m_, n_] = 
  PiecewiseExpand[
    Which[m == n, 
    Integrate[Cos[m Pi x] Cos[n Pi x], {x, 0, 1}, 
Assumptions -> {m \[Element] Integers && n \[Element] Integers && 
   m == n}], m != n, 
  Integrate[Cos[m Pi x] Cos[n Pi x], {x, 0, 1}, 
Assumptions -> {m \[Element] Integers && n \[Element] Integers && 
   m != n}]]]

{f[1, 2],f[1,1]}

(*    {0, 1/2}    *)
$\endgroup$

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.