% Plots of d'Alembert's solutions for two fixed ends 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) ((x < 0.5*L).*x) + ((x>=0.5*L).*(L-x)); % Pluck in middle
%f = @(x) ((x < 0.25*L) .*x*2) + ((x>=0.25*L) .*(L-x)*2/3); % Pluck at x=.25
%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) (2*x.*(L-x)); % Parabolic function 
%f = @(x) ((x < 0.25*L).*x.*(.25*L-x))*50+((x>=0.25*L).*0); % Pulse function 

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

% Extensions
fodd = @(x)((x < L) .*f(x)) + ((x>=L) .*-f(2-x)); % Odd extension
fper = @(x) ((fodd(mod(x,2*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,fodd(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
movie(M,1);
%pause

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