% exercise 13
% wave equation
% Raphael Kruse, 16 Jun 2009

% (i)

% initial value
u0 = @(x) (x<pi).*(x>-pi).*(cos(x) + 1);

% solution
u = @(x,t) 0.5*(u0(x+2*t)+u0(x-2*t));

% generating the grid
x = linspace((-2*pi),(2*pi),101);
t = linspace(0,2,101);
[X,T]=meshgrid(x,t);

% animation
figure(1)
subplot(2,2,1)
for j = 1:length(t)
    plot(x,u(x,t(j)))
    title(['Wellengleichung (i), t = ' num2str(t(j))])
    xlabel('x')
    ylabel('u(x,t)')
    axis([-2*pi 2*pi 0 2])
    pause(0.1)
end
    
% generating the surface plot
subplot(2,2,2)
surf(X,T,u(X,T)) 
xlabel('x')
ylabel('t')
zlabel('u(x,t)')
    

% (ii)
% initial value
u0 = @(x) x.^2;
v0 = @(x) 2*x;

% solution
u = @(x,t) (x+t).^2;

% generating the grid
x = linspace(-2,1,101);
t = linspace(0,1,101);
[X,T]=meshgrid(x,t);

% animation
%figure(3)
subplot(2,2,3)
for j = 1:length(t)
    plot(x,u(x,t(j)))
    title(['Wellengleichung (ii), t = ' num2str(t(j))])
    xlabel('x')
    ylabel('u(x,t)')
    axis([-2 1 0 4])
    pause(0.1)
end


% generating the surface plot
%figure(4)
subplot(2,2,4)
surf(X,T,u(X,T))
xlabel('x')
ylabel('t')
zlabel('u(x,t)')





