% Hua-sheng XIE, huashengxie@gmail.com, IFTS-ZJU, 2012-01-10 14:01
% On Numerical Calculation of the Plasma Dispersion Function, section 10.3
% http://ifts.zju.edu.cn/forum/viewtopic.php?f=18&t=527,bump-on-tail
% close all; clear; clc;
nb=0.1; % nb/n0
Tb=1; % Tb/Te
vd=5; % vd/vtb
zeta=@(x)faddeeva(x)*1i*sqrt(pi);
f=@(x,k)k*k+(1-nb)*(1+x*zeta(x))+nb/Tb*(1+(x/sqrt(Tb)-vd)*zeta(x/sqrt(Tb)-vd));
w=[];w2=[];

kmin=0.01;dk=0.02;kmax=0.4;
% k=[kmin:dk:kmax];
k=kmax:-dk:kmin;
% x0=(0.0+0.1i)*sqrt(2)*kmin;
x0=(1.1-0.01i)*sqrt(2)*kmax;
x20=(-3.1-0.8i)*sqrt(2)*kmax;
for kk=k
    options=optimset('Display','off');
    
    x=fsolve(f,x0,options,kk)*sqrt(2)*kk;
    x0=x/(sqrt(2)*kk);
    w=[w,x];
    
    x2=fsolve(f,x20,options,kk)*sqrt(2)*kk;
    x20=x2/(sqrt(2)*kk);
    w2=[w2,x2];
end
wre=real(w);wie=imag(w); wre2=real(w2);wie2=imag(w2);
% figure;
% set(gcf,'DefaultAxesFontSize',15);
%%
subplot(121);hold on; 
% plot(k,wie,'r+',k,wie2,'rx','LineWidth',2);
plot(k,wie,'r+','LineWidth',2);
% grid on;
% title(['Bump-on-tail, n_b=',num2str(nb),', T_b/T_e=',...
%     num2str(Tb),', v_d=',num2str(vd)]);
xlabel('k\lambda_{De}');ylabel('\gamma/\omega_{pe}'); % wp^2=wpe^2+wpb^2
% xlim([kmin,kmax]);
% ylim([-0.08,0.28]);
legend('\omega^M(1)','\omega^M(2)','\omega^M(3)','\omega^Z(1)',3); legend('boxoff');
text(0.02,0.25,'(a)','FontSize',18);


subplot(122);hold on; 
% plot(k,wre,'r+',k,wre2,'rx','LineWidth',2);
plot(k,wre,'r+','LineWidth',2);
xlabel('k\lambda_{De}');ylabel('\omega_r/\omega_{pe}');
text(0.02,2.72,'(b)','FontSize',18);

text(0.02,-0.5,['n_b=',num2str(nb),', T_b/T_e=',...
    num2str(Tb),', v_d=',num2str(vd)],'FontSize',18);

% print('-dpng',['beam_nb',num2str(nb),'.png']);
