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

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

swch=input('Case?');

switch swch
    case 1
        k1=1;
        o1=1;
        k2=2;
        o2=2;
        k3=3;
        o3=3;
    case 2
        k1=1;
        o1=5;
        k2=2;
        o2=1;
        k3=3;
        o3=1;
    case 3
        k1=1;
        o1=5;
        k2=2;
        o2=4;
        k3=3;
        o3=3;
    case 4
        k1=1;
        o1=5;
        k2=2;
        o2=6;
        k3=3;
        o3=3;
    case 5
        k1=1;
        o1=5;
        k2=2;
        o2=2;
        k3=3;
        o3=6;
    case 6
        k1=1;
        o1=5;
        k2=2;
        o2=2;
        k3=3;
        o3=9;
    case 7
        k1=1;
        o1=5;
        k2=2;
        o2=4;
        k3=3;
        o3=6;
    case 8
        k1=1;
        o1=5;
        k2=2;
        o2=4;
        k3=3;
        o3=9;
    case 9
        k1=1;
        o1=5;
        k2=2;
        o2=6;
        k3=3;
        o3=6;
    case 10
        k1=1;
        o1=5;
        k2=2;
        o2=6;
        k3=3;
        o3=9;
    case 11
        k1=1;
        o1=5;
        k2=2;
        o2=0;
        k3=3;
        o3=0;
    case 12
        k1=1;
        o1=0;
        k2=2;
        o2=2;
        k3=3;
        o3=0;
    case 13
        k1=1;
        o1=0;
        k2=2;
        o2=0;
        k3=3;
        o3=3;
    otherwise
        clc;
        display('Available cases are: 1,2,3,4,5,6,7,8,9,10,11,12,13');
        break
end

% domain, bounds and interval

x0=-3*pi;
xn=6*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)+sin(k3*x);

% some vertical reference lines

xl1=zeros(1,1000);
xl2=pi*ones(1,1000);
xl3=2*pi*ones(1,1000);
yl1=linspace(-3.05,3.05,1000);
yl2=yl1;
yl3=yl1;

% plot and parameters

p=plot(x,y,xl1,yl1,'-',xl2,yl2,'--',xl3,yl3,'-');
title(['Travelling Wave Animation for Three Superimposed Travelling Waves, ' sprintf('k1=%d, o1=%d, k2=%d, o2=%d, k3=%d, o3=%d',k1,o1,k2,o2,k3,o3)]);
grid on;
xlabel('x');
ylabel('sin(k1*x-o1*t)+sin(k2*x-o2*t)+sin(k3*x-o3*t)');
xlim([x0 xn]);
ylim([-3.05 3.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)+sin(k3*x-o3*t);
    set(p(1),'YData',y);
    drawnow;
end