function GUI_slider

clc
clear
s=tf('s');
t = 0:0.1:20;
r = ones(1,length(t)); 
l = 0.1*ones(size(t)).*(t>10);
e = 0*randn(size(t));
t2=0.1;
G1 = 1/(1+s*t2);
G2 = 10/s^2;
Gp = G1*G2;

a = 0;
M = 1;
wv = logspace(-2,2,200);
fr1 = squeeze(freqresp(Gp,wv));

%// Create GUI controls
h1 = figure('Position',[0   580   680   450],'Units','Pixels');
subplot(2,1,1)
subplot(2,1,2)
h2 = figure('Position',[690   660   590   450],'Units','Pixels');
h3 = figure('Position',[5   50   850   100],'Units','Pixels');

figure(h3)

handles.Slider1 = uicontrol('Style','slider','Position',[50 50 200 40],'Min',0,'Max',1,'Value',0.13,'Callback',@SliderCallback);
addlistener(handles.Slider1,'Value','PreSet',@SliderCallback);
handles.Text1 = uicontrol('Style','Text','Position',[100 15 30 40],'String','K','Fontsize',20);
handles.Edit1 = uicontrol('Style','Edit','Position',[130 30 60 25],'String','','Fontsize',14);

handles.Slider2 = uicontrol('Style','slider','Position',[300 50 200 40],'Min',0.1,'Max',20,'Value',5,'Callback',@SliderCallback);
addlistener(handles.Slider2,'Value','PreSet',@SliderCallback);
handles.Text2 = uicontrol('Style','Text','Position',[350 15 25 40],'String','Ti ','Fontsize',20);
handles.Edit2 = uicontrol('Style','Edit','Position',[380 30 60 25],'String','','Fontsize',14);

handles.Slider3 = uicontrol('Style','slider','Position',[550 50 200 40],'Min',0.1,'Max',4,'Value',1.6,'Callback',@SliderCallback);
addlistener(handles.Slider3,'Value','PreSet',@SliderCallback);
handles.Text3 = uicontrol('Style','Text','Position',[600 15 25 40],'String','Td ','Fontsize',20);
handles.Edit3 = uicontrol('Style','Edit','Position',[630 30 60 25],'String','','Fontsize',14);


%
%
Gk = 1;
yold = lsim(feedback(Gk*Gp,1),r-e,t) + lsim(feedback(Gp,Gk),l,t);
yold = yold';  
uold = lsim(feedback(Gk,Gp),r-e,t) - lsim(feedback(Gk*Gp,1),l,t);
uold = uold';        

guidata(h3,handles); %// Update the handles structure.

    function SliderCallback(~,~) %// This is the slider callback, executed when you release it or press the arrows at each extremity. 
        figure(h3)
        handles = guidata(gcf);
        K = get(handles.Slider1,'Value');
        set(handles.Edit1,'String',num2str(K));
        Ti = get(handles.Slider2,'Value');
        set(handles.Edit2,'String',num2str(Ti));
        Td = get(handles.Slider3,'Value');
        set(handles.Edit3,'String',num2str(Td));
        N=10;
        
        
        Gpi = K*(1+1/(s*Ti));
        Glead = (1+s*Td)/(1+s*Td/N);
        Gk = Gpi*Glead;
        [Gm,Pm,Wcg,Wcp] = margin(Gk*Gp);
        
        figure(h1)
        fr2 = squeeze(freqresp(Gk,wv));
        y = lsim(feedback(Gpi*Gp,Glead),r,t) + lsim(feedback(Gk*Gp,1),-e,t) + lsim(feedback(Gp,Gk),l,t);
        y = y';     
        u = lsim(feedback(Gpi,Gp*Glead),r,t) + lsim(feedback(Gk,Gp),-e,t) - lsim(feedback(Gk*Gp,1),l,t);
        u = u';        
        subplot(211)
        %plot(t,yold,'b')
        plot(t,y,'r');
        hold on
        plot(t,0.95*ones(size(t)),'k--')
        plot(t,1.05*ones(size(t)),'k--')
        hold off
        ylabel('step response y','Fontsize',20)
        axis([min(t),max(t),0,2]);
        subplot(212)
        %plot(t,uold,'b')
       
        plot(t,u,'r')
        hold off
        ylabel('input u','Fontsize',20)
        axis([min(t),max(t),-0.4,0.4]);
        
        figure(h2)
        subplot(211)
        loglog(wv,abs(fr1),'b')
        hold on
        loglog(wv,abs(fr1.*fr2),'r')
        plot([Wcp Wcp],[1e-2 1e2],'r--')
        text(1,0.03,['\omega_c = ' num2str(Wcp,3)],'Fontsize',16)        
        plot([min(wv) max(wv)],[1 1],'k')
        axis([min(wv) max(wv) 1e-5 1e5])
        hold off
        subplot(212)
        semilogx(wv,-360+180/pi*phase(fr1),'b')
        hold on
        semilogx(wv,-360+180/pi*phase(fr1.*fr2),'r')
        plot([Wcp Wcp],[-300 -60],'r--')
        plot([1e-1 1e2],[-180+Pm -180+Pm],'r--')
        text(1,-160,['\phi_m = ' num2str(Pm,3)],'Fontsize',16)
        axis([min(wv) max(wv) -280 -90])
        plot([min(wv) max(wv)],[-180 -180],'k')
        hold off
        
    end

endfunction GUI_slider

clc
clear
s=tf('s');
t = 0:0.1:20;
r = ones(1,length(t)); 
l = 0.1*ones(size(t)).*(t>10);
e = 0*randn(size(t));
t2=0.1;
G1 = 1/(1+s*t2);
G2 = 10/s^2;
Gp = G1*G2;

