function [T,K,Mdyn] = compute_period_eig_alt(m, kStory, unitType)
% compute_period_eig
%
% Computes fundamental period of an n-story shear building
% using the eigenvalue method only.
%
% INPUTS:
%   m        = story masses
%              English: lbm
%              Metric : kg
%
%   h        = story elevations
%              English: ft
%              Metric : m
%
%   kStory   = story stiffnesses
%              English: lbf/in
%              Metric : N/m
%
%   unitType = 'english' or 'metric'
%
% OUTPUT:
%   T = fundamental period (seconds)

arguments
    m (:,1) double {mustBePositive}
    kStory (:,1) double {mustBePositive}
    unitType (1,:) char {mustBeMember(unitType,{'english','metric'})}
end

n = length(m);

%% ==========================================================
% UNIT HANDLING
%% ==========================================================

if strcmpi(unitType,'english')

    % Gravitational conversion constant
    gc = 386.0886;  % lbm*in/(lbf*s^2)

    % Dynamic mass matrix (lbf*s^2/in)
    Mdyn = diag(m / gc);

else  % metric

    % Elevations remain in meters
    hU = h;

    % Dynamic mass matrix (kg)
    Mdyn = diag(m);

end

%% ==========================================================
% ASSEMBLE SHEAR BUILDING STIFFNESS MATRIX
%% ==========================================================

K = zeros(n);

for i = 1:n
    ki   = kStory(i);
    kip1 = 0;
    if i < n, kip1 = kStory(i+1); end

    K(i,i) = ki + kip1;

    if i > 1
        K(i,i-1) = -ki;
        K(i-1,i) = -ki;
    end

    if i < n
        K(i,i+1) = -kip1;
        K(i+1,i) = -kip1;
    end
end

%% ==========================================================
% EIGENVALUE SOLUTION (Fundamental Mode)
%% ==========================================================

[Phi,Omega2] = eig(K,Mdyn);

w2 = real(diag(Omega2));

[w2min,idx] = min(w2);

omega = sqrt(w2min);

T = 2*pi / omega;

end