function mu_usd;
%Animation of electron trajectory in a semiconductor crystal,
%illustrating the concept of mobility.
%(Section 1.4.1 in Understanding Semiconductor Devices)

%   Sima Dimitrijev, September 27, 1999.
%   Copyright (c) 2000, Oxford University Press.


global con_m_usd5; global con_t_usd5; global con_d_usd5; global con_start_usd5; 
global con_stop_usd5; global con_close_usd5; global con_e_usd5; global con_go_usd5;
global con_r_usd5;
con_go_usd5=0;

%OPEN PARENT FIGURE
Hf=figure('NumberTitle','off','Name','Sections 1.4.2/1.4.3: Mobility',...
   'Color','k');
set(gcf,'InvertHardCopy','off');

Ha0=axes('Xlim',[0 10],'Ylim',[0 10],'Visible','off','Position',[0 0 1 1]);
Hp_bckgr=patch([0 10 10 0],[0 0 10 10],[0.8 0.8 0.8]); 
%The Figure color is black to produce a black trace (trajectory). 
%It is covered by  Hp_bckgr to convert it into normal color otherwise

%READ THE INTRO TEXT              
introtext ={'UNDERSTANDING SEMICONDUCTOR DEVICES ';...
      'Section 1.4.2: Mobility';...
      'Section 1.4.3: Dependence of Mobility on Temperature and Doping Concentration' ;...
      ' ';...
      'Mobility is related to the drift velocity (the animation associated with ';...
      'Section 1.4.1) and its dependence on temperature and doping  ';...
      'concentration is illustrated. '};
Ht_intro=text(0.1,0.6,char(introtext),'Units','normalized');
Hp_cont=uicontrol(Hf,'Units','normalized','Position',[0.1 0.2 0.7 0.1],...
   'Style','pushbutton','String','CLICK HERE TO CONTINUE!','CallBack','con_go_usd5=1;');
con_go_usd5=0;
while con_go_usd5<1; %Read the intro text
   if ishandle(Hf)==0;
         return; 
   end
   pause(0.2);
end
con_go_usd5=0;
if ishandle(Hf)==0;
  return; 
end
set(Ht_intro,'Visible','off');
set(Hp_cont,'Visible','off','Enable','off');
drawnow;

con_m_usd5=0; con_t_usd5=0; con_d_usd5=0; con_start_usd5=0; 
con_stop_usd5=0; con_close_usd5=0; con_e_usd5=0; con_r_usd5=0; idop=0;
%SETTING THE CONTROL PANEL              
Hframe=uicontrol(Hf,'Units','normalized','Position',[0.77 0.01 0.22 0.98],...
   'Style','frame');

%Electric field
Ht_efu=uicontrol(Hf,'Units','normalized','Position',[0.78 0.90 0.20 0.05],...
   'Style','text','String','ELECTRIC FIELD','BackgroundColor',[.7 0 0],...
   'ForegroundColor','w');
Hsl_ef=uicontrol(Hf,'Units','normalized','Position',[0.78 0.85 0.20 0.05],...
   'Style','slider','min',0,'max',0.8,'Value',0,'CallBack','con_e_usd5=1;');
Ht_efd=uicontrol(Hf,'Units','normalized','Position',[0.78 0.80 0.20 0.05],...
   'Style','text','String',[num2str(round(10*(get(Hsl_ef,'Value')))/10) ' V/um'],...
   'BackgroundColor',[0.7 0 0],'ForegroundColor','w');
Ef=round(10*(get(Hsl_ef,'Value')))/10;

%Temperature slider- NOTE: the temperature is labeled by meff in this file
Ht_mu=uicontrol(Hf,'Units','normalized','Position',[0.78 0.73 0.20 0.03],...
   'Style','text','String','TEMPERATURE');
Hsl_m=uicontrol(Hf,'Units','normalized','Position',[0.78 0.68 0.20 0.05],...
   'Style','slider','min',0.2,'max',1.0,'Value',0.5,'CallBack','con_m_usd5=1;');
Ht_md=uicontrol(Hf,'Units','normalized','Position',[0.78 0.63 0.20 0.05],...
   'Style','text','String',[num2str(round(600*(get(Hsl_m,'Value')))) ' K']);


%Doping radio buttons
Ht_d=uicontrol(Hf,'Units','normalized','Position',[0.78 0.57 0.20 0.03],...
   'Style','text','String','DOPING CONCENTRATION');