a = 0;
M = 1;
wv = logspace(-2,2,200);
fr1 = squeeze(freqresp(Gp,wv));

%// Create GUI controls
h1 = figure('Position',[0   580   680   450],'Units','Pixels');
subplot(2,1,1)
subplot(2,1,2)
h2 = figure('Position',[690   660   590   450],'Units','Pixels');
h3 = figure('Position',[5   50   1000   100],'Units','Pixels');

figure(h3)

handles.Slider1 = uicontrol('Style','slider','Position',[50 50 200 40],'Min',0,'Max',1,'Value',0.13,'Callback',@SliderCallback);
addlistener(handles.Slider1,'Value','PreSet',@SliderCallback);
handles.Text1 = uicontrol('Style','Text','Position',[100 15 30 40],'String','K','Fontsize',20);
handles.Edit1 = uicontrol('Style','Edit','Position',[130 30 60 25],'String','Move','Fontsize',14);

handles.Slider2 = uicontrol('Style','slider','Position',[300 50 200 40],'Min',0.1,'Max',20,'Value',5,'Callback',@SliderCallback);
addlistener(handles.Slider2,'Value','PreSet',@SliderCallback);
handles.Text2 = uicontrol('Style','Text','Position',[350 15 25 40],'String','Ti ','Fontsize',20);
handles.Edit2 = uicontrol('Style','Edit','Position',[380 30 60 25],'String','Any','Fontsize',14);

handles.Slider3 = uicontrol('Style','slider','Position',[550 50 200 40],'Min',0.1,'Max',4,'Value',1.6,'Callback',@SliderCallback);
addlistener(handles.Slider3,'Value','PreSet',@SliderCallback);
handles.Text3 = uicontrol('Style','Text','Position',[600 15 25 40],'String','Td ','Fontsize',20);
handles.Edit3 = uicontrol('Style','Edit','Position',[630 30 60 25],'String','Slider','Fontsize',14);

handles.Slider4 = uicontrol('Style','slider','Position',[800 50 200 40],'Min',1,'Max',20,'Value',10,'Callback',@SliderCallback);
addlistener(handles.Slider4,'Value','PreSet',@SliderCallback);
handles.Text4 = uicontrol('Style','Text','Position',[850 15 25 40],'String','N ','Fontsize',20);
handles.Edit4 = uicontrol('Style','Edit','Position',[880 30 60 25],'String','!','Fontsize',14);


%
%
Gk = 1;
yold = lsim(feedback(Gk*Gp,1),r-e,t) + lsim(feedback(Gp,Gk),l,t);
yold = yold';  
uold = lsim(feedback(Gk,Gp),r-e,t) - lsim(feedback(Gk*Gp,1),l,t);
uold = uold';        

guidata(h3,handles); %// Update the handles structure.

    function SliderCallback(~,~) %// This is the slider callback, executed when you release it or press the arrows at each extremity. 
        figure(h3)
        handles = guidata(gcf);
        K = get(handles.Slider1,'Value');
        set(handles.Edit1,'String',num2str(K));
        Ti = get(handles.Slider2,'Value');
        set(handles.Edit2,'String',num2str(Ti));
        Td = get(handles.Slider3,'Value');
        set(handles.Edit3,'String',num2str(Td));
        N = get(handles.Slider4,'Value');
        set(handles.Edit4,'String',num2str(N));
        %N=10;
        
        Gpi = K*(1+1/(s*Ti));
        Glead = (1+s*Td)/(1+s*Td/N);
        Gk = Gpi*Glead;
        [Gm,Pm,Wcg,Wcp] = margin(Gk*Gp);
        
        figure(h1)
        fr2 = squeeze(freqresp(Gk,wv));
        y = lsim(feedback(Gpi*Gp,Glead),r,t) + lsim(feedback(Gk*Gp,1),-e,t) + lsim(feedback(Gp,Gk),l,t);
        y = y';     
        u = lsim(feedback(Gpi,Gp*Glead),r,t) + lsim(feedback(Gk,Gp),-e,t) - lsim(feedback(Gk*Gp,1),l,t);
        u = u';        
        subplot(211)
        %plot(t,yold,'b')
        plot(t,y,'r');
        hold on
        plot(t,0.95*ones(size(t)),'k--')
        plot(t,1.05*ones(size(t)),'k--')
        hold off
        ylabel('step response y','Fontsize',20)
        axis([min(t),max(t),0,2]);
        subplot(212)
        %plot(t,uold,'b')
       
        plot(t,u,'r')
        hold off
        ylabel('input u','Fontsize',20)
        axis([min(t),max(t),-0.4,0.4]);
        
        figure(h2)
        subplot(211)
        loglog(wv,abs(fr1),'b')
        hold on
        loglog(wv,abs(fr1.*fr2),'r')
        plot([Wcp Wcp],[1e-2 1e2],'r--')
        text(1,0.03,['\omega_c = ' num2str(Wcp,3)],'Fontsize',16)        
        plot([min(wv) max(wv)],[1 1],'k')
        axis([min(wv) max(wv) 1e-5 1e5])
        hold off
        subplot(212)
        semilogx(wv,-360+180/pi*phase(fr1),'b')
        hold on
        semilogx(wv,-360+180/pi*phase(fr1.*fr2),'r')
        plot([Wcp Wcp],[-300 -60],'r--')
        plot([1e-1 1e2],[-180+Pm -180+Pm],'r--')
        text(1,-160,['\phi_m = ' num2str(Pm,3)],'Fontsize',16)
        axis([min(wv) max(wv) -280 -90])
        plot([min(wv) max(wv)],[-180 -180],'k')
        hold off
        
    end

end