function GUI_slider

clc
clear
s=tf('s');
t = 0:0.01:1;
r = ones(1,length(t)); 
l = 0*ones(size(t)).*(t>5);
e = 0*randn(size(t));
Gp = 100/(s*(s+10));
a = 0;
M = 1;
wv = logspace(-1,2,200);
fr1 = squeeze(freqresp(Gp,wv));

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

figure(h3)

handles.Slider1 = uicontrol('Style','slider','Position',[50 50 200 40],'Min',0,'Max',5,'Value',1,'Callback',@SliderCallback);
addlistener(handles.Slider1,'Value','PreSet',@SliderCallback);
handles.Text1 = uicontrol('Style','Text','Position',[100 15 30 40],'String','Kk','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,'Max',40,'Value',1,'Callback',@SliderCallback);
addlistener(handles.Slider2,'Value','PreSet',@SliderCallback);
handles.Text2 = uicontrol('Style','Text','Position',[350 15 20 40],'String','b ','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',1,'Max',11,'Value',1,'Callback',@SliderCallback);
addlistener(handles.Slider3,'Value','PreSet',@SliderCallback);
handles.Text3 = uicontrol('Style','Text','Position',[600 15 20 40],'String','N ','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);
        Kk = get(handles.Slider1,'Value');
        set(handles.Edit1,'String',num2str(Kk));
        b = get(handles.Slider2,'Value');
        set(handles.Edit2,'String',num2str(b));
        N = get(handles.Slider3,'Value');
        set(handles.Edit3,'String',num2str(N));
        
        Gk = Kk*N*(s+b)/(s+b*N);
        [Gm,Pm,Wcg,Wcp] = margin(Gk*Gp);
        
        figure(h1)
        fr2 = squeeze(freqresp(Gk,wv));
        y = lsim(feedback(Gk*Gp,1),r-e,t) + lsim(feedback(Gp,Gk),l,t);
        y = y';     
        u = lsim(feedback(Gk,Gp),r-e,t) - lsim(feedback(Gk*Gp,1),l,t);
        u = u';        
        subplot(211)
        plot(t,yold,'b')
        hold on
        plot(t,y,'r'); 
        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,1.4]);
        subplot(212)
        plot(t,uold,'b')
        hold on
        plot(t,u,'r')
        hold off
        ylabel('input u','Fontsize',20)
        axis([min(t),max(t),-1,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-2 1e2])
        hold off
        subplot(212)
        semilogx(wv,180/pi*phase(fr1),'b')
        hold on
        semilogx(wv,180/pi*phase(fr1.*fr2),'r')
        plot([Wcp Wcp],[-200 -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) -200 -60])
        plot([min(wv) max(wv)],[-180 -180],'k')
        hold off
        
    end

end