% Cartoons of group and phase velocities in a Gaussian packet 

clear; close all; 

prompt = 'Group faster than phase velocity [g], phase faster than group [p], or equal [e]?:';
str = input(prompt,'s');

if (str == 'g') || (str == 'G')
    vid = VideoWriter('groupVelocityFast.mp4','MPEG-4');
elseif (str == 'p') || (str == 'P')
    vid = VideoWriter('phaseVelocityFast.mp4','MPEG-4');
elseif (str == 'e') || (str == 'E')
    vid = VideoWriter('groupPhaseVelocityEqual.mp4','MPEG-4');
end
vid.Quality = 100;
vid.FrameRate = 60; 
open(vid); 
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7); 
    
for i = 0:pi/20:40*pi  
    hold off; 
    % Sinusoidal wave packet 
    t = 0:pi/500:40*pi;
    if (str == 'g') || (str == 'G')
        Y = sin(2*(i-1.12*t)).*exp(-(i-t).^2/50);
    elseif (str == 'p') || (str == 'P')
        Y = sin(2*(i-0.88*t)).*exp(-(i-t).^2/50);
    elseif (str == 'e') || (str == 'E')
        Y = sin(2*(i-t)).*exp(-(i-t).^2/50);
    end
    plot(t,Y, 'color',[1,0,0], 'LineWidth', 2.0)
    hold on; 

    % Positive Gaussian envelope  
    Y = exp(-(i-t).^2/50);
    plot(t,Y, 'color',[0.25,0.28,0.55], 'LineWidth', 2.0) 
    
    % Negative Gaussian envelope  
    Y = -exp(-(i-t).^2/50);
    plot(t,Y, 'color',[0.25,0.28,0.55], 'LineWidth', 2.0) 
    
    axis square 
    xlim([17.5 100]);
    ylim([-1.1 1.1]);
    
    axis off 
    % Store the frame
    frame = getframe(gcf); 
    writeVideo(vid,frame); 
end
hold off;
% Output the movie as an mpg file
close(vid); 