Hr_d(1)=uicontrol(Hf,'Units','normalized','Position',[0.8 0.52 0.16 0.05],...
   'Style','radio','String','None','CallBack','con_r_usd5=1;','Value',1);
Hr_d(2)=uicontrol(Hf,'Units','normalized','Position',[0.8 0.47 0.16 0.05],...
   'Style','radio','String','Medium','CallBack','con_r_usd5=2;','Value',0);
Hr_d(3)=uicontrol(Hf,'Units','normalized','Position',[0.8 0.42 0.16 0.05],...
   'Style','radio','String','High','CallBack','con_r_usd5=3;','Value',0);


%Pushbuttons
Hb_close=uicontrol(Hf,'Units','normalized','Position',[0.78 0.02 0.20 0.07],...
   'Style','pushbutton','String','CLOSE','CallBack','con_close_usd5=1;');
Hb_stop=uicontrol(Hf,'Units','normalized','Position',[0.78 0.11 0.20 0.07],...
   'Style','pushbutton','String','STOP','CallBack','con_stop_usd5=1;','Enable','off');
Hb_start=uicontrol(Hf,'Units','normalized','Position',[0.78 0.20 0.20 0.07],...
   'Style','pushbutton','String','START','CallBack','con_start_usd5=1;');

%CONSTANTS
meff=0.5; %Change in the temperature reduces the step size, thus speed (later)
r=0.10*meff/0.5; %Change in the temperature increases the phonon radius
rdop=0.7;

Aion(1)=0; %Constant responsible for the Coulomb scattering
Aion(2)=0.3; %Medium conc.
Aion(3)=0.9; %High concen.
mu0=0.1; %mu0/Aphon should be 0.1*0.5^3/2
Aphon=mu0/(0.1*0.5^1.5); %Constant responsible for the phonon scattering
mu=mu0/(Aion(3)*meff^(-1.5)+Aphon*meff^1.5);
mu=mu0/(Aion(2)*meff^(-1.5)+Aphon*meff^1.5);
mu=mu0/(Aion(1)*meff^(-1.5)+Aphon*meff^1.5);
vsat=0.1;
alpha_e=pi/8; 
dr0=0.8; 
alpha_i=(rand-0.5)*pi/3+3*pi/4; %Initial incident angle
Xmax=24; Ymax=16;
Nst_med=3; %There is a Coulomb center every 5th node (integer) for med. conc
Nst_high=2; %There is a Coulomb center every 3rd node (integer) for med. conc
Nst_dop=0; %The actual Nst_ value

%SET THE 2-D GRAPH
Haan=axes('Xlim',[0 Xmax],'Ylim',[0 Ymax],'Visible','off',...
   'Position',[0.05 0.5 0.675 0.45]);
xe1=[-r:0.1*r:r];
ye1=sqrt(r*r-xe1.*xe1);
xe=[xe1 xe1]; %elementary phonon
ye=[ye1 -ye1]; %elementary phonon

xce1=[-rdop:0.1*rdop:rdop];
yce1=sqrt(rdop*rdop-xce1.*xce1);
xce=[xce1 xce1]; %elementary Coulomb sc.c.
yce=[yce1 -yce1]; %elementary Coulomb sc.c.


hold on;
Hp_bckgr=patch([0 Xmax Xmax 0],[0 0 Ymax Ymax],'y','EdgeColor','y','EraseMode','none');
%Set the phonon graph
for i=1:25;
for j=1:17;
   Hp(i,j)=patch((i-1+xe),(j-1+ye),[1 0.75 0],'EdgeColor',[1 0.75 0],'EraseMode','none',...
      'Visible','off');
end   
end
%Set the Coulomb center graphs
for i=1:Nst_high:25;
for j=1:Nst_high:17;
   Hpch(i,j)=patch((i-1+xce),(j-1+yce),'g','EdgeColor','g','EraseMode','none',...
      'Visible','off');
end   
end

for i=1:Nst_med:25;
for j=1:Nst_med:17;
   Hpcm(i,j)=patch((i-1+xce),(j-1+yce),'g','EdgeColor','g','EraseMode','none',...
      'Visible','off');
end   
end


%SET the electron
xel=6.6; yel=6.6;
Hp_el=plot(xel,yel,'r.','MarkerSize',10,'EraseMode','xor','Visible','off');

%SET the electron trace
xtr=xel; ytr=yel;
Hp_tr=plot(xtr,ytr,'k-','LineWidth',1,'EraseMode','none','Visible','off');

hold off;

