Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to solve the wave PDE using the Fourier method and I need to animate the plots of the partial sum of order 45 -- U45(x,t) with initial conditions U45(x,t0), {t0, 0, 12, .2}.

I am confused: should I define the function f[x, t, k_] or should I do it as f[x, k_] and later access the 't'.

a[k_] := 
    {2/(k*π) Integrate[Cos[(k*π*x)/6] (2 - x) Sin[π*x]^2, {x, 1, 3}], k != 0},
    {1/6 Integrate[(2 - x) Sin[π*x]^2, {x, 1, 3}], k == 0}}]

f[x, t, k_] := 
  a[0]*t + Sum[a[i] Cos[(i*π*x)/6] Sin[(i*π*t)/6], {i, k}]

sol = f[x, t, 45];

Anyway, the above is the piece of code I've written so far. Tried a dozen different Plot3D combinations, but the error I receive tells me I can not replace t. I am open to everything tutorials, papers, tips, plain solutions, everything.

share|improve this question
Please consider using an ASCII equivalent for your name if such a thing exists to make it easier for others to interact with you here. According to Google Translate that is Nikola Dimitrov. Is that acceptable to you? –  Mr.Wizard Jan 21 '13 at 18:12
Yes, I am sorry for the inconvenience. –  Sektor Jan 21 '13 at 18:49
No problem, and thank you. –  Mr.Wizard Jan 21 '13 at 19:57

1 Answer 1

up vote 2 down vote accepted

In your definition of f you should have f[x_,t_,k_], x_ means "Match anything and let it be referenced by x" you might want to read about patterns in general in the documentation.

Clear[a, f];
a[k_] := Piecewise[{
   {0, k == 12},
   {(1728 (k (-144 + k^2) \[Pi] Cos[(k \[Pi])/6]
    - 18 (-48 + k^2) Sin[(k \[Pi])/6]) Sin[(k \[Pi])/3])
         /(k^3 (-144 + k^2)^2 \[Pi]^3), k != 0},
   {0, k == 0}}]

f[x_, t_, k_] := a[0]*t + Sum[a[i] Cos[(i*\[Pi]*x)/6] Sin[(i*\[Pi]*t)/6], {i, k}]

Plot3D[f[x, t, 45], {x, -1, 1}, {t, 0, 2}]


I replaced the Integrate in your definition of a to not have to calculate it over and over again, thereby speeding up the plot.

share|improve this answer
I heart NDSolve –  drN Jan 20 '13 at 20:31
Thank you for your explanation and reference ! :) –  Sektor Jan 20 '13 at 20:34
+1 for adding the speed hint. To be more pedagogical, it's worth pointing out that the definition of a[k_] was OK in principle, except for the little difference between := and =. In this case, all that was needed is to use the latter (Set) to get the same speedup that you achieved by copying and pasting into the definition of a[k_]. –  Jens Jan 20 '13 at 20:35
@Jens Yea, problem was that for k=12 the integral should be zero but the resulting expression was undefined (limit was still zero though) so had to do it manually anyway –  ssch Jan 20 '13 at 20:40
Right, that was probably an oversight in the original Piecewise... –  Jens Jan 20 '13 at 20:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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