function integrate_x_direction
%INTEGRATE_X_DIRECTION integrates u_x=Au and appended
%connection form equation from x=L->L'

clear; clf;
L=10;           %U-integration start
L_prime=-L;
numsteps=100;
k=0;

%define lambda
lambda_0=1.0+1.0*i;           %centre of lambda path circle
r=0;                    %radius of lambda path circle
phi_1=0;            %angle determining where we begin to integrate in x direction
lambda = lambda_0 + r*exp(i*phi_1);
mu=sqrt(lambda+1);

%setup graphics boxes
subplot(131);
eigenvalues=[-0.75,0,1.25];%[-0.75,0,1.25];
plot(real(eigenvalues), imag(eigenvalues),'*k');
hold on; grid on; axis([-1.5,3,-2,2]); xlabel Re(\lambda); ylabel Im(\lambda);
title(['\lambda = ', num2str(lambda)]);
subplot(132); set(gca,'XDir','reverse');
title(['x-\theta/(2\pi) plot, L = ', num2str(L), ' , L_{prime} = ',num2str(L_prime)]);
axis([-10,10,-4,4]); xlabel x; ylabel \theta(x)/2\pi; hold on; grid on;


%draw lambda picture
subplot(131)
plot(real(lambda),imag(lambda),'+k');hold on;

%--------------------------------------------------------------------------
%integrate the connection from x=L->x=L_prime at lambda=r*exp(i*phi_1)
stepsize=L/numsteps;

clear theta
j=1;
ksi_1=[1;-mu];                         %stable evec of A_infinity exp(-mu*L)
u_init=ksi_1;                          %starting vector u(L,lambda)
theta_init=0;
start_theta=theta_init;
path_on_S2_array=[0 0 0];
extended_u_init = [u_init;theta_init];
xspan=L:-stepsize:L_prime;
[x extended_u] = ode45(@ODE_integration, xspan, extended_u_init);
u=[extended_u(:,1) extended_u(:,2)];
theta=[extended_u(:,3)]/(2*pi);

%v=u./abs(u);

end_theta=theta(length(theta));
delta_theta=(end_theta-start_theta)

subplot(132)
plot(x,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-0.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-0.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),'w');
comet3(path_on_S2_array(:,1),path_on_S2_array(:,2),path_on_S2_array(:,3));

a=length(theta); %new a,b
b=length(path_on_S2_array);
if a>=b
    for j=1:a
        k=ceil((j/a)*b);
        subplot(132); plot(xspan(j),theta(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(132); plot(xspan(k),theta(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,'integrate_x_direction.avi');


%Nested Functions
%-----------------------------------------------------------
%function for integrating along x=L->L_prime
    function extended_u_x = ODE_integration(x,extended_u)        
        u = [extended_u(1)                                         %u=(u1,u2) in C^2
             extended_u(2)  ];
         theta = extended_u(3);
        
        f = (3/2)*((sech(x/2))^2);
        A = [	0               1
                lambda+1-2*f    0	];
        dtheta_dx = (imag(u'*(A*u)))/(u'*u);
        u_x = (A - (-mu)*eye(2))*u;
        extended_u_x = [u_x
                        dtheta_dx];
        
       mod_u= sqrt( (real(u(1)))^2 + (imag(u(1)))^2 + (real(u(2)))^2+ (imag(u(2)))^2 );
       v=u/mod_u;                                               %v=(v1,v2) in S^3 in C^2
       
        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;
    end
%----------------------------------------------------------
end