% 6.4.4 d'Alembert solutions to square wave and sawtooth wave.

N=1000;
dx = 2 * pi/(N-1);
x = -pi:dx:pi;

t = [0,1,pi];
SW = zeros(N,3);
ST = zeros(N,3);
for j = 1:3
SW(:,j) = square(pi/2 + x-t(:,j))/2 + square(pi/2 + x+t(:,j))/2;
ST(:,j) = square(x+pi/2 - t(:,j)).* sawtooth(x+pi/2 - t(:,j)) + square(x+pi/2 + t(:,j)).* sawtooth(x+pi/2 + t(:,j)) + 1;
end

for j = 1:3
	subplot(3,2,2*j-1)
	plot(x,SW(:,j)*pi/4)
	axis([-pi pi -2 2])
	xlabel('x')
	ylabel('SW')
	title(sprintf('SW(x,%1.2f)',t(:,j)))
	subplot(3,2,2*j)
	plot(x,ST(:,j)*pi*pi/8)
	xlabel('x')
	ylabel('ST')
	axis([-pi pi -2 2])
	title(sprintf('ST(x,%1.2f)',t(:,j)))
end

