% E6_6 - Example 6.6 in book

clc
clear
format compact

K = 8
p = [1 4 5 (2+2*K)];
r = roots (p)

K = 10
p = [1 4 5 (2+2*K)];
r = roots (p)

% Program to check stability for a range of K's
for K = 7:10
    disp (['K = ' num2str(K)])
    p = [1 4 5 (2+2*K)];        % or just p(4) = 2+2*K
    r = roots(p)
    stable = true;
    for i = 1:length(r)
        if (real(r(i)) >= 0)
            stable = false;
            break;
        end
    end
    if (stable)
        disp ('stable');
    else
        disp ('not stable');
    end
    
    % alternative code
    if (all (real(r) < 0))
        disp ('the system is stable');
    else
        disp ('the system is not stable');
    end
end

