% Plots of d'Alembert's solutions for one fixed end, one free end and g=0.
clear

% x - mesh
a=0;
b=1;
L=b-a;
N=40;
x=linspace(a,b,N);

% t - mesh
T=10;
tsteps=50;
t=linspace(0,T,tsteps);

% speed
c=1;

% Initial profiles 
f = @(x) (sin(3*pi*x/2/L)); % One Sinusoidal mode 
%f = @(x) (sin(pi*x/2/L)+.5*sin(3*pi*x/2/L)); % Two Sinusoidal modes 
%f = @(x) (2*x.*(2*L-x)); % Parabolic function 
%f = @(x) ((x < 0.25*L).*x.*(.25*L-x))*50+((x>=0.25*L).*0); % Pulse function 
f = @(x) ((x < 0.5*L).*x) + ((x>=0.5*L).*(L-x)); % Pluck in middle

% Plot profile
figure(1)
plot(x,f(x),'r')
pause

% Extensions
feven = @(x)(((x < L).*f(x)) + (( x<2*L & x>=L).*f(2*L-x)) ... 
    +  ( x<3*L & x>=2) .*(-f(3*L-x))+ (( x>=3) .*(-f(4*L-x))));  % Even/Odd extension
fper = @(x) ((feven(mod(x,4*L))));                 % Periodic Extension

% Plot functions
figure(2)
y=linspace(0,2,4*N);
z=linspace(0,4,8*N);
plot(z,fper(z),'k:',y,feven(y),'b',x,f(x),'ro')
pause

% Animation of extensions
figure(3)
for k = 1:tsteps
	plot(z,fper(z-c*t(k)))
    hold on
    plot(z,fper(z+c*t(k)),'r')
    plot(z,(fper(z-c*t(k))+fper(z+c*t(k)))/2,'k')
    hold off
	axis([0,4,-2,2])
	M(k) = getframe;
    pause(0.3)
end
beep
movie(M,1);
pause

% Animation of string
figure(4)
for k = 1:tsteps
	plot(x,(fper(x-c*t(k))+fper(x+c*t(k)))/2)
	axis([0,L,-2,2])
	M2(k) = getframe;
    pause(0.3)
end
%pause
%movie(M,1);