% Cartoon of a vibrating diatomic molecule 

clear; close all; 
vid = VideoWriter('vibratingMolecule.mp4','MPEG-4'); 
vid.Quality = 100; 
vid.FrameRate = 30; 
open(vid); 
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7); 
lp = [0.4 -0.4 0.7]; 
ec = [0.25 0.28 0.5]; % Phil blue 
fc = [1.0 0.83 0]; % Gold
LL= 7;


for i=0:29
    hold off; 
    newplot; 
    xlim([-LL LL]);
    ylim([-LL LL]);
    zlim([-LL LL]);
    axis equal;
    axis off;
    light('Position',lp,'Style','infinite'); 
    set(gca, 'Projection','perspective');     
    axis equal 
    axis off 
    
     
    
    view(20,40);
    hold on;
    xlim([-LL LL]);
    ylim([-LL LL]);
    zlim([-LL LL]);

    wavex0 = -2.5:0.00025:2.5;
    ampl = 1 + 0.2*sin(2*pi*i/30);
    wavex = ampl*wavex0;
    wavey = sin(5*pi*wavex/ampl);
    wavez = cos(5*pi*wavex/ampl);
    plot3(wavex,wavey,wavez,'color',ec, 'LineWidth', 3.0);
    r = 2;
    xpos = max(wavex)+r
    [x,y,z] = sphere(70); surf(xpos+x*r,y*r,z*r,'FaceColor',ec,'EdgeColor','none');
    [x,y,z] = sphere(70); surf(-xpos+x*r,y*r,z*r,'FaceColor',ec,'EdgeColor','none');
    frame = getframe(gcf);
    writeVideo(vid,frame);
end

frame = getframe(gcf);
writeVideo(vid,frame);
close(vid);