Dependence of bending-magnet spectra on storage-ring energy
and magnetic field

Matlab codes 

% Create video showing how total emittance changes as a function of electron

% emittance with optimal beta of L/4pi, starting at horizontal electron

% emittances typical of 3rd generation facilities of 5.5 nm.rad

 

clear; close all;

 

vid = VideoWriter('emittanceChanges2.mp4','MPEG-4');

vid.FrameRate = 30;    % Default 30

vid.Quality = 100;    % Default 75

open(v);

figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');

set(0,'defaultfigurecolor',[1 1 1]); % White background

 

myBlue = [0.4 0.44 0.73];

myGold = [0.7 0.6 0];

 

pos1 = [0.1 0.2 0.6 0.6];

pos2 = [0.74 0.65 0.25 0.25];

pos3 = [0.74 0.375 0.25 0.25];

pos4 = [0.74 0.1 0.25 0.25];

 

undLength = 2; % Length of undulator in m

beta = undLength/(4*pi);

photonE = (0.1:0.002:100); % Range of photon energies in keV

lambda = (1e-10*12.3984)./photonE; % Convert photon energy in keV to wavelength in m

 

part1 = (5500:-40:1500); % 100 frames

part2 = (1480:-20:800); % 34 frames

part3 = (790:-5:100); % 139 frames

part4 = (95:-2.5:10); % 35 frames

 

