% This is a MATLAB / Octave script for the mass-spring model we discussed
% in class.  You can download Octave from https://www.gnu.org/software/octave

% choose n random points on the circle

n = 100;
t = rand(n,1);
t = sort(t);
Z = exp(2*pi*i*t);

% display initial configuration

clf;
plot(real(Z),imag(Z),'ro-');
axis([-1,1,-1,1]);
fprintf(stdout,'Hit enter to start\n');
pause;

% evolve

while 1
    
  step = 0.1;
  t = 0;
  while t < step;

    % compute neighbor displacements

    Z1 = Z([2:end,1]) - Z;
    Z2 = Z([end,1:end-1]) - Z;

    % compute crude step size limit h

    h = min(abs(Z1));
    h = min(h/4,step-t);

    % step forward by h
    
    Z = Z + h*(Z1+Z2);
    t = t+h;
  end

  % display

  clf;
  plot(real(Z),imag(Z),'ro-');
  axis([-1,1,-1,1]);
  drawnow;

  
end
