function waves1
%WAVES1: MATLAB function M-file that plots and 
%stores solutions to two wave-equation examples
%
%times
x = linspace(-10,10,1000);
dt = .01;
timesteps = 250;
%Example 1
%
%initial function
for k=1:length(x);
        u(k) = .5*(f(x(k))+f(x(k)));
end
fig=plot(x,u,'erase','xor');
axis([-10 10 -1 2]);
title(['Time = ',num2str(0),'s'])
pause
for j=1:timesteps;
    t = j*dt;
    for k=1:length(x);
        u(k) = .5*((f(x(k)-2*t)+f(x(k)+2*t)));
    end
    set(fig,'xdata',x,'ydata',u)
    axis([-10 10 -1 2]);
    title(['Time = ',num2str(t),'s'])
    pause(.04)
end
%
pause
clf
%Example 2
%initial function
for k=1:length(x);
        u(k) = .25*(atan(x(k))-atan(x(k)));
end
fig=plot(x,u,'erase','xor');
axis([-10 10 -1 2]);
title(['Time = ',num2str(0),'s'])
pause
for j=1:timesteps;
    t = j*dt;
    for k=1:length(x);
        u(k) = .25*((atan(x(k)+2*t)-atan(x(k)-2*t)));
    end
    set(fig,'xdata',x,'ydata',u)
    axis([-10 10 -1 2]);
    title(['Time = ',num2str(t),'s'])
    pause(.04)
    
end
%
pause
clf
%Example 3 (Superposition)
%initial function
for k=1:length(x);
        u(k) = .5*(f(x(k))+f(x(k)))+.25*(atan(x(k))-atan(x(k)));
end
fig=plot(x,u,'erase','xor');
axis([-10 10 -1 2]);
title(['Time = ',num2str(0),'s'])
pause
for j=1:timesteps;
    t = j*dt;
    for k=1:length(x);
        u(k) = .5*((f(x(k)-2*t)+f(x(k)+2*t)))+.25*((atan(x(k)+2*t)-atan(x(k)-2*t)));
    end
    set(fig,'xdata',x,'ydata',u)
    axis([-10 10 -1 2]);
    title(['Time = ',num2str(t),'s'])
    pause(.04)
    
end
%
pause
clf
clear x;
x = linspace(0,20,1000);
%Example 4 (quarter-plane)
%initial function
for k=1:length(x);
        u(k) = .5*(f3(x(k))+f3(x(k)));
end
fig=plot(x,u,'erase','xor');
axis([0 20 -1 2]);
title(['Time = ',num2str(0),'s'])
pause
for j=1:timesteps;
    t = j*dt;
    for k=1:length(x);
        if x(k) >= 2*t
            u(k) = .5*((f3(x(k)-2*t)+f3(x(k)+2*t)));
        else
            u(k) = .5*((f3(x(k)+2*t)-f3(2*t-x(k))));
        end
    end
    set(fig,'xdata',x,'ydata',u)
    axis([0 20 -1 2]);
    title(['Time = ',num2str(t),'s'])
    pause(.04)
    
end
%
pause
clf
%Example 5
%initial function
for k=1:length(x);
        u(k) = .25*(atan(x(k))-atan(x(k)));
end
fig=plot(x,u,'erase','xor');
axis([0 20 -1 2]);
title(['Time = ',num2str(0),'s'])
pause
for j=1:timesteps;
    t = j*dt;
    for k=1:length(x);
        if x(k) >= 2*t
            u(k) = .25*((atan(x(k)+2*t)-atan(x(k)-2*t)));
        else
            u(k) = .25*((atan(x(k)+2*t)-atan(2*t-x(k))));
        end
    end
    set(fig,'xdata',x,'ydata',u)
    axis([0 20 -1 2]);
    title(['Time = ',num2str(t),'s'])
    pause(.04)
    
end
pause
clf
%Example 6 (superposition)
%initial function
for k=1:length(x);
        u(k) = .5*(f3(x(k))+f3(x(k)))+.25*(atan(x(k))-atan(x(k)));
end
fig=plot(x,u,'erase','xor');
axis([0 20 -1 2]);
title(['Time = ',num2str(0),'s'])
pause
for j=1:timesteps;
    t = j*dt;
    for k=1:length(x);
        if x(k) >= 2*t
            u(k) = .5*((f3(x(k)-2*t)+f3(x(k)+2*t)))+.25*((atan(x(k)+2*t)-atan(x(k)-2*t)));
        else
            u(k) = .5*((f3(x(k)+2*t)-f3(2*t-x(k))))+.25*((atan(x(k)+2*t)-atan(2*t-x(k))));
        end
    end
    set(fig,'xdata',x,'ydata',u)
    axis([0 20 -1 2]);
    title(['Time = ',num2str(t),'s'])
    pause(.04)
    
end
%
%
function value = f(x)
if x < -1
    value = 0;
elseif x <= 0
    value = 1+x;
elseif x <= 1
    value = 1-x;
else
    value=0;
end
%
function value = f3(x)
if x < 2
    value = 0;
elseif x <= 3
    value = x-2;
elseif x <= 4
    value = 4-x;
else
    value=0;
end
%