%SET Vdr-E graph
efx=[0:0.02:0.8];
vdr=mu*efx./(1+mu*efx/vsat);
vdrq=mu*Ef./(1+mu*Ef/vsat);
if ishandle(Hf)==0;
   return; 
end
Havdr=axes('Xlim',[0 0.8],'Ylim',[0 0.08],'Position',[0.1 0.1 0.15 0.3],...
   'FontAngle','italic');
hold on;
Hp_vdr=plot(efx,vdr,'b-','LineWidth',2);
xlabel('E   (V/\mu m)'); ylabel('v_{d}=\mu_{ n} E   (\mu m/ps)');
set(Havdr,'Color',[0.85 0.85 0.85]);
Hp_vdrq=plot(Ef,vdrq,'r.','MarkerSize',15);
hold off;

%SET \mu_{n} - T graphs
temp=[0.2:0.02:1];
muplot=1e4*mu0./(Aion(1)*temp.^(-1.5)+Aphon*temp.^1.5);
if ishandle(Hf)==0;
   return; 
end
Hamut=axes('Xlim',[100 600],'Ylim',[100 1e4],'Position',[0.35 0.1 0.4 0.3],...
   'YScale','log','FontAngle','italic');
xlabel('TEMPERATURE  (K)'); ylabel('\mu_{ n}  (cm^{2}/Vs)');
set(Hamut,'Color',[0.85 0.85 0.85]);
hold on;
Hp_mut(1)=plot(temp*600,muplot,'c-','LineWidth',2);

muplot=1e4*mu0./(Aion(2)*temp.^(-1.5)+Aphon*temp.^1.5);
Hp_mut(2)=plot(temp*600,muplot,'y-','LineWidth',2);
set(Hp_mut(2),'Color',[0.6 0.6 0.6]);

muplot=1e4*mu0./(Aion(3)*temp.^(-1.5)+Aphon*temp.^1.5);
Hp_mut(3)=plot(temp*600,muplot,'y-','LineWidth',2);
set(Hp_mut(3),'Color',[0.6 0.6 0.6]);

muplotq=1e4*mu0/(Aion(1)*meff^(-1.5)+Aphon*meff^1.5);

Hp_mutq=plot(meff*600,muplotq,'b.','MarkerSize',15);

hold off;
%The Equation text
Ht_f=text(300,6e3,'\mu_{n}=|v_{d} | /E','Color','k','FontSize',12,...
       'FontAngle','italic');

for i=1:25;
   for j=1:17;
      if ishandle(Hf)==0;
         return; 
      end
   set(Hp(i,j),'Visible','on');
end   
end
%***********************************
while con_close_usd5<1 %MAIN LOOP
   
%SET THE SLIDER VALUES and RENEW 
  
if con_e_usd5>0; %ELECTRIC FIELD
   if ishandle(Hf)==0;
      return; 
   end
set(Ht_efd,'String',[num2str(round(10*(get(Hsl_ef,'Value')))/10) ' V/um']);
Ef=round(10*(get(Hsl_ef,'Value')))/10;
vdrq=mu*Ef./(1+mu*Ef/vsat);
set(Hp_vdrq,'XData',Ef,'YData',vdrq);
end
con_e_usd5=0; 

if con_m_usd5>0; %TEMPERATURE
if ishandle(Hf)==0;
   return; 
end
set(Ht_md,'String',[num2str(round(600*(get(Hsl_m,'Value')))) ' K']);
   
%Set vdr-E graph   
   for id=1:3;
      if get(Hr_d(id),'Value')==1; idop=id; end
   end   
meff=get(Hsl_m,'Value');   
mu=mu0/(Aion(idop)*meff^(-1.5)+Aphon*meff^1.5);
vdr=mu*efx./(1+mu*efx/vsat);
vdrq=mu*Ef./(1+mu*Ef/vsat);
if ishandle(Hf)==0;
   return; 
end
set(Hp_vdr,'YData',vdr);
set(Hp_vdrq,'XData',Ef,'YData',vdrq);

%Set the dot on \mu_{n}-T graph
muplotq=1e4*mu0/(Aion(idop)*meff^(-1.5)+Aphon*meff^1.5);
set(Hp_mutq,'XData',600*meff,'YData',muplotq);

