0
$\begingroup$

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}];

$\endgroup$
3
  • $\begingroup$ The expression Plot[u,{x,0,10}] doesn't contain t explicitly, which is why Animate doesn't know what to do with it. $\endgroup$ Jul 10, 2020 at 8:10
  • $\begingroup$ Notice that, as mentioned in Details and Options section of document of Animate, "Animate generates a Manipulate object containing an Animator. " $\endgroup$
    – xzczd
    Jan 31, 2021 at 13:59
  • $\begingroup$ As to the additional question, "Any way to force mathematica to immediately use only real values? " I don't think so, and that's the reason why we have so many functions for transforming/simplifying. $\endgroup$
    – xzczd
    Jan 31, 2021 at 14:19

1 Answer 1

1
$\begingroup$

Replace the corresponding parts in your code with this,

sol = DSolve[{D[an[t], {t, 2}] + c^2*lambdan*an[t] == rhside, 
     an[0] == a1i, an'[0] == a2i}, an[t], t][[1, 1, 2]];

u = Sum[sol*Sin[n Pi x/N[L]], {n, 1, 50}];

u2[x_, t_] = Re[u] // ComplexExpand;

Animate[Plot[u2[x, t], {x, 0, 10}], {t, 0, 100}]

enter image description here

$\endgroup$

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