%
% Interference.m - Calculates time-dependent interference between
%                  up to four harmonic waves 8/31/09 
% Modified: parameter control, 09/01/16 M.K.

function Interference;
tmax = 10; nt = 200;

% wave parameters, amplitudes:
    a1 = 1.0; a2 = 1.5; a3 = 0.0; a4 = 0.0;

% wave parameters, wavenumbers:
    k1 = 2*pi; k2 = 0.1*2*pi; k3 = 0.7*2*pi; k4 = 2*pi;

% wave parameters, velocities:
    v1 = 1.0; v2 = 1.0; v3= 1.0; v4 = 1.0;

%Parameters1

x = [0:0.01:50];
for i=1:nt
    t = (i-1)*tmax/(nt-1);
    psi(:,i) = a1*sin(k1*(x(:)-v1*t))  +  a2*sin(k2*(x(:)-v2*t))...
             + a3*sin(k3*(x(:)-v3*t))  +  a4*sin(k4*(x(:)-v4*t));
end

animate(psi,x);
set(gca,'FontSize',15);
xlabel('x (microns)');
ylabel('\psi(x,t)');
title('Interference or beat pattern');

%
function [mov] = animate(arr,v)
if nargin < 2, v = [1:size(arr,1)]'; end
mx = max(max(arr));
mn = min(min(arr));
mnx = min(v);
mxx = max(v);
mov = moviein(size(arr,2));
for i = 1:size(arr,2)
    plot(v,arr(:,i));
    xlim([mnx,mxx]);
    ylim([mn,mx]);
    mov(:,i) = getframe;
end
