The vibrating string example

Solve the wave equation with initial value f(s), initial velocity 0, where f(s)=s if 0<s <L/2 and L-s if L/2 < s < L. Here, we take L=10, c=1.

syms s k n t
evalin(symengine,'assume(k,Type::Integer)');

First we calculate the Fourier sine coefficients of f. We know the coefficients with even index are 0 so we only have to find the ones with odd index.

b = @(k) 4*int(s*sin((2*k-1)*pi*s/10),s,0,5)/10
b = 

    @(k)4*int(s*sin((2*k-1)*pi*s/10),s,0,5)/10

The nth partial sum of the solution is

u = @(s,t,n) symsum(b(k)*sin((2*k-1)*pi*s/10)*cos((2*k-1)*pi*t/10),k,1,n);

Here are some plots of the solution u(s,t) versus s for some fixed values of t (taking 100 terms in the series for u).

ezplot(u(s,0,100),[0,10])
title('t=0')
ezplot(u(s,10/8,100),[0,10])
hold on
fplot('s*0',[0,10],'k')
hold off
title('t=10/8')
axis equal
ezplot(u(s,10,100),[0,10])
title('t=10')

Here are some plots of u(s,t) versus t for some fixed values of s.

T = 0:0.01:20;
U = inline(vectorize(u(5,t,100)));
plot(T,U(T))
title('s=5')
ezplot(u(10,t,100),[0,20])
title('s=10')

Here is a movie of the motion of the string.

S = 0:0.01:10;
for n = 0:100
    U = inline(vectorize(u(s,n,100)));
    plot(S, U(S)), axis([0,10,-5.5,5.5]);
    hold on
    plot(S,0*S,'k'), axis([0,10,-5.5,5.5]);
    hold off
    M(n+1) = getframe;
end


mplay(M,6)