%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MATLAB wave script
%% M. Peszynska,  2008/10
%% Copyright Department of Mathematics, Oregon State University
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%%% INSTRUCTIONS: type wave
%%%%%%%%%%%%%%%%%%%
%%% you can comment out different choices in
%%%	  initfun to change shape
%%% you can implement nonconstant initial velocity (use numerical integration)
%%% you can implement solution on a semi-infinite or finite interval
%%% as well
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
function wave
x=linspace(-10,10,10000);
%% fixed speed
c=1;
%% initial velocity is constant;
initvel = 0;
plot(x,initfun(x));
axis([-10 10 -10 10]);
v=axis;
title('Initial condition');
pause
for t=0:0.1:3
    s=sprintf('Time = %g',t);
    current=1/2*(initfun(x-c*t)+initfun(x+c*t))...
        +1/(2*c)*initvel*( (x+c*t)- (x-c*t)); %% note the assumption is of constant initvel
    plot(x,current);
    axis (v);
    title(s);
    pause (0.1);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function v = initfun(x)
v = box(x);%-2*box(x);
%v=hat(x)-3*hat(x+3);
%v=max(0,1-x.^2);
%v=1./(1+x.^2);
%v=max(0,sin(pi*max(x,pi)));
%v=sin(x);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function v = box(x)
for j=1:length(x)
    if abs(x(j))<1
        v(j)=1;
    else
        v(j)=0;
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function v = hat(x)
v=max(0,1-abs(x));
