function [GM,PM] = computeGMPM(G,w)

    [gain, p_angle] = computeBode(G,w);

    gain_xover_index = [];
    phase_xover_index = [];
    GM = [];
    PM = [];

    for a = 1:length(gain)
        if a > 1
            if gain(a-1) > 0 && gain(a) < 0
                gain_xover_index = a;
            end
            if p_angle(a-1) > -180 && p_angle(a) < -180
                phase_xover_index = a;
            end
        end
        if ~isempty(gain_xover_index)
            PM = p_angle(gain_xover_index) + 180;
        end
        if ~isempty(phase_xover_index)
            GM = 0 - gain(phase_xover_index);
        end
    end
    
    if ~isempty(GM)
        sprintf('Gain Margin is %0.4g dB.',GM)
    else
        sprintf('Gain Margin is undefined.')
    end
    if ~isempty(PM)
        sprintf('Phase Margin is %0.4g degrees.',PM)
    else
        sprintf('Phase Margin is undefined.')
    end
     if ~isempty(gain_xover_index)
        sprintf('Gain crossover index is %d.',gain_xover_index)
    else
        sprintf('No Gain crossover')
    end

     if ~isempty(phase_xover_index)
        sprintf('Phase crossover index is %d.',gain_xover_index)
    else
        sprintf('No Phase crossover')
    end

end
