%Created by Eric Cox - http://ericboy.wordpress.com - 5/17/2012

function [] = PWT(n,f,df,t)

%This function plots pendulum wave motion as seen from the top view.
%
%   n is the number of pendulums, f is the frequency of the first pendulum 
%   in the wave, df is the incremental change in frequency between pendulums
%   (added), t is the time to display the animation

%set initial time to zero.
t_n=0;

%preallocate lengths of matrices.
y=(1:n);
x=(1:n);

filename=[datestr(now,30) '.avi']
writerObj = VideoWriter(filename);
writerObj.FrameRate = 30;
open(writerObj);

while (t>t_n)
    
    %for each value in x,y matrices, populate with value at current t.
    for j=1:n  
        x(j)=j;
        y(j)=sin(2*pi*(f+(j)*df)*t_n+pi/2);
    end

%Plot pendulum wave
plot(x,y,'o','MarkerEdgeColor','k',...
                'LineWidth',2,...
                'MarkerFaceColor','b',...
                'MarkerSize',11);
            
%Define axis
axis([0 n+1 -1.5 1.5]);

%Increment loop time
t_n=t_n+1/30;

frame = getframe;
writeVideo(writerObj,frame);

%Animation delay
pause(1/30);

end

close(writerObj);