% k-dep eigvals of douple-layer op.
% barnett 11/5/08

verb = 1;    % 0 for sweep plot, 1 for animation in complex plane
w = 3; a = 0.3;
R = @(t) 1 + a*cos(w*t); Rp = @(t) -a*w*sin(w*t);   % wobbly shape
Rpp = @(t) -a*w*w*cos(w*t);

kress = 1;
N = 80; %1600;  %80                                           % # quad pts
t = (1:N)/N*2*pi; sp = sqrt(Rp(t).^2 + R(t).^2);  % speed func
w = ones(size(t))*2*pi/N;                         % weights, peri trap quad
y = [R(t).*cos(t); R(t).*sin(t)];
% n_y * speed:
nysp = [0 1;-1 0]*[Rp(t).*cos(t) - R(t).*sin(t); Rp(t).*sin(t) + R(t).*cos(t)];
ypp = [-R(t).*cos(t) - 2*Rp(t).*sin(t) + Rpp(t).*cos(t); ...
       -R(t).*sin(t) + 2*Rp(t).*cos(t) + Rpp(t).*sin(t)];
ny = nysp./repmat(sp, [2 1]);           % unit n_y
kappa = -sum(ny.*ypp,1)./sp.^2;
Rjn = kress_Rjn(N/2);                   % Kress weights
y1 = repmat(y(1,:), [N 1]); y2 = repmat(y(2,:), [N 1]);
ny1 = repmat(ny(1,:), [N 1]); ny2 = repmat(ny(2,:), [N 1]);
r = sqrt((y1-y1').^2+(y2-y2').^2); r(diagind(r)) = 1; % dummy diag
costh = -(ny1.*(y1-y1') + ny2.*(y2-y2'))./r;
if verb, figure; end
ks = 0.03:0.03:20;                  % for animation
%ks = 2.7047:0.000001:2.7048;
%ks = 1.51:0.00001:1.512;
s = 0*ks;
for j=1:numel(ks);
  k = ks(j); K = (k*1i/4)* costh .* besselh(1, k*r);
  if kress
    D1 = -(k/4/pi)* costh .* besselj(1, k*r);
    K = K - D1.*circulant(log(4*sin(pi*(0:N-1)/N).^2) - (N/2/pi)*Rjn);
  end
  K(diagind(K)) = -kappa/(4*pi);
  K = K * diag(w.*sp);                        % scale cols, weights and speed
  if verb, d = eig(2*K); plot(real(d), imag(d), 'k+'); circle(0, 0, 1, 'r:');
  xlabel('Re eig(2K)'); xlabel('Im eig(2K)');
  axis equal; axis([-1.5 1.5 -1.5 1.5]); text(1,1,sprintf('k=%g',k)); drawnow;
  else
    d = svd(eye(N)+2*K); s(j) = min(d);
  end
end
figure; plot(ks, s, '+-');
ks(find(s==min(s)))
