function integration_at_L
%INTEGRATION_AT_L integrates the connection around a loop in lambda-space at
%endpoint x=infinity

%we integrate the connection u^H.du around a loop in lambda-space while using the
%eigenvector of the system u_x=Au at +infinity as the vector u. we write
%lambda=r*exp(i*s) and use s to parametrize the path

clear; clf;

%define lambda path
lambda_0=2.0-1.0*i;           %centre of lambda path circle
r=0.8;                  %radius of lambda path circle
phi_1=0;            %angle offset from which to begin integrating

%setup graphics boxes
subplot(131);
eigenvalues=[-0.75,0,1.25];
plot(real(eigenvalues), imag(eigenvalues),'*k');
hold on; grid on; axis([-1.5,3,-2,2]); xlabel Re(\lambda(s)); ylabel Im(\lambda(s)); zlabel x;
%title('\lambda')
title(['\lambda -path: \lambda_0 = ', num2str(lambda_0), ' , r = ',num2str(r)]);
subplot(132);
title('s- \theta/(2\pi) plot'); xlabel s; ylabel '\theta(s)'/2\pi; hold on; grid on; axis([0,8,-0.07,0.07]);

%draw lambda path
numsteps=100;
subplot(131)
for j=1:(numsteps+1)
    lambda_array_for_diagram(j)=lambda_0 + r*exp(2*pi*i*j/numsteps);
end
plot(real(lambda_array_for_diagram),imag(lambda_array_for_diagram),'r');hold on;
clear lambda_array_for_diagram

j=1;
path_on_S2_array=[0 0 0];
theta_init=0;           %theta initial value 
sspan=0:0.05:2*pi;
[s theta_array] = ode45(@theta_integration, sspan, theta_init);
theta_array=theta_array/(2*pi);

subplot(132)
plot(s,theta_array)
delta_theta = theta_array(length(theta_array))
title(['s-\theta/(2\pi) plot, \Delta(\theta)/(2\pi)=',num2str(delta_theta)])


%S^2 figure;
subplot(133)
[a b]=meshgrid(-1:0.05:1,-1:0.05:1);z=real(sqrt(1-a.^2-b.^2));
surf(a,b,z); hold on; title('Image of path on S^3 under Hopf map');
z=-real(sqrt(1-a.^2-b.^2));surf(a,b,z);axis equal;set(gca,'ZDir','reverse');
xlabel '\pi_1(v)'; ylabel '\pi_2(v)'; zlabel '\pi_3(v)';
plot3(path_on_S2_array(:,1),path_on_S2_array(:,2),path_on_S2_array(:,3));

a=length(theta_array); %new a,b
b=length(path_on_S2_array);
if a>=b
    for j=1:a
        k=ceil((j/a)*b);
        subplot(131); plot(real(lambda_0 + r*exp(i*s(j))),imag(lambda_0 + r*exp(i*s(j))),'.k');
        subplot(132); plot(s(j),theta_array(j),'.k');
        subplot(133); plot3(path_on_S2_array(k,1),path_on_S2_array(k,2),path_on_S2_array(k,3),'.y');
        F(j)=getframe(gcf);
    end
else
    for j=1:b
        k=ceil((j/b)*a);
        subplot(131); plot(real(lambda_0 + r*exp(i*s(k))),imag(lambda_0 + r*exp(i*s(k))),'.k');
        subplot(132); plot(s(k),theta_array(k),'.k');
        subplot(133); plot3(path_on_S2_array(j,1),path_on_S2_array(j,2),path_on_S2_array(j,3),'.y');
        F(j)=getframe(gcf);
    end
end
%movie2avi(F,'integration_at_L_2.avi');
%Nested Functions
%-----------------------------------------------------------
%function for integrating around lambdaloop at x=L
    function theta_s = theta_integration(s,theta)
        
        lambda = lambda_0 + r*exp(i*(s+phi_1));
        lambda_array(j)=lambda;

        u=[     1
            -sqrt(lambda+1)];                               %u=(u1,u2) in C^2
        u_lambda=[      0
                  -1/(2*sqrt(lambda+1))];
        lambda_s=i*r*exp(i*(s+phi_1));
        
        %project u on to spere
        v(1)=u(1)/sqrt( (abs(u(1)))^2 + (abs(u(2)))^2 );
        v(2)=u(2)/sqrt( (abs(u(1)))^2 + (abs(u(2)))^2 );    %v=(v1,v2) in S^3 in C^2
        %apply hopf map
        w(3)=conj(v(1))*v(2) + v(1)*conj(v(2));
        w(2)=i*(conj(v(1))*v(2) - v(1)*conj(v(2)));
        w(1)= (abs(v(1)))^2 - (abs(v(2)))^2;                %w=(w1,w2,w3) in S^2 in R^3

        path_on_S2_array(j,:)=w;
        j=j+1;

        theta_s = imag ( u'*u_lambda*lambda_s / (u'*u) );
        1;
    end;
        
%-----------------------------------------------------------

end