clear vars
close all
clc

%% Global parameters

c = 2.998E10;          % speed of light [cm/s]
Eps_0 = 8.854E-12;     % Vac Permittivity [C/V/m]
e = 1.602E-19;         % elementary charge [C]
M = 9.109E-31;         % mass of an electron [kg]
Me = 0.4*M;            % electron effective mass [kg]

%% Window of interest

nmax=1;               % maximum bessel function order
Wmin=290;              % wavelength min in nm
Wmax=290;            % wavelength MAX in nm
lambda=(Wmin:1:Wmax)*1e-9;
energy = 1239.84./(lambda*1e9);
N_radii=1;                     % number or NC radii calculated
r=linspace(5.5,5.5,N_radii)*1e-9; % linearly spaced radii

%% Parameters for calcultating the epseff of ITO

epsInf=4.0;            % ITO epsinf
epsSolvent= 1.5053^2;  % PCE
N=sqrt(epsSolvent);    % refractive index of the medium;
ne_c=1e27;             % electron concentration #e*m^-3
nu = (2*pi*c/100)./lambda;
w_c=sqrt(ne_c*e^2/(Me*Eps_0)); % plasma frequency %rad*s^-1
wc=w_c/(2*pi*c);
nu_p_c = wc;              %cm-1
nu_p_c = nu_p_c*2*pi*c;   %rad*s^-1
gamma_c= 2081;
gamma_c = gamma_c*2*pi*c; %rad*s^-1

index1=0;
index2=0;

epsrealmin=-400;
epsrealmax=400;

epsimagmin=-400;
epsimagmax=0;

ler=length(epsrealmin:epsrealmax)
lei=length(epsimagmin:epsimagmax)

Csca=zeros(ler,lei);
Cabs=zeros(ler,lei);
Cext=zeros(ler,lei);

for epsreal=epsrealmin:epsrealmax
    index1=index1+1;
for epsimag=epsimagmin:epsimagmax
    index2=index2+1;

epseff = epsreal + 1i*epsimag;

N1= sqrt( (abs(epseff)+real(epseff))/2 ) + 1i* sqrt( (abs(epseff)-real(epseff))/2 );
m=N1/N;
k=2*pi*N./lambda;

tic
for iii=1:N_radii
    x = k.*r(iii); % Size parameter
    mx = m.*x;
    sca = 0;
    ext = 0;
    for n=1:nmax
        % Refer to Bohern EQ.4.53
        jx  = sqrt(pi./(2.*x)).*besselj(n+0.5,x);
        jmx = sqrt(pi./(2.*mx)).*besselj(n+0.5,mx);
        hx  = sqrt(pi./(2.*x)).*besselh(n+0.5,x);
        
        jx_1  = sqrt(pi./(2.*x)).*besselj(n-0.5,x);
        jmx_1 = sqrt(pi./(2.*mx)).*besselj(n-0.5,mx);
        hx_1  = sqrt(pi./(2.*x)).*besselh(n-0.5,x);
        %Derivative terms
        Dxjx   = x.*jx_1 - n.*jx;
        Dmxjmx = mx.*jmx_1 - n.*jmx;
        Dxhx   = x.*hx_1 - n.*hx;
        
        An = (m.^2.*jmx.*Dxjx-jx.*Dmxjmx)./(m.^2.*jmx.*Dxhx-hx.*Dmxjmx);
        Bn = (jmx.*Dxjx-jx.*Dmxjmx)./(jmx.*Dxhx-hx.*Dmxjmx);
        sca = sca + (2*n+1).*(An.*conj(An)+Bn.*conj(Bn));
        ext = ext + (2*n+1).*real(An+Bn);
    end

    Csca(index1,index2) = 2*pi./(k.^2).*sca;
    Cext(index1,index2) = 2*pi./(k.^2).*ext;
    Cabs(index1,index2) = Cext(index1,index2)-Csca(index1,index2);
end

end
index2=0;
end

ratio=Cabs./Csca
toc

%%Image plots

figure
hold on
imagesc(epsrealmin:epsrealmax,epsimagmin:epsimagmax,abs(ratio'))

%% Show the colorbar
colorbar

%% Set the log colobar
set(gca,'ColorScale','log')