%Set the phonon size
r=0.10*meff/0.5; %Change in the temperature increases the phonon radius
xe1=[-r:0.1*r:r];
ye1=sqrt(r*r-xe1.*xe1);
xe=[xe1 xe1]; %elementary phonon
ye=[ye1 -ye1]; %elementary phonon
set(Hp_bckgr,'XData',[0 Xmax Xmax 0],'YData',[0 0 Ymax Ymax]);
for i=1:25;
   for j=1:17;
      if ishandle(Hf)==0;
         return; 
      end
set(Hp(i,j),'XData',(i-1+xe),'YData',(j-1+ye));
end   
end
if Nst_dop>0;
   if Nst_dop==Nst_med;
      for i=1:Nst_dop:25;
      for j=1:Nst_dop:17;
            if ishandle(Hf)==0;
               return; 
            end
      set(Hpcm(i,j),'XData',(i-1+xce),'YData',(j-1+yce),'Visible','on');
      set(Hpch,'Visible','off');   
      end   
      end
   else
      for i=1:Nst_dop:25;
      for j=1:Nst_dop:17;
            if ishandle(Hf)==0;
               return; 
            end
      set(Hpch(i,j),'XData',(i-1+xce),'YData',(j-1+yce),'Visible','on');
      set(Hpcm,'Visible','off');   
      end   
      end
   end   
 end;
con_m_usd5=0;
end;
if ishandle(Hf)==0;
   return; 
end
set([Hp_el Hp_tr],'Visible','off');

if con_r_usd5>0; %DOPING CONCENTRATION
   idop=con_r_usd5;
   if ishandle(Hf)==0;
      return; 
   end
   set(Hr_d,'Value',0);
   set(Hr_d(idop),'Value',1);  
   
   set([Hp_el Hp_tr],'Visible','off');
   set(Hp_bckgr,'Xdata',[0 Xmax Xmax 0]); %To renew the background
   for i=1:25;
   for j=1:17;
         if ishandle(Hf)==0;
            return; 
         end
   set(Hp(i,j),'XData',(i-1+xe),'YData',(j-1+ye));
   end   
   end
   
   %Set vd-E graph
   meff=get(Hsl_m,'Value');   
   mu=mu0/(Aion(idop)*meff^(-1.5)+Aphon*meff^1.5);
   vdr=mu*efx./(1+mu*efx/vsat);
   vdrq=mu*Ef./(1+mu*Ef/vsat);
   if ishandle(Hf)==0;
      return; 
   end
   set(Hp_vdr,'YData',vdr);
   set(Hp_vdrq,'XData',Ef,'YData',vdrq);

   %Set mu_{n}-T graphs
   set(Hp_mut,'Color',[0.6 0.6 0.6]);
   set(Hp_mut(idop),'Color','c');
   muplotq=1e4*mu0/(Aion(idop)*meff^(-1.5)+Aphon*meff^1.5);
   set(Hp_mutq,'XData',600*meff,'YData',muplotq);
   
   switch idop;
   case 1;
      Nst_dop=0;
      if ishandle(Hf)==0;
         return; 
      end
      set(Hpcm,'Visible','off');
      set(Hpch,'Visible','off');
      set(Hp,'Visible','on');
   case 2;
      Nst_dop=Nst_med;
      if ishandle(Hf)==0;
         return; 
      end
      set(Hp,'Visible','on');
      set(Hpch,'Visible','off');
      %Set the Coulomb center graph
      for i=1:Nst_dop:25;
      for j=1:Nst_dop:17;
            if ishandle(Hf)==0;
               return; 
            end
      set(Hpcm(i,j),'XData',(i-1+xce),'YData',(j-1+yce),'Visible','on');
      end   
      end
   case 3;   
      Nst_dop=Nst_high;
      if ishandle(Hf)==0;
         return; 
      end
      set(Hp,'Visible','on');
      set(Hpcm,'Visible','off');
      %Set the Coulomb center graph
      for i=1:Nst_dop:25;
      for j=1:Nst_dop:17;
            if ishandle(Hf)==0;
               return; 
            end
      set(Hpch(i,j),'XData',(i-1+xce),'YData',(j-1+yce),'Visible','on');
      end   
      end
   end;   

con_r_usd5=0;
end   

if ishandle(Hf)==0;
   return; 
end
pause(1);
if ishandle(Hf)==0;
   return; 
end

if con_start_usd5>0;
   con_start_usd5=0;
   %Renew the background
   if ishandle(Hf)==0;
      return; 
   end
   set([Hp_el Hp_tr],'Visible','off');
   set(Hp_bckgr,'Xdata',[0 Xmax Xmax 0]); %To renew the background
   for i=1:25; % To renew the scattering centers
   for j=1:17;
         if ishandle(Hf)==0;
            return; 
         end
     set(Hp(i,j),'Xdata',(i-1+xe));
   end   
   end
   