for ii = [part1 part2 part3 part4]

    subplot('Position',pos1); % Main plot

    

    sigxp = (1/(4*pi))*(lambda.*undLength).^0.5; % Photon beam source size in m

    sigpxp = (lambda./(undLength)).^0.5; % Photon beam divergence in rad

    epsxe = ii*1e-12; % Horizontal electron emittance 5000 to 10 pm.rad

    sigxe = (epsxe*beta)^0.5; % Range of sigma_x^e (horiz e-beam SD width) in m

    sigpxe = (epsxe/beta)^0.5;  % Value of divergence of e-beam in rad for present value of sigxe

    totsigx = (sigxe^2 + sigxp.^2).^0.5; % Convoluted total source size

    totsigpx = (sigpxe^2 + sigpxp.^2).^0.5; % Convoluted total source divergence

    totEmitt = 1e12.*totsigx.*totsigpx; % Total emittance for present value of epsxe

    diffLimEnergy = 12.3984/(4*pi*epsxe*1e10); % Diffraction-limited energy

    diffLimI = round((diffLimEnergy - 0.1)/0.002); % Index of diffraction-limited energy in photonE

    

    if (ii == max(part1)) % Beginning emittance curve at largest electron emittance

        totEmittStart = totEmitt;

    end

    loglog(0.1,50,5,7000,photonE,totEmitt,'color',myBlue, 'LineWidth', 3.0); % Varying curve with electron emittance

    hold on;

    loglog(0.1,50,5,7000,photonE,totEmittStart,'color','r', 'LineWidth', 3.0); % Initial curve

    if (diffLimI >= 1)

        plot(photonE(1,diffLimI),totEmitt(1,diffLimI),'ko',...

            'MarkerFaceColor',myBlue,'MarkerSize',10);

    end

    % Markers for 3 exemplary photon energies

    index300 = round((0.3 - 0.1)/0.002);

    index3k = round((3 - 0.1)/0.002);

    index30k = round((30 - 0.1)/0.002);

    plot(0.3,totEmitt(index300),'ro','MarkerFaceColor','red','MarkerSize',10);

    plot(3,totEmitt(index3k),'ro','MarkerFaceColor','red','MarkerSize',10);

    plot(30,totEmitt(index30k),'ro','MarkerFaceColor','red','MarkerSize',10);

    

    

    str1 = '{\epsilon_x^e} = ';

    str2 = num2str(epsxe*1e12,'% 7.1f');

    str3 = ' pm.rad';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.13 0.4 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{\sigma_x^e} = ';

    str2 = num2str(sigxe*1e6,'% 7.2f');

    str3 = ' {\mu}m';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.13 0.36 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{\sigma_x^{\prime e}} = ';

    str2 = num2str(sigpxe*1e6,'% 7.3f');

    str3 = ' {\mu}rad';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.13 0.32 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{L_u} = ';

    str2 = num2str(undLength,'% 7.1f');

    str3 = ' m';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.13 0.28 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{\beta} = L_u/4{\pi} = ';

    str2 = num2str(beta,'% 7.3f');

    str3 = ' m';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.13 0.24 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    set(gca,'FontName','Helvetica','fontsize',16);

    set(gca,'TickLength',[0.016, 2]);

    set(gca,'xscale','log');

    set(gca,'yscale','log');

    set(gca,'linewidth',2);

    xlim([0.1 100]);

    ylim([8 10000]);

    xlabel('Photon energy [keV]');

    ylabel('Total horizontal emittance [pm.rad]');

    hold off

    

    % 300 eV phase-space plot

    subplot('Position',pos2);

    newplot

    annotation('textbox',[0.77 0.88 0.1 0.1],'String','electron','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom','Color',myBlue);

    annotation('textbox',[0.77 0.855 0.1 0.1],'String','photon','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom','Color',myGold);

    annotation('textbox',[0.77 0.83 0.1 0.1],'String','total','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom','Color','g');

    

    annotation('textbox',[0.9 0.88 0.1 0.1],'String','300 eV','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    set(gca,'xtick',[]); % No tick marks

    set(gca,'ytick',[]);

    

    sigp300 = (1e-10*(12.3984/0.3)*undLength)^0.5/(4*pi); % sigma_p @ 300 eV

    sigpp300 = (1e-10*(12.3984/0.3)/undLength)^0.5; % sigma_p' @ 300 eV

    maxSigx300 = 1.25*((1e-12*max(part1)*beta) + sigp300^2)^0.5; % max sigma_p @ 300 eV

    maxSigpx300 = 1.1*((1e-12*max(part1)/beta) + sigpp300^2)^0.5; % max sigma_p' @ 300 eV

    

    totsigx300 = (sigxe^2 + sigp300^2)^0.5; % Convoluted total source size @ 300 eV

    totsigpx300 = (sigpxe^2 + sigpp300^2)^0.5; % Convoluted total source divergence @ 300 eV

    

    ellipse(sigxe,sigpxe,0,0,0,myBlue); % electron-emittance phase-space ellipse

    hold on

    ellipse(sigp300,sigpp300,0,0,0,myGold); % photon-emittance phase-space ellipse

    ellipse(totsigx300,totsigpx300,0,0,0,'g'); % total-emittance phase-space ellipse

    ax = gca;

    ax.XAxisLocation = 'origin'; % Plot with axes through origin

    ax.YAxisLocation = 'origin';

    xlim([-maxSigx300 maxSigx300]); % Set axes limits

    ylim([-maxSigpx300 maxSigpx300]);

    

    axis square;

    axis on;

    set(gca,'fontsize',18,'linewidth',2);

    xlabel('\sigma');

    ylabel('\sigma^{\prime}');

    

    hold off;

    

    % 3 keV phase-space plot

    subplot('Position',pos3);

    newplot

    annotation('textbox',[0.9 0.605 0.1 0.1],'String','3 keV','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    sigp3k = (1e-10*(12.3984/3)*undLength)^0.5/(4*pi);

    sigpp3k = (1e-10*(12.3984/3)/undLength)^0.5;

    maxSigx3k = 1.25*((1e-12*max(part1)*beta) + sigp3k^2)^0.5;

    maxSigpx3k = 1.1*((1e-12*max(part1)/beta) + sigpp3k^2)^0.5;

    

    totsigx3k = (sigxe^2 + sigp3k^2)^0.5;

    totsigpx3k = (sigpxe^2 + sigpp3k^2)^0.5;

    ellipse(sigxe,sigpxe,0,0,0,myBlue);

    hold on

    ellipse(sigp3k,sigpp3k,0,0,0,myGold);

    ellipse(totsigx3k,totsigpx3k,0,0,0,'g');

    ax = gca;

    ax.XAxisLocation = 'origin';

    ax.YAxisLocation = 'origin';

    xlim([-maxSigx3k maxSigx3k]);

    ylim([-maxSigpx3k maxSigpx3k]);

    set(gca,'xtick',[]);

    set(gca,'ytick',[]);

    

    axis square;

    axis on;

    set(gca,'fontsize',18,'linewidth',2);

    xlabel('\sigma');

    ylabel('\sigma^{\prime}');

    hold off;

    

    % 30 keV phase-space plot

    subplot('Position',pos4);

    newplot

    annotation('textbox',[0.9 0.33 0.1 0.1],'String','30 keV','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    sigp30k = (1e-10*(12.3984/30)*undLength)^0.5/(4*pi);

    sigpp30k = (1e-10*(12.3984/30)/undLength)^0.5;

    maxSigx30k = 1.25*((1e-12*max(part1)*beta) + sigp30k^2)^0.5;

    maxSigpx30k = 1.1*((1e-12*max(part1)/beta) + sigpp30k^2)^0.5;

    

    totsigx30k = (sigxe^2 + sigp30k^2)^0.5;

    totsigpx30k = (sigpxe^2 + sigpp30k^2)^0.5;

    ellipse(sigxe,sigpxe,0,0,0,myBlue);

    hold on

    ellipse(sigp30k,sigpp30k,0,0,0,myGold);

    ellipse(totsigx30k,totsigpx30k,0,0,0,'g');

    ax = gca;

    ax.XAxisLocation = 'origin';

    ax.YAxisLocation = 'origin';

    xlim([-maxSigx30k maxSigx30k]);

    ylim([-maxSigpx30k maxSigpx30k]);

    set(gca,'xtick',[]);

    set(gca,'ytick',[]);

    

    axis square;

    axis on;

    set(gca,'fontsize',18,'linewidth',2);

    xlabel('\sigma');

    ylabel('\sigma^{\prime}');

    hold off;

    

    frame = getframe(gcf);

    writeVideo(vid,frame);

    delete(findall(gcf,'type','annotation'));

    

    hold off;

end

 

% Output the movie as an mpg file

close(vid);

% Create video showing how total emittance changes according to beta

% function and optimal matching of electron to photon emittance

 

clear; close all;

 

vid = VideoWriter('emittanceChanges.mp4','MPEG-4');

vid.FrameRate = 30;    % Default 30

vid.Quality = 100;    % Default 75

open(vid);

figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');

set(0,'defaultfigurecolor',[1 1 1]); % White background

 

myBlue = [0.4 0.44 0.73];

myGold = [0.7 0.6 0];

 

pos1 = [0.1 0.2 0.6 0.6];

pos2 = [0.74 0.65 0.25 0.25];

pos3 = [0.74 0.375 0.25 0.25];

pos4 = [0.74 0.1 0.25 0.25];

 

%epsxe = 150e-12; % Horizontal electron emittance 150 pm.rad for DLSR

epsxe = 5500e-12; % Horizontal electron emittance 5.5 nm.rad for 3rd-gen

undLength = 2; % Length of undulator in m

 

photonE = (0.1:0.02:100); % Range of photon energies in keV

lambda = (1e-10*12.3984)./photonE; % Convert photon energy in keV to wavelength in m

 

index300 = round(1 + (0.3-0.1)/0.02); % Index for 300 eV

index3k = round(1 + (3-0.1)/0.02); % Index for 3 keV

index30k = round(1 + (30-0.1)/0.02); % Index for 30 keV

 

part1 = [0.01:0.005:0.25]; % Lots happening for DLSRs in this regime of L/2pi

part2 = [0.26:0.02:1.0]; % Increasing dull...

part3 = [1.05:0.05:5]; % Zzzzzzz

 

 

for ii = [part1 part2 part3]

    subplot('Position',pos1); % Main plot

    

    sigxp = (1/(4*pi))*(lambda.*undLength).^0.5; % Photon beam source size in m

    sigpxp = (lambda./(undLength)).^0.5; % Photon beam divergence in rad

    sigxe = (epsxe*ii)^0.5; % Range of sigma_x^e (horiz e-beam SD width) in m

    sigpxe = epsxe/sigxe; % Value of divergence of e-beam in rad for present value of sigxe

    totsigx = (sigxe^2 + sigxp.^2).^0.5; % Convoluted total source size

    totsigpx = (sigpxe^2 + sigpxp.^2).^0.5; % Convoluted total source divergence

    totEmitt = 1e12.*totsigx.*totsigpx; % Total emittance for present value of sigxe in pm.rad

    

    loglog(0.1,50,5,7000,photonE,totEmitt,'color',myBlue, 'LineWidth', 3.5);

    hold on

    % Markers for 3 exemplary photon energies

    plot(photonE(index300),totEmitt(index300),'ro','MarkerFaceColor','red','MarkerSize',10);

    plot(photonE(index3k),totEmitt(index3k),'ro','MarkerFaceColor','red','MarkerSize',10);

    plot(photonE(index30k),totEmitt(index30k),'ro','MarkerFaceColor','red','MarkerSize',10);

    

    str1 = '{\beta_e} = ';

    str2 = num2str(ii,'% 7.3f');

    str3 = ' m';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.59 0.71 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{\sigma_x^e} = ';

    str2 = num2str(sigxe*1e6,'% 7.2f');

    str3 = ' {\mu}m';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.59 0.67 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{\sigma_x^{\prime e}} = ';

    str2 = num2str(sigpxe*1e6,'% 7.3f');

    str3 = ' {\mu}rad';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.59 0.63 0.14 0.07],'String',strTot,'EdgeColor'...

        ,'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{\epsilon_x^e} = ';

    str2 = num2str(epsxe*1e12,'% 7.1f');

    str3 = ' pm.rad';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.47 0.71 0.1 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{L_u} = ';

    str2 = num2str(undLength,'% 7.1f');

    str3 = ' m';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.47 0.67 0.1 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    set(gca,'FontName','Helvetica','fontsize',18);

    set(gca,'TickLength',[0.016, 2]);

    set(gca,'xscale','log');

    set(gca,'yscale','log');

    set(gca,'linewidth',2);

    

    xlim([0.1 100]);

    %ylim([100 3400]); % For DLSR

    ylim([5000 15000]); % For 3rd gen.

    xlabel('Photon energy [keV]');

    ylabel('Total horizontal emittance [pm.rad]');

    hold off

    

    % 300 eV phase-space plot

    subplot('Position',pos2);

    newplot

    annotation('textbox',[0.77 0.88 0.1 0.1],'String','electron','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom','Color',myBlue);

    annotation('textbox',[0.77 0.855 0.1 0.1],'String','photon','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom','Color',myGold);

    annotation('textbox',[0.77 0.83 0.1 0.1],'String','total','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom','Color','g');

    

    annotation('textbox',[0.9 0.88 0.1 0.1],'String','300 eV','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    set(gca,'xtick',[]); % No tick marks

    set(gca,'ytick',[]);

    

    sigp300 = (1e-10*(12.3984/0.3)*undLength)^0.5/(4*pi); % sigma_p @ 300 eV

    sigpp300 = (1e-10*(12.3984/0.3)/undLength)^0.5; % sigma_p' @ 300 eV

    maxSigx300 = 1.1*((max(part3)*epsxe) + sigp300^2)^0.5; % max sigma_p @ 300 eV

    maxSigpx300 = 1.1*((epsxe/min(part1)) + sigpp300^2)^0.5; % max sigma_p' @ 300 eV

    

    totsigx300 = (sigxe^2 + sigp300^2)^0.5; % Convoluted total source size @ 300 eV

    totsigpx300 = (sigpxe^2 + sigpp300^2)^0.5; % Convoluted total source divergence @ 300 eV

    

    ellipse(sigxe,sigpxe,0,0,0,myBlue); % electron-emittance phase-space ellipse

    hold on

    ellipse(sigp300,sigpp300,0,0,0,myGold); % photon-emittance phase-space ellipse

    ellipse(totsigx300,totsigpx300,0,0,0,'g'); % total-emittance phase-space ellipse

    ax = gca;

    ax.XAxisLocation = 'origin'; % Plot with axes through origin

    ax.YAxisLocation = 'origin';

    xlim([-maxSigx300 maxSigx300]); % Set axes limits

    ylim([-maxSigpx300 maxSigpx300]);

    

    axis square;

    axis on;

    set(gca,'fontsize',18,'linewidth',2);

    xlabel('\sigma');

    ylabel('\sigma^{\prime}');

    

    hold off;

    

    % 3 keV phase-space plot

    subplot('Position',pos3);

    newplot

    annotation('textbox',[0.9 0.605 0.1 0.1],'String','3 keV','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    sigp3k = (1e-10*(12.3984/3)*undLength)^0.5/(4*pi);

    sigpp3k = (1e-10*(12.3984/3)/undLength)^0.5;

    maxSigx3k = 1.1*((max(part3)*epsxe) + sigp3k^2)^0.5;

    maxSigpx3k = 1.1*((epsxe/min(part1)) + sigpp3k^2)^0.5;

    

    totsigx3k = (sigxe^2 + sigp3k^2)^0.5;

    totsigpx3k = (sigpxe^2 + sigpp3k^2)^0.5;

    ellipse(sigxe,sigpxe,0,0,0,myBlue);

    hold on

    ellipse(sigp3k,sigpp3k,0,0,0,myGold);

    ellipse(totsigx3k,totsigpx3k,0,0,0,'g');

    ax = gca;

    ax.XAxisLocation = 'origin';

    ax.YAxisLocation = 'origin';

    xlim([-maxSigx3k maxSigx3k]);

    ylim([-maxSigpx3k maxSigpx3k]);

    set(gca,'xtick',[]);

    set(gca,'ytick',[]);

    

    axis square;

    axis on;

    set(gca,'fontsize',18,'linewidth',2);

    xlabel('\sigma');

    ylabel('\sigma^{\prime}');

    hold off;

    

    % 30 keV phase-space plot

    subplot('Position',pos4);

    newplot

    annotation('textbox',[0.9 0.33 0.1 0.1],'String','30 keV','EdgeColor',...

        'none','FontSize',18,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    sigp30k = (1e-10*(12.3984/30)*undLength)^0.5/(4*pi);

    sigpp30k = (1e-10*(12.3984/30)/undLength)^0.5;

    maxSigx30k = 1.1*((max(part3)*epsxe) + sigp30k^2)^0.5;

    maxSigpx30k = 1.1*((epsxe/min(part1)) + sigpp30k^2)^0.5;

    

    totsigx30k = (sigxe^2 + sigp30k^2)^0.5;

    totsigpx30k = (sigpxe^2 + sigpp30k^2)^0.5;

    ellipse(sigxe,sigpxe,0,0,0,myBlue);

    hold on

    ellipse(sigp30k,sigpp30k,0,0,0,myGold);

    ellipse(totsigx30k,totsigpx30k,0,0,0,'g');

    ax = gca;

    ax.XAxisLocation = 'origin';

    ax.YAxisLocation = 'origin';

    xlim([-maxSigx30k maxSigx30k]);

    ylim([-maxSigpx30k maxSigpx30k]);

    set(gca,'xtick',[]);

    set(gca,'ytick',[]);

    

    axis square;

    axis on;

    set(gca,'fontsize',18,'linewidth',2);

    xlabel('\sigma');

    ylabel('\sigma^{\prime}');

    hold off;

    

    frame = getframe(gcf);

    writeVideo(vid,frame);

    delete(findall(gcf,'type','annotation'));

    

    

end

% Output the movie as an mp4 file

close(vid);

% Program to plot the coherent fraction as a function of photon energy and

% electron emittance, assuming perfectly matched beta functions between

% photons and electrons

 

clear; close all;

 

vid = VideoWriter('cohFracChanges3.mp4','MPEG-4');

vid.FrameRate = 30;    % Default 30

vid.Quality = 100;    % Default 75

open(vid);

figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');

set(0,'defaultfigurecolor',[1 1 1]); % White background

 

myBlue = [0.4 0.44 0.73];

 

sigye = 2e-7;    % Vertical electron size 2 um

epsye = 1e-14;

sigpye = epsye/sigye;

undLength = 2; % Length of undulator in m

betax = undLength/(4*pi); % Ideal beta function of electron beam to match photon beta function

photonE = (0.01:0.0025:100); % Range of photon energies in keV

lambda = (1e-10*12.3984)./photonE; % Convert photon energy in keV to wavelength in m

 

epsxerange = (5500:-10:50);

for ii = [epsxerange]

    sigp = (1/(4*pi)).*(lambda.*undLength).^0.5; % Photon beam source size in m

    sigpp = (lambda./(undLength)).^0.5; % Photon beam divergence in rad

    epsxe = ii*1e-12; % Range of horizontal electron emittances

    sigxe = (betax*epsxe)^0.5; % Range of sigma_x^e (horiz e-beam SD width) in m

    sigpxe = epsxe/sigxe; % Value of divergence of e-beam in rad for present value of sigxe

    pt1 = 1 + (sigxe./sigp).^2;

    pt2 = 1 + (sigpxe./sigpp).^2;

    pt3 = 1 + (sigye./sigp).^2;

    pt4 = 1 + (sigpye./sigpp).^2;

    fcoh = (pt1 .* pt2 .* pt3 .* pt4).^(-0.5);

    diffLimEnergy = 12.3984/(4*pi*epsxe*1e10); % Diffraction-limited energy

    diffLimI = round((diffLimEnergy - 0.1)/0.0025); % Index of diffraction-limited energy in photonE

    

    if (ii == max(epsxerange))

        fcohStart = fcoh;

    end

    loglog(0.01,1e-5,100,2,photonE,fcoh,'color',myBlue, 'LineWidth', 3.5);

    hold on;

    loglog(0.01,1e-5,100,2,photonE,fcohStart,'color','red', 'LineWidth', 3.5);

    %     if (diffLimI >= 1)

    %         plot(photonE(1,diffLimI),fcoh(1,diffLimI),'ko',...

    %             'MarkerFaceColor',myBlue,'MarkerSize',10);

    %     end

    

    str1 = '{\epsilon_x^e} = ';

    str2 = num2str(epsxe*1e12,'% 7.1f');

    str3 = ' pm.rad';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.16 0.4 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',22,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{\sigma_x^e} = ';

    str2 = num2str(sigxe*1e6,'% 7.1f');

    str3 = ' {\mu}m';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.16 0.35 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',22,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{\sigma_x^{\prime e}} = ';

    str2 = num2str(sigpxe*1e6,'% 7.3f');

    str3 = ' {\mu}rad';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.16 0.30 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',22,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    str1 = '{L_u} = ';

    str2 = num2str(undLength,'% 7.1f');

    str3 = ' m';

    strTot = [str1,str2,str3];

    annotation('textbox',[0.16 0.25 0.14 0.07],'String',strTot,'EdgeColor',...

        'none','FontSize',22,'FitBoxToText','on','HorizontalAlignment',...

        'left','verticalAlignment','bottom');

    

    set(gca,'FontName','Helvetica','fontsize',22);

    set(gca,'TickLength',[0.016, 2]);

    set(gca,'xscale','log');

    set(gca,'yscale','log');

    set(gca,'linewidth',2);

    xlim([0.01 100]);

    ylim([1e-4 2]);

    xlabel('Photon energy [keV]');

    ylabel('Coherent fraction');

    

    frame = getframe(gcf);

    writeVideo(vid,frame);

    hold off;

    

    delete(findall(gcf,'type','annotation'));

    

end

% Output the movie as an avi file

close(vid);