function lambda_plane_eval_search_schrodinger

clear; clf;
L=13.5;           %U-integration start
L_prime=-13.5;
xspan=[L L_prime];  %x integration range
numsteps=100;  %number of steps used in calculating U_lambda
s_numsteps=10000;   %number of steps around soln loop at L_prime
delta = 2*pi/s_numsteps;    %angle increment

%Schrodinger params
D = 12;
omega = 0.204124;
n = 0:1:23;
E_n = -12 + (n+0.5) - (1/48)*(n+0.5).^2;




%setup graphics boxes
subplot(211);
hold on; grid on;
plot(real(E_n), imag(E_n),'.k');
title('\lambda paths and centres in C-plane, r = 0.14'); xlabel Re(\lambda); ylabel Im(\lambda); zlabel \theta(s)/2\pi;
subplot(212); title('\theta(s)/2\pi for the various paths'); hold on; grid on; ylabel \theta(s)/2\pi; xlabel s;
f=getframe;

lambda_0_RE_counter=0;

for lambda_0_RE = -12:0.2:0.2
    lambda_0_RE_counter = lambda_0_RE_counter+1
    lambda_0_IM_counter = 0;

    for lambda_0_IM = -0.801:0.2:0.8
        lambda_0_IM_counter = lambda_0_IM_counter+1

        %lambda path definition along loop around lambda_0
        lambda_0 = lambda_0_RE + i*lambda_0_IM; %centre of lambda path circle 
        r=0.14;                                  %radius of lambda path circle

        subplot(211)
        for j=1:numsteps
            lambda_array(j)=lambda_0 + r*exp(2*pi*i*j/numsteps);
        end
        plot3(real(lambda_array),imag(lambda_array),zeros(length(lambda_array)),'r');
        plot3(real(lambda_0), imag(lambda_0),0,'.r');
        f=getframe;

        %lambda path integration
        theta_init=0;           %theta initial value
        sspan=[0 2*pi];
        [s theta] = ode45(@theta_integration, sspan, theta_init);

        subplot(212)
        plot(s,theta/(2*pi))
        delta_theta = (theta(length(theta))-theta(1))/(2*pi);
        subplot(211)
        plot3(real(lambda_0),imag(lambda_0),delta_theta,'*k')
        Z(lambda_0_IM_counter,lambda_0_RE_counter)=delta_theta;
    end
end

figure
meshc(Z)


%Nested Functions
%----------------------------------------------------------
%function for integrating connection form around solns at L_prime
    function theta_s = theta_integration(s,theta)
        s
        present_s = s;
        next_s = s + delta; 
        last_s = s - delta;
        
        present_lambda = lambda_0 + r*exp(i*present_s);   
        next_lambda = lambda_0 + r*exp(i*(next_s));          
        last_lambda = lambda_0 + r*exp(i*(last_s));

        lambda = present_lambda;
        present_ksi_plus=[ -sqrt(-2*lambda); 1];                                  %stable evec of A_infinity
        
        lambda = next_lambda;
        next_ksi_plus=[ -sqrt(-2*lambda); 1];                                  %stable evec of A_infinity
        
        lambda = last_lambda;
        last_ksi_plus=[ -sqrt(-2*lambda); 1];                                  %stable evec of A_infinity

        %next_lambda... integration of u from x=L -> L_prime
        xspan = [L L_prime];           %x integration range
        lambda = next_lambda;
        u_init = next_ksi_plus;
        [x u] = ode45(@u_ODE, xspan, u_init);
        next_u=u(length(u),:).'; %results of integration
        clear u

        %present_lambda... integration of u from x=L -> L_prime
        xspan = [L L_prime];
        lambda = present_lambda;
        u_init = present_ksi_plus;
        [x u] = ode45(@u_ODE, xspan, u_init);
        present_u=u(length(u),:).';
        clear u
       
        %last_lambda... integration of u from x=L -> L_prime
        xspan = [L L_prime];
        lambda = last_lambda;
        u_init = last_ksi_plus;
        [x u] = ode45(@u_ODE, xspan, u_init);
        last_u=u(length(u),:).';
        clear u

        u=present_u;
        
        u_s = (next_u - last_u)/(2*delta);

        theta_s = imag((u')*(u_s))/(u'*u);

        %----------------------------------------------------------
        function u_x = u_ODE(x,u)

            V = D*(exp(-2*omega*x) - 2*exp(-omega*x));
            B = 2*(lambda-V);
            A = [	0   -B
                    1    0	];
            u_x = A*u;

        end
        %----------------------------------------------------------
    end

%----------------------------------------------------------

end