% Travelling Wave Animation for Two Superimposed Travelling Waves
% David Bloore, August 2011

% properties of the first wave are k1,o1; second wave k2,o2

swch=input('Case?');

switch swch
    case 1
        k1=1;
        o1=5;
        k2=4;
        o2=5;
    case 2
        k1=1;
        o1=5;
        k2=4;
        o2=50;
    case 3
        k1=1;
        o1=50;
        k2=4;
        o2=1;
    case 4
        k1=1;
        o1=5;
        k2=4;
        o2=0;
    case 4.1
        k1=1;
        o1=0;
        k2=4;
        o2=5;
    case 5
        k1=1;
        o1=5;
        k2=4;
        o2=-10;
    case 6
        k1=1;
        o1=-5;
        k2=4;
        o2=-1;
    otherwise
        clc;
        display('Available cases are: 1,2,3,4,4.1,5,6');
        break
end

% domain, bounds and interval -- adjusts as a funcion of k

x0=-pi;
xn=3*pi;
d=floor(5000*(1/pi)*(xn-x0));

x=linspace(x0,xn,d);

% parameters relating to time, end time (tn), and number of intervals (nti)

tn=10;
nti=1000;

% initial y-values for plot

y=sin(k1*x)+sin(k2*x);

% plot and parameters

p=plot(x,y);
grid on;
title(['Travelling Wave Animation for Three Superimposed Travelling Waves, ' sprintf('k1=%d, o1=%d, k2=%d, o2=%d',k1,o1,k2,o2)]);
xlabel('x');
ylabel('sin(k1*x-o1*t)+sin(k2*x-o2*t)');
xlim([x0 xn]);
ylim([-2.05 2.05]);
set(gcf,'Position',[0 356 1440 900]);

% animation loop

for t=linspace(0,tn,nti);
    y=sin(k1*x-o1*t)+sin(k2*x-o2*t);
    set(p,'YData',y);
    drawnow;
end