%
%  Making movies: motion of N masses connected with springs
%             Victor Matveev, June 9 2010
%   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global N;
N = 10;   % number of masses (change this and see what happens)

figure('Position',[1 200 900 200]);  % Window position and size
colors = rand(N,3);                  % Create N random [R G B] colors

T = 30; % simulation time
L = 5;  % Horizontal size of each spring in the plot 

IC(1:2*N) = 0; % Initialize the initial condition to zero
IC(1)     = 1; % Pluck the first mass only

[T, Y] = ode45(@Nsprings, [0 T], IC);  % Solve the Newton's equations

for i=1:length(T);  % Loop over all time points in the solution
    hold off;
                    % Draw lines first
    for j = 1:N-1
        plot([Y(i,j) + (j-1)*L, Y(i,j+1) + j*L], [10 10], '-', 'color', colors(j,:));
        hold on;
    end;
					% Now draw the mass positions (circles)
    for j = 1:N
        plot(Y(i,j) + L*(j-1), 10,'o','markersize',10,'markerfacecolor', colors(j,:));
    end
        
    axis([-10 L*(N+1) 0 20]);
    title( ['System of ', num2str(N), ' masses connected with springs'] );
    xlabel('Position');
    drawnow;
end;
    

    
   
    
    
    
    

    
    
    
    



