% Plots of d'Alembert's solutions for g=0.
clear

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

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

% speed
c=1;
type=1; % 1 - two fixed ends, 2 - one fixed end,  3 - two free ends

% Initial profiles 
f = @(x) (sin(2*pi*x/L)); % One Sinusoidal mode 
%f = @(x) (sin(2*pi*x/L)+.5*sin(4*pi*x/L)); % Two Sinusoidal modes 
%f = @(x) ((x < 0.25*L).*x.*(.25*L-x))*50+((x>=0.25*L).*0); % Pulse function 
%f = @(x) ((x < 0.25*L).*0+(x>=0.25*L & x<=0.75*L).*1+((x>0.75*L).*0)); % Box Pulse


% Extensions
fodd = @(x)((x < L) .*f(x)) + ((x>=L) .*-f(2-x)); % Odd extension
foe = @(x)(((x < L).*f(x)) + (( x<2*L & x>=L).*f(2*L-x)) ... 
    + ( x<3*L & x>=2) .*(-f(4*L-x))+ (( x>=3) .*(-f(4*L-x))));  % Even/Odd
feven = @(x)((x < 1) .*f(x)) + ((x>=1) .*f(2-x)); % Odd extension

switch type
    case 1
        fper = @(x) ((fodd(mod(x,2*L))));           
    case 2
        fper = @(x) ((foe(mod(x,4*L))));   
    case 3
        fper = @(x) ((feven(mod(x,2*L))));   
end

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