2
$\begingroup$

The summation of Fourier series of Sin[x-x[i]] at three location x[1] to x[3] is:

Sum[FourierSeries[Sin[x - x[i]], x, 5], {i, 3}]

However, due to x[i] contains x literally, the result is not right

-((E^(I x) (-E^(I (-π)[1]) + E^(I π[1]) + 
     2 I E^(-I (-π)[1]) π + 2 I E^(-I π[1]) π))/(
  8 π)) + (
 E^(-I x) (E^(-I (-π)[1]) - E^(-I π[1]) + 
    2 I E^(I (-π)[1]) π + 2 I E^(I π[1]) π))/(
 8 π) - (
 E^(I x) (-E^(I (-π)[2]) + E^(I π[2]) + 
    2 I E^(-I (-π)[2]) π + 2 I E^(-I π[2]) π))/(
 8 π) + (
 E^(-I x) (E^(-I (-π)[2]) - E^(-I π[2]) + 
    2 I E^(I (-π)[2]) π + 2 I E^(I π[2]) π))/(
 8 π) - (
 E^(I x) (-E^(I (-π)[3]) + E^(I π[3]) + 
    2 I E^(-I (-π)[3]) π + 2 I E^(-I π[3]) π))/(
 8 π) + (
 E^(-I x) (E^(-I (-π)[3]) - E^(-I π[3]) + 
    2 I E^(I (-π)[3]) π + 2 I E^(I π[3]) π))/(
 8 π) + (-Cos[(-π)[1]] + Cos[π[1]])/(
 2 π) + (-Cos[(-π)[2]] + Cos[π[2]])/(
 2 π) + (-Cos[(-π)[3]] + Cos[π[3]])/(2 π) + (
 E^(-2 I x) (Cos[(-π)[1]] - Cos[π[1]] + 
    2 I (Sin[(-π)[1]] - Sin[π[1]])))/(6 π) + (
 E^(-3 I x) (-Cos[(-π)[1]] + Cos[π[1]] - 
    3 I (Sin[(-π)[1]] - Sin[π[1]])))/(16 π) + (
 E^(3 I x) (-Cos[(-π)[1]] + Cos[π[1]] + 
    3 I (Sin[(-π)[1]] - Sin[π[1]])))/(16 π) + (
 E^(-4 I x) (Cos[(-π)[1]] - Cos[π[1]] + 
    4 I (Sin[(-π)[1]] - Sin[π[1]])))/(30 π) + (
 E^(-5 I x) (-Cos[(-π)[1]] + Cos[π[1]] - 
    5 I (Sin[(-π)[1]] - Sin[π[1]])))/(48 π) + (
 E^(5 I x) (-Cos[(-π)[1]] + Cos[π[1]] + 
    5 I (Sin[(-π)[1]] - Sin[π[1]])))/(48 π) + (
 E^(2 I x) (Cos[(-π)[1]] - Cos[π[1]] - 2 I Sin[(-π)[1]] + 
    2 I Sin[π[1]]))/(6 π) + (
 E^(4 I x) (Cos[(-π)[1]] - Cos[π[1]] - 4 I Sin[(-π)[1]] + 
    4 I Sin[π[1]]))/(30 π) + (
 E^(-2 I x) (Cos[(-π)[2]] - Cos[π[2]] + 
    2 I (Sin[(-π)[2]] - Sin[π[2]])))/(6 π) + (
 E^(-3 I x) (-Cos[(-π)[2]] + Cos[π[2]] - 
    3 I (Sin[(-π)[2]] - Sin[π[2]])))/(16 π) + (
 E^(3 I x) (-Cos[(-π)[2]] + Cos[π[2]] + 
    3 I (Sin[(-π)[2]] - Sin[π[2]])))/(16 π) + (
 E^(-4 I x) (Cos[(-π)[2]] - Cos[π[2]] + 
    4 I (Sin[(-π)[2]] - Sin[π[2]])))/(30 π) + (
 E^(-5 I x) (-Cos[(-π)[2]] + Cos[π[2]] - 
    5 I (Sin[(-π)[2]] - Sin[π[2]])))/(48 π) + (
 E^(5 I x) (-Cos[(-π)[2]] + Cos[π[2]] + 
    5 I (Sin[(-π)[2]] - Sin[π[2]])))/(48 π) + (
 E^(2 I x) (Cos[(-π)[2]] - Cos[π[2]] - 2 I Sin[(-π)[2]] + 
    2 I Sin[π[2]]))/(6 π) + (
 E^(4 I x) (Cos[(-π)[2]] - Cos[π[2]] - 4 I Sin[(-π)[2]] + 
    4 I Sin[π[2]]))/(30 π) + (
 E^(-2 I x) (Cos[(-π)[3]] - Cos[π[3]] + 
    2 I (Sin[(-π)[3]] - Sin[π[3]])))/(6 π) + (
 E^(-3 I x) (-Cos[(-π)[3]] + Cos[π[3]] - 
    3 I (Sin[(-π)[3]] - Sin[π[3]])))/(16 π) + (
 E^(3 I x) (-Cos[(-π)[3]] + Cos[π[3]] + 
    3 I (Sin[(-π)[3]] - Sin[π[3]])))/(16 π) + (
 E^(-4 I x) (Cos[(-π)[3]] - Cos[π[3]] + 
    4 I (Sin[(-π)[3]] - Sin[π[3]])))/(30 π) + (
 E^(-5 I x) (-Cos[(-π)[3]] + Cos[π[3]] - 
    5 I (Sin[(-π)[3]] - Sin[π[3]])))/(48 π) + (
 E^(5 I x) (-Cos[(-π)[3]] + Cos[π[3]] + 
    5 I (Sin[(-π)[3]] - Sin[π[3]])))/(48 π) + (
 E^(2 I x) (Cos[(-π)[3]] - Cos[π[3]] - 2 I Sin[(-π)[3]] + 
    2 I Sin[π[3]]))/(6 π) + (
 E^(4 I x) (Cos[(-π)[3]] - Cos[π[3]] - 4 I Sin[(-π)[3]] + 
    4 I Sin[π[3]]))/(30 π)

simply rename x[i] to g[i], the result is

-(1/2) I E^(I x - I g[1]) + 1/2 I E^(-I x + I g[1]) - 
 1/2 I E^(I x - I g[2]) + 1/2 I E^(-I x + I g[2]) - 
 1/2 I E^(I x - I g[3]) + 1/2 I E^(-I x + I g[3])

Therefore, Sum[FourierSeries[Sin[x - g[i]], x, 5], {i, 3}] /. g -> x can return the correct answer. However, this looks not elegant. Is there a better way or formal way to do this?

$\endgroup$
  • 1
    $\begingroup$ Nothing wrong with the latter - I'd argue using x and x[...] in such a construct is potentially confusing (and leads to unintended consequences as you've seen). You could also just use x[0] for the plain x, this keeps it consistent at gets the desired result (with x[0] in the place of x, o/c). $\endgroup$ – ciao Apr 21 '16 at 7:37
  • $\begingroup$ @ciao Because x is a variable, but x[..] is a constant value in x-axis. I think maybe it is a little easier to understand. If no better solution, I think changing x[..] to magic[..] also works for me ^_^ $\endgroup$ – Kattern Apr 21 '16 at 8:09
  • 1
    $\begingroup$ I'm with ciao, the solution you found is optimal, though I might have switched out the dummy variable and written Sum[FourierSeries[Sin[x0 - x[i]], x0, 5], {i, 3}] /. x0 -> x $\endgroup$ – Jason B. Apr 21 '16 at 9:13
  • $\begingroup$ @JasonB Then, I think this question is solved. $\endgroup$ – Kattern Apr 21 '16 at 9:17
  • $\begingroup$ @JasonB do you have any comment on the answer I started? $\endgroup$ – Kattern Apr 21 '16 at 9:43
3
$\begingroup$

Based on comments from @JasonB and @ciao, the short answer for this problem is NO, there is no formal way to do this in Mathematica.

Although x and x[...] are clearly two different variables, Mathematica sometime may trade them as somehow related variables or one variable. I do not know whether this assumption has any potential usage, but it looks like some kind of "bug" from my point of view.

As @ciao argued x and x[...] maybe "potentially confusing". This may be true. At least, it confused several functions in Mathematica like FourierCoefficient, FourierSinSeries and other similar functions.

Therefore, although in some case this representation may be easier to understand , the best way to avoid these problems may be not to use this kind of expression. Or, use the solution provided in the question and @JasonB's comments.

Sum[FourierSeries[Sin[x0 - x[i]], x0, 5], {i, 3}] /. x0 -> x

or

 Sum[FourierSeries[Sin[x - magic[i]], x, 5], {i, 3}] /. magic -> x
$\endgroup$
  • 2
    $\begingroup$ Another possible workaround would be to use \[FormalX] as either the expansion variable or the subscripted variable. $\endgroup$ – J. M. will be back soon Apr 21 '16 at 9:46
  • $\begingroup$ @J.M. aha, so \[FormalX] is the "formal" way to do this in MMA ^_^ $\endgroup$ – Kattern Apr 21 '16 at 9:48
  • 1
    $\begingroup$ I think the main thing is that x and x[i] aren't really separate entities to Mathematica. Look at what you get when you run Table[x - x[3], {x, -2, 2}] $\endgroup$ – Jason B. Apr 21 '16 at 9:49
  • $\begingroup$ @JasonB so what is the correct way to generate a group of variable? Any thought about this? I did something like ithVar[var_, i_] := ToExpression[ToString[var] <> "$" <> ToString[i]]; , but it is kind of ugly. $\endgroup$ – Kattern Apr 21 '16 at 9:51
  • 1
    $\begingroup$ No, I like this method, using x[1], etc, you just can't then use x as an iteration variable in the same expression. What I used to use before I knew this was something like ToExpression["x" <> IntegerString[j]]. In your case, Sum[FourierSeries[Sin[x - ToExpression["x" <> IntegerString[i]]], x, 5], {i, 3}] works pretty well $\endgroup$ – Jason B. Apr 21 '16 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.