% What is the self-energy of a thin conducting rod of unit length and unit charge?
% What is the equilibrium distribution of charge?
%
% If you are impatient, the answer is here:
%   http://arxiv.org/pdf/physics/9808038v1.pdf
%
xmin = -0.5; % coordinate of left end or rod
xmax = +0.5; % coordinate of right end of rod
x0 = xmin:0.005:xmax; % initial charge position array 


maxiter = 1000; % maximum number of iterations
tol = sqrt(eps); % tolerance for convergence

t0 = cputime();

[xfinal, Efinal, info, iter] = sqp(x0, @energy, [], [], xmin, xmax, maxiter, tol);

execution_time = cputime() - t0

% Print out some stats...
N = numel(x0)  % Number of charges
Einitial = energy(x0)
Efinal
info
iter
fractional_energy_change = (Efinal - Einitial)/Efinal

hold off
plot(x0, xfinal,'r-')
hold on 
xlabel('initial x')
ylabel('final x')
title('Test 1 completes with "warning: division by zero"')
print('test1.pdf','-dpdf')
hold off