function Control1_1_JMS(version)
% Control1_1.m
% Solution to problem 1 of control 1
% JMS, Oct.2019

% Set parameters
K = 8.99e9;    % Coulomb constant (N*m^2/C^2)
N = 200;       % number of particles
Q = 1.e-7;     % absolute charge of each particle (C)
b = 0.1e-3;    % min distance to origin (m)
if strcmp(version,'A')
  beta = 0.25; % angular distance between particles (rad)
elseif strcmp(version,'B')
  beta = 0.11;
elseif strcmp(version,'C')
  beta = 0.05;
elseif strcmp(version,'D')
  beta = 0.15;
elseif strcmp(version,'Z')
  beta = 0.1;
end

% Set particle charges and positions
k = 1:N;
q = Q*(-1).^k;
x = b*k.*cos(beta*k);
y = b*k.*sin(beta*k);

% Plot positions
figure(11)
plot(x,y,'o')
axis equal
grid on
xlabel('x (m)')
ylabel('y (m)')

% Find distance and energy between particles
for k = 1:N
  for j = 1:N
    D(k,j) = sqrt( (x(k)-x(j))^2 + (y(k)-y(j))^2 );
    U(k,j) = K*q(k)*q(j)/D(k,j);
  end
  U(k,k) = 0;
end
Utot = 0.5*sum(sum(U))

% Plot interactions of particle 30
figure(12)
plot(U(30,:),'.')
grid on
xlabel('particle index')
ylabel('U_{30,j} (J)')

% Plot interparticle energies
figure(13)
plot(sum(U))
grid on
xlabel('particle index')
ylabel('potential energy (J)')

end
