%Prepared by R.A.Garaev for UHF labs
n=288;
u0min=400; u0max=2000; u0=1000;
u1min=10; u1max=500;  u1=50;
Fmin=1.0e+9; Fmax=3.0e+9;Fr=2.0e+9; omega=2*pi*Fr;
DrifDmin=0.01; DrifDmax=0.30; DrifD=0.20;
teta=pi/2;
uRfmin=1000; uRfmax=3000;  uRf=2000;
sRfmin=0.005; sRfmax=0.9; sRf=0.25;
aRf=((uRf+u0) / sRf) * 1.6e-19 / 9.1e-31;
ns=1; nf=ns+172;

phi= 0.01: pi/12 : 20*pi;

% --------------------------------
windG1=figure;  figure(windG1);  %Там графики
set(windG1,'Color',[0 0 1]);
traject(u0, u1, teta, omega, n, ns, nf, aRf);

% --------------------------------
Change_button = uicontrol('Style','Pushbutton','Units','normalized',...
        'Position',[.91 .01 .1 .07],...
        'Callback','figure(windM1)','String','Меню'); %Кнопка выхода в меню%

% --------------------------------
windM1=figure;  %Там меню с органами управления 
figure(windM1);  % Окно с будущим меню на передний план

ctl_ini;  %Инициализация 
captionM = 'Klistron simulation';

% --------------------------------
Ctrl1='on';  captnPar1 ='U0';
popup1Val =1; popup1Opts='группировка|ток I2|гармоники'; 
Slider1Min=u0min; Slider1Max=u0max; Slider1Val=u0;

% --------------------------------
Ctrl2='on'; captnPar2 ='U1-voltage';
Slider2Min=u1min; Slider2Max=u1max; Slider2Val=u1;

% --------------------------------
Ctrl3='on'; captnPar3 ='U reflect';
Slider3Min=uRfmin; Slider3Max=uRfmax; Slider3Val=uRf;

% --------------------------------
Ctrl4='on'; captnPar4 ='ReflDistance';
Slider4Min=sRfmin; Slider4Max=sRfmax; Slider4Val=sRf;

% --------------------------------
Ctrl5='on';  captnPar5 ='Frequency';
Slider5Min=Fmin; Slider5Max=Fmax; Slider5Val=Fr;

% --------------------------------
Ctrl6='on';  captnPar6 ='DriftDistance';
Slider6Min=DrifDmin; Slider6Max=DrifDmax; Slider6Val=DrifD;

% --------------------------------
%Ctrl7='on';  captnPar7 ='U0';
%Slider7Min=u0min; Slider7Max=u0max; Slider7Val=u0;

% --------------------------------
%Ctrl8='on';  captnPar8 ='U0';
%Slider8Min=u0min; Slider8Max=u0max; Slider8Val=u0;

% --------------------------------
UActionCheck=['uo= Edit1Val; u1= Edit2Val;'...
              'uRf= Edit3Val; sRf= Edit4Val;'...
	      'Fr= Edit5Val; DrifD=Edit6Val;'...
	      'figure(windG1);'...
	      'if  popup1Val==1,'... 
              'aRf=((uo+uRf) / sRf) * 1.6e-19 / 9.1e-31;'...
              'traject(u0, u1, teta, 2*pi*Fr, n, ns, nf, aRf);'...
              'end;'...
	      'if  popup1Val~=1,'... 
	      've=sqrt(2.*1.6e-19 .*u0  ./9.1e-31);'... 
              'X=2*pi*Fr*DrifD*sin(teta/2)/(teta/2)*u1/2/u0/ve;'...
              'I2=abs(1./(1-X * cos(phi))); hold off;'...
              'end;'...
	      'if  popup1Val==2,'... 
              'plot(phi(1:128),I2(1:128),''g-'');hold on;grid on;'...
              'xlabel(sprintf(''X=%f'',X));'...
              'end;'...
	      'if  popup1Val==3,'... 
	      'Y=fft(I2,480);  Pyy=Y.*conj(Y)/480;'...
              'f=Fr*24/480*(0:240);'...
              'plot(f(10:120),Pyy(10:120),''g-''), grid on;'...
              'end;'...
	     ];	

% =================================
ctlgra00;  % В окно - меню органов управления