% Animation of change in wavelength v photon energy in the visible regime 

clear; close all; 

Konst = 6.626e-34*2.9979e8/1.6022e-19; % hc in eV.m 
radFactor = pi/180; 

N = 300; 
z = (1:3500); 
ampl = 0.*z; 
v = VideoWriter('wavelength_v_photonenergy.mp4','MPEG-4'); 
v.Quality = 100; 
open(v); 
set(0,'defaultfigurecolor',[1 1 1]);

for i = 1:300 % scan photon energy from red (1.77 eV) to blue (3.11 eV) 
    lambda = (700 - i); % wavelength in nm 
    % Plot 
    % Begin with colour of plot as function of loop (here, energy) 
    
    if (i <= N/4) % from red (1 0 0) to yellow (1 1 0) 
        rval = 1.0;
        gval = 4*i/N; 
        bval = 0.0;
    elseif (i > N/4) && (i <= N/2) % from yellow (1 1 0) to green (0 1 0) 
        rval = 1.0 - 4*(i - N/4)/N; 
        gval = 1.0; 
        bval = 0.0;
    elseif (i > N/2) && (i <= 3*N/4) % from green (0 1 0) to cyan (0 1 1) 
        rval = 0.0;
        gval = 1.0; 
        bval = 4*(i - N/2)/N; 
    else % from cyan (0 1 1) to blue (0 0 1) 
        rval = 0;
        gval = 1 - 4*(i - 3*N/4)/N; 
        bval = 1.0;
    end 
    
    ampl = sin(2*pi.*(z - 10*i)/lambda) + 1.0; % 10i makes sine wave move to right 
                                               % + 1.0 offsets x-axis position 
    plot(-0.1,2.1,0,3500,z,ampl,'color',[rval, gval, bval], 'LineWidth', 2.0);
        
    xaxisLabel = get(gca,'XTickLabel'); 
    h=get(gca);
    set(gca,'FontName','Helvetica','fontsize',18);
    set(gca,'TickLength',[0.016, 2]); 
    set(gca,'YColor','none'); % Removes left and right y-axes 
    set(gca,'Box', 'off'); % Removes upper x-axis 
    xlim([0 3500]) % Plot 5 red wavelengths, or nearly 9 blue wavelengths 
    ylim([-0.1 2.1]) % 
    set(gca,'linewidth',2); 
    xlabel('z [nm]'); 
    
    str1 = '{\lambda} = ';
    str2 = num2str(lambda,'% 7.0f'); 
    str3 = ' nm, '; 
    strTot = [str1,str2,str3]; 
    hText = text(2150, 2.2, strTot, 'FontSize',14); 

    str1 = 'E = ';
    str2 = num2str(Konst/(lambda*1e-9),'% 7.3f'); 
    str3 = ' eV'; 
    strTot = [str1,str2,str3]; 
    hText = text(2800, 2.2, strTot, 'FontSize',14); 

    
    % Store the frame
    frame = getframe(gcf); 
    writeVideo(v,frame); 
end 
% Output the movie as an mpg file
close(v); 
