function wave(choice)

close all
% clf

if nargin == 0
    choice = 'exp';
end

T = 5.0;
L = 1;    % Domain length
c = 1;    % Wave speed

show_complete = false;   % Set to false to see solution only on [0,L]



switch choice
    case 'sin'
        finit = @(x) sin(4*pi*x/L);
    case 'exp'         
        finit = @(x) exp(-80*(x-L/2).^2);
    case  'abs'
        finit = @(x) 1-2*abs(x-L/2);
    case 'sin-exp'
        finit = @(x) exp(-40*(x-L/2).^2).*cos(8*pi*x/L);
    case 'sin-exp2'
        finit = @(x) exp(-80*(x-L/2).^2).*cos(16*pi*x/L);
end

u_dalembert = @(x,t) 0.5*(f_odd_periodic(finit,x-c*t,L) + f_odd_periodic(finit,x + c*t,L));

% Numerical parameters
ax = 0;
bx = L;
ay = -1.1;
by = 1.1;

M = 500;    % Number of time steps
N = 500;


x = linspace(-3*L,3*L,1000+1);

plot(x,0*x,'k');
hold on;
plot([0 0],ylim,'k');

t = linspace(0,T,M+1);

plot(x,0*x,'k');
hold on;
set(gca,'fontsize',16);
xlabel('x','fontsize',16);
ylabel('u(x,t)','fontsize',16);

u = u_dalembert(x,0);
h = plot(x,u,'b','linewidth',2);
if show_complete
    axis([-3*L,3*L,ay,by]);
    hleft = plot(x,f_odd_periodic(finit,x,L),'r--');
    hright = plot(x,f_odd_periodic(finit,x,L),'r--');
else
    axis([ax,bx,ay,by]);
end

for i = 2:M+1
    u = u_dalembert(x,t(i));
    
    set(h,'ydata',u);
    title(sprintf('t = %.2f',t(i)),'fontsize',16);
    shg
    if (show_complete)        
        uleft = f_odd_periodic(finit,x+c*t(i),L);
        set(hleft,'ydata',uleft);
        uright = f_odd_periodic(finit,x-c*t(i),L);
        set(hright,'ydata',uright);
        axis([-3*L,3*L,ay,by]);
        % input('Hit enter to see next plot : ');
    else
        axis([ax,bx,ay,by]);
    end

    pause(0.025)
end

end

function fx = f_odd_periodic(f,x,L)

x = mod(x,2*L);

m1 = 0 < x & x <= L;
m2 = L < x & x <= 2*L;

fx = zeros(size(x));

fx(m1) = f(x(m1));
fx(m2) = -f(-(x(m2)-2*L));

end
    
   