if Nst_dop>0;
   if Nst_dop==Nst_med;
      for i=1:Nst_dop:25;
      for j=1:Nst_dop:17;
            if ishandle(Hf)==0;
               return; 
            end
      set(Hpcm(i,j),'XData',(i-1+xce),'YData',(j-1+yce),'Visible','on');
      set(Hpch,'Visible','off');   
      end   
      end
   else
      for i=1:Nst_dop:25;
      for j=1:Nst_dop:17;
            if ishandle(Hf)==0;
               return; 
            end
      set(Hpch(i,j),'XData',(i-1+xce),'YData',(j-1+yce),'Visible','on');
      set(Hpcm,'Visible','off');   
      end   
      end
   end   
 end;
   
   % renew the electron position and starting angle
   dir=1; xel=6.6; yel=6.6; xtr=xel; ytr=yel;
   alpha_i=(rand-0.5)*pi/3+3*pi/4; beta=alpha_i;%Initial incident angle
   
   %set the buttons
   if ishandle(Hf)==0;
      return; 
   end
   set([Hb_start Hb_close Hsl_ef Hsl_m],'Enable','off');
   set(Hr_d,'Enable','off');
   set(Hb_stop,'Enable','on');
   set(Hp_el,'Xdata',xel,'Ydata',yel','Visible','on');
   set(Hp_tr,'Xdata',[xel xtr],'Ydata',[yel ytr],'Visible','on');
   
nst=0;   
dr=dr0*meff^2; 
accel_y=0;
%THE ANIMATION LOOP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
while con_stop_usd5<1;
   
csec=0;   
nst=nst+1; %To accelerate the particle
   
%remember the old position, and project the new position
xel_old=xel; yel_old=yel;
dx=dir*dr*cos(alpha_i); 
dy=dir*dr*sin(alpha_i);
accel=0.2*dr*(Ef)^0.25;
xel=xel+dx+accel*nst; 
yel=yel+dy+accel_y*nst;  

%COULOMB SCATTERING FIRST

if Nst_dop>0; %if not, there cannot be Coulomb scattering
%find the step size
   stp=sqrt((xel-xel_old)^2+(yel-yel_old)^2);
   
   if stp>=rdop; %Need to cut the step into substeps
      beta=atan((yel-yel_old)/(xel-xel_old));
      alpha_i;
      dxels=0.9*sign(xel-xel_old)*rdop*abs(cos(beta));
      dyels=0.9*sign(yel-yel_old)*rdop*abs(sin(beta));
   else
      dxels=xel-xel_old; dyels=yel-yel_old;
   end   
        
   xels=xel_old; yels=yel_old; csec=0; stps=0;
   while ((stps<stp)*(csec==0));
      xels=xels+dxels; yels=yels+dyels;   
      stps=sqrt((xels-xel_old)^2+(yels-yel_old)^2);
      if stps>stp;
         xels=xel; yels=yel;
         stps=stp;
      end   
      
   %Check if the electron is inside a circle
   xdist=xels-Nst_dop*round(xels/Nst_dop); ydist=yels-Nst_dop*round(yels/Nst_dop);
   dist=sqrt(xdist^2+ydist^2);
   if (dist<=rdop); %Check if the electron is inside a circle
      csec=1; %There has been a cross section
   %put it back at the cross-section with the circle
   mm=tan(beta);
   aa=1+mm*mm;
   bb=2*mm*(ydist-mm*xdist);
   cc=mm*mm*xdist*xdist-2*mm*xdist*ydist+ydist*ydist-rdop*rdop;
   sq=sqrt(bb*bb-4*aa*cc);
   x1=(-bb+sq)/(2*aa); %x1 and x2 are the two cross-secting x co-ord.
   y1=mm*(x1-xdist)+ydist;
   x2=(-bb-sq)/(2*aa);
   y2=mm*(x2-xdist)+ydist;
   x1o=x1+Nst_dop*round(xels/Nst_dop);
   y1o=y1+Nst_dop*round(yels/Nst_dop);
   x2o=x2+Nst_dop*round(xels/Nst_dop);
   y2o=y2+Nst_dop*round(yels/Nst_dop);
   if ((x1o-xel_old)^2+(y1o-yel_old)^2)<((x2o-xel_old)^2+(y2o-yel_old)^2);
      xels=x1o; yels=y1o;
      xdist=x1; ydist=y1;
   else
      xels=x2o; yels=y2o;
      xdist=x2; ydist=y2;
   end
   %ydist_new=mm*(xdist_new-xdist)+ydist;
   if csec==1;   
   accel_y=(2*rand-1)*accel*0.5;
   nst=0;
   dir=-dir; 
   alpha_i=2*atan(ydist/xdist)-alpha_i;
   beta=alpha_i;
   end
   end   
   end % while
   xel=xels;
   yel=yels;
end; %if Nst_dop>0


%NOW PHONON SCATTERING, IF NOT DONE ALREADY
if csec==0; %If not, there has been a Coulomb scattering already
   %find the step size
   stp=sqrt((xel-xel_old)^2+(yel-yel_old)^2);
   
   if stp>=r %Need to cut the step into substeps
      beta=atan((yel-yel_old)/(xel-xel_old));
      alpha_i;
      dxels=0.9*sign(xel-xel_old)*r*abs(cos(beta));
      dyels=0.9*sign(yel-yel_old)*r*abs(sin(beta));
   else
      dxels=xel-xel_old; dyels=yel-yel_old;
   end   
        
   xels=xel_old; yels=yel_old; csec=0; stps=0;
   while ((stps<stp)*(csec==0));
      xels=xels+dxels; yels=yels+dyels;   
      stps=sqrt((xels-xel_old)^2+(yels-yel_old)^2);
      if stps>stp;
         xels=xel; yels=yel;
         stps=stp;
      end   
      
   %Check if the electron is inside a circle
   xdist=xels-round(xels); ydist=yels-round(yels);
   dist=sqrt(xdist^2+ydist^2);
   if (dist<=r); %Check if the electron is inside a circle
      csec=1; %There has been a cross section
   %put it back at the cross-section with the circle
   mm=tan(beta);
   aa=1+mm*mm;
   bb=2*mm*(ydist-mm*xdist);
   cc=mm*mm*xdist*xdist-2*mm*xdist*ydist+ydist*ydist-r*r;
   sq=sqrt(bb*bb-4*aa*cc);
   x1=(-bb+sq)/(2*aa); %x1 and x2 are the two cross-secting x co-ord.
   y1=mm*(x1-xdist)+ydist;
   x2=(-bb-sq)/(2*aa);
   y2=mm*(x2-xdist)+ydist;
   x1o=x1+round(xels);
   y1o=y1+round(yels);
   x2o=x2+round(xels);
   y2o=y2+round(yels);
   if ((x1o-xel_old)^2+(y1o-yel_old)^2)<((x2o-xel_old)^2+(y2o-yel_old)^2);
      xels=x1o; yels=y1o;
      xdist=x1; ydist=y1;
   else
      xels=x2o; yels=y2o;
      xdist=x2; ydist=y2;
   end
   %ydist_new=mm*(xdist_new-xdist)+ydist;
   if csec==1;   
   accel_y=(2*rand-1)*accel*0.5;
   nst=0;
   dir=-dir; 
   alpha_i=2*atan(ydist/xdist)-alpha_i;
   beta=alpha_i;
   end
   end   
   end % while
   xel=xels;
   yel=yels;
end; % if csec==0;   
   
if xel>Xmax; 
   if Ef==0;
   alpha_i=pi-alpha_i;
   xel=Xmax;
   else
   con_stop_usd5=1;
   end   
end   
if (xel<0);
   alpha_i=pi-alpha_i;
   xel=0;
end
if yel>Ymax;
   alpha_i=pi-alpha_i;
   dir=-dir;
   yel=Ymax;
end
if yel<0;
   alpha_i=pi-alpha_i;
   dir=-dir;
   yel=0;
end
if ishandle(Hf)==0;
   return; 
end
set(Hp_el,'Xdata',xel,'YData',yel);
set(Hp_tr,'Xdata',[xel xtr],'Ydata',[yel ytr]);
xtr=xel; ytr=yel;

tic;
while toc<0.05;
   pause(0.01);
end   
   
end; end % if/while con_stop_usd5<1

con_stop_usd5=0;
if ishandle(Hf)==0;
   return; 
end
set([Hb_start Hb_close Hsl_ef Hsl_m],'Enable','on');
set(Hr_d,'Enable','on');
set(Hb_stop,'Enable','off');

end; % while of main loop (con_close_usd5=1)
close
