
clear all; close all;

%paramters
v=150; %km/h
v=v*1000/3600;  %speed in meter/sec
M=12000; %kg
dt=0.001; %sec
chainMass = 24; %kg/m

%Initialize variables
t = 0;
x = 0;
oldM = M;
newM = M;
PER_PLOT = 10; 

n=0;

while x<260
    
    dx = v * dt;
    t  = t + dt;
    x  = x + dx
    newM  = oldM + (dx*chainMass);
    v  = oldM*v/newM;  %find new speed from linear momentum conservation
    oldM = newM;
    n=n+1;
    
    % rest for plotting
    if mod(n,PER_PLOT) == 0                    
      plot(x,v*3600/1000,'.');
      hold on;
      ylim([0 150]);
      xlim([0 265]);
      drawnow;
      xlabel('DISTANCE in meters');
      ylabel('speed in KM/H');
      
    end
end
    
    