
%  twowaves;
%  Some elementary wave exercises to demonstrate the
%  effects of superposition, interferance, etc., between
%  two sinusoidal waves by way of a Matlab movie. 
%
%  Jim Price, January, 2000

clear
clear memory
close all

%  Set some default graphics things.
set(0,'DefaultTextFontSize',14)
set(0,'DefaultAxesFontSize',14)
set(0,'DefaultAxesLineWidth',1.4)
set(0,'DefaultLineLineWidth',1.6)


str2(1) =  {'twowaves:' };
str2(2) =  {'  '};
str2(3) =  {'Demonstrate the superposition of two sinusoidal '};
str2(4) =  {'propagating waves (red and green). The red wave has'};
str2(5) =  {'frequency and wavenumber set to (5, 5). Choose the'};
str2(6) =  {'frequency and wavenumber of the green wave by '};
str2(7) =  {'clicking the mouse anywhere on the dispersion diagram.'}; 
str2(8) =  {'The superposition of the waves is solid blue, and '}; 
str2(9) =  {'their envelope is dashed blue.  The phase speeds '};
str2(10) = {'of the base waves are evident by the motion of  '};
str2(11) = {'red and green asterisks; the blue asterisk is the '};
str2(12) = {'carrier (or average). Note that the envelope, or group,'};
str2(13) = {'moves at the speed of the moving `group` speed.'};
str2(14) = {'  '};
str2(15) = {' ****    Hit any key to continue.    **** '};
str2(17) = {''};
str2(18) = {'Jim Price, January, 2001 and 2010.'};

hf3 = figure(10);
clf
set(hf3,'Position',[50 50 600 500])
set(gca,'Visible','off')
text(0, 0.50, str2,'FontSize', 12, 'HorizontalAlignment', 'left')
pause

figure(1)
clf reset
orient tall

x = -15:0.05:15;   %  the distance domain
dt = 0.1;          %  the time step
nstep = 200;       %  number of time steps to take
M = moviein(nstep);

%  define the wavelengths and wavenumbers of two pure waves
%  the base wave (the second pair is selected with the mouse)
k1 = 5.;
omega1 = 5.;

%  plot the dispersion diagram
subplot(2,1,1)
plot(k1, omega1, 'r*')
hold on; plot([0 10], [0 10], ':b')
axis([4 6 4 6])
axis('square')
grid
xlabel('wavenumber, k')
ylabel('frequency, \omega')
title('choose the dispersion relation')

aaa = ...
  '  Use the mouse to select the (k, \omega) of the second, green  wave.'

[k2, omega2] = ginput(1)
hold on
plot(k2, omega2, 'g*')


c1 = omega1/k1;
c2 = omega2/k2;
cgc = (omega2 + omega1)/(k2 + k1);
cg = (omega2 - omega1)/(k2 - k1);
deltg = 10; if cg <= 0; deltg = -10.; end


for j = 1:nstep
   
   t = j*dt;
   arg1 = k1*x - omega1*t;
   arg2 = k2*x - omega2*t;
   
   s1 = cos(arg1);
   s2 = cos(arg2);   
   sum = s1 + s2;

sg = 2*cos((k2-k1)*x/2 - (omega2 - omega1)*t/2);

subplot(2,1,2)

plot(x, sum, 'b', 'linewidth', 1.5) 
hold on
plot(x, s1, 'r', x, s2, 'g')
plot(x, -sg, '--b', 'linewidth', 1.5)
plot(x, sg, '--b',  'linewidth', 1.5);

plot(t*c1-10, 0, '*r', t*c2-10, 0, '*g', t*cgc-10, 0, '*b')
hp = text(t*cg-deltg, 0, 'group', 'color', 'b');
hold off

 xlabel('x distance')
 ylabel('amplitude') 
 axis([-15 15 -2 2]);
   
 M(:,j) = getframe;
   
end

%  rerun the movie

movie(M,2,6);