
clear

X = linspace(-5,5,101);
T = linspace(0,6,51);
u0 = @(x) exp(-4*(x+1).^2);
for i=1:length(T)
    U(:,i) = (u0(X+T(i)*ones(size(X)))+u0(X-T(i)*ones(size(X))))/2;
end

figure(1)
mesh(X,T,U')
axis tight
xlabel('$x$','interpreter','latex')
ylabel('$t$','interpreter','latex')
zlabel('$u$','interpreter','latex')
title('Wave equation ','interpreter','latex')
set(gca,'fontsize',18,'zlim',[0 1])

figure(2)
plot(X,U(:,1),X,U(:,5),X,U(:,11),X,U(:,21),'linewidth',1)
axis tight
xlabel('$x$','interpreter','latex')
ylabel('$u$','interpreter','latex')
title('Wave equation ','interpreter','latex')
l = legend('$t=0$','$t=0.48$','$t=1.2$','$t=2.4$');
set(l,'interpreter','latex','location','northeast')
set(gca,'fontsize',18,'ylim',[0 1])

figure(3)
for i=1:length(T)
    plot(X,U(:,i),'k','linewidth',1)
    axis tight
    set(gca,'fontsize',18,'xlim',[-5 5],'ylim',[0 1])
    xlabel('$x$','interpreter','latex')
    ylabel('$u$','interpreter','latex')
    M(i) = getframe;
end

movie(M)
