Wave on string from [0,L=10]. Endpoints fixed and initial conditions a double sinc: Ti1(x) = Sinc[3*(x - 7)] + Sinc[3*(x - 3)]. Initial velocity Ti2(x)= 0. Evaluating as expansion and trying to animate fails without ugly code.
c = 1;
L=10;
B1 = Sinc[3*(0 - 7)] + Sinc[3*(0 - 3)];
B2 = Sinc[3*(10 - 7)] + Sinc[3*(10 - 3)];
r = B1 + (x/L)*(B2 - B1);
lambdan = (n Pi/L)^2;
Ti1 = Sinc[3*(x - 7)] + Sinc[3*(x - 3)] ;
Ti2 = 0;
Q = (x/L - 1) D[B1, {t, 2}] - (x/L) D[B2, {t, 2}] // FullSimplify;
rhside = Integrate[Q*Sin[n Pi x/L], {x, 0, L}] / Integrate[Sin[n Pi x/L]^2, {x, 0, L}] // FullSimplify;
a1i = Integrate[(Ti1 + (x/L - 1)*(B1 /. t -> 0) - (x/L)*(B2 /. t -> 0) )*Sin[n Pi x/L], {x, 0, L}] / Integrate[Sin[n Pi x/L]^2, {x, 0, L}];
a2i = 0;
an = DSolve[{D[an[t], {t, 2}] + c^2*\[lambdan*an[t] == rhside, an[0] == a1i, an'[0] == a2i}, an[t],t][[1, 1, 2]] // FullSimplify;
u = Sum[an*Sin[n Pi x / N[L]], {n, 1, 50}];
u = Re[u]//ComplexExpand;
Animate[Plot[u,{x,0,10}],{t,0,100}];
If I don't suppress the output of u, then copy and paste the (very long) expansion explicitly into Animate[] then the animation will work.
As an additional question, I don't like having to add the second line for u, wrapping it in the real function and applying ComplexExpand. Shouldn't the series evaluate to a real value. Any way to force mathematica to immediately use only real values?
Animate[Plot[u,{x,0,10}],{t,0,100}];

Plot[u,{x,0,10}]doesn't containtexplicitly, which is whyAnimatedoesn't know what to do with it. – Sjoerd Smit Jul 10 '20 at 08:10Animate, "Animategenerates aManipulateobject containing anAnimator. " – xzczd Jan 31 '21 at 13:59