% nmode5: Draws normal modes of five coupled oscillators
close all;
set( gcf, 'DoubleBuffer', 'on');

% define the number of oscillators and the fundamental frequency
N = 10;
w = zeros(1, N);
w(1) = 1;
for n = 2:N
   w(n) = w(1) * sin(n*pi/(2*(N+1))) / sin(pi/(2*(N+1)));
end

% define amplitudes
C = zeros(1, N);
C(1) = .43;
C(2) = .1;
C(3) =  .21;
C(4) = .78;
C(8) = .59;
C(6) = .14;

% frames (convenient to use units of time)
t = [ 0 : 0.05 : 20 ];
% dimensions
xres = 0.1;
x = [ 0 : xres : N+1 ];


for i = 1:length(t)
    
    y = zeros(1,length(x));
    for n = 1:N
        y = y + C(n)*cos(w(n)*t(i))*sin(n*pi.*x/(N+1));
    end

    % Draws the background sinusoid
    plot(x, y, 'b-'); hold on;
    
    % Draws each particle
    for n = 1:N
        plot(n, y(n/xres), 'r*'); hold on;
    end
    hold off;
    
    set(gca, 'XLim', [0 N+1]);
    set(gca, 'YLim', [-2 2]);
    
    drawnow;
end


