function [bound_S, bound_T, bound_KS, bound_SG, bound_KSGd, bound_SGd]=beregn_bounds(G,dist_zeros,dist_poles,Gd)

%=========================================================================\
% Finds Gdms (minimum-phase and stable version of Gd)
%=========================================================================/
Gdms=finn_Gms(Gd);
%=========================================================================\
% Bounds on S and T in the SISO-case
%=========================================================================/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Her ville jeg prøve å finne de distinkte polene automatisk! men det ble
% problemer fordi matlab ikke finner polene eksakt! derfor byr det på
% problemer å benytte seg av funksjonen unique på en tabell!.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [num_Gdms, den_Gdms]=tfdata(Gdms,'v');
% roots(num_Gdms)
% roots(den_Gdms)
% %skal finne distinkte poler!!!!!!!!!
% [num_h, den_h]=tfdata(h,'v');
% dist_zeros=unique(roots(num_h)');
% dist_poles=unique(roots(den_h)');
[Qz, Qp, Qzp]=finn_Qz_Qp_Qzp(dist_zeros, dist_poles);
bound_S=sqrt(1+max((svd(sqrtm(pinv(Qz))*Qzp*sqrtm(pinv(Qp)))))^2);
bound_T=bound_S;        
%=========================================================================\
% Bounds on KS in the SISO-case
%=========================================================================/
%The Hankel-norm
[hankel_stab,hankel_unstab] = hankelsv(G);
if(length(hankel_unstab)==0)
    bound_KS=0;
else    
    bound_KS=1/min(hankel_unstab);
end
%=========================================================================\
% Bounds on SG in the SISO-case : Litt humbug implementasjon
%=========================================================================/
%Tror det må være ingen poler eller nullpunkt på den imaginære aksen! s 225
%i Skogestad&Postlewaith. Polene og nullpunktene må i tillegg være
%distinkte!
%Spør Morten eller Sigurd om utregningen av denne!
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Fungerer ikke helt; HVORFOR?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Gms=finn_Gms(G); %finds minimum-phase and stable version of G!
% %pz_dir(G)
% [Qz1,Qz2,Qp1,Qp2,Qzp2]=finn_Qzpij(dist_zeros,dist_poles, Gms);
% 
% %bound_SG=sqrt(max(abs(eig(sqrtm(pinv(Qz1))*(Qz2+Qzp2'*pinv(Qp2)*Qzp2)*sqrtm(pinv(Qz1))))))
% 
% 
% bound_SG=sqrtm(pinv(Qz1))*(Qz2+(Qzp2'*pinv(Qp2)*Qzp2))*sqrtm(pinv(Qz1))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Denne metoden gir IKKE tight bound!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Gms=finn_Gms(G);
rhp_zero=finn_rhp_zero(G);
rhp_pole=finn_rhp_pole(G);
rhp_zero
bound=0;
if(rhp_zero(1)==0)
    bound_SG=0;
else
    for ii=1:length(rhp_zero)
        for jj=1:length(rhp_pole)
            if(jj==1)
                bound(ii)=abs(rhp_zero(ii)+rhp_pole(jj))/abs(rhp_zero(ii)-rhp_pole(jj));
            else
                bound(ii)=bound(ii)*abs(rhp_zero(ii)+rhp_pole(jj))/abs(rhp_zero(ii)-rhp_pole(jj));
            end
        end
    end
end
bound_SG=max(bound)
%=========================================================================\
% Bounds on KSGd in the SISO-case : HUSK AT JEG MÅ FINNE GD!!! Ikke
% implementert skikkelig!
%=========================================================================/
% [hankel_stab,hankel_unstab] = hankelsv(inv(Gdms)*G);
% bound_KSGd=1/hankel_unstab;
bound_KSGd=0;
%=========================================================================\
% Bounds on SGd in the SISO-case : IKKE IMPLEMENTERT
%=========================================================================/
bound_SGd=0;
end