File size: 5,216 Bytes
5ab75a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | function [V1, V2, L1, L2] = eig2x2(Araw)
% eig2x2 - eigenvalue and eigenvector solver for 2x2 symmetric matrices.
%
% Syntax: [V1, V2, L1, L2] = eig2x2(Araw);
%
% =========================================================================
%
% Copyright (c) 2023-2024 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Input:
%
% Araw - matrix representing n symmetric matrices [nx3]
% Araw(:,1) is the 1,1 component for each symmetric
% matrix
% Araw(:,2) is the 1,2 and 2,1 component for each
% symmetric matrix
% Araw(:,3) is the 2,2 component for each symmetric
% matrix
%
% =========================================================================
%
% Output:
%
% V1 - matrix representing n of the 1st eigenvectors [nx2]
% V2 - matrix representing n of the 2nd eigenvectors [nx2]
% L1 - matrix representing n of the 1st eigenvalues [nx1]
% L2 - matrix representing n of the 2nd eigenvalues [nx1]
%
% =========================================================================
%
% References:
%
% Martel, Stephen J., "Eigenvectors and eigenvalues of real symmetric
% matrices," June 2016
%
% =========================================================================
%
% Initial version: May 2022; Latest update: Nov 2024
%
% ----------------- BEGIN CODE -----------------
Nvec = size(Araw,1);
% Calculate eigenvalues and eigenvectors for each covariance matrix
% A = [a b; c d], with b = c
% with
% a = Amat(:,1); b = Amat(:,2); c = b; d = Amat(:,3);
% Trace: T = a+d
% Determinant: D = a*d-b*c
T = Araw(:,1)+Araw(:,3);
D = Araw(:,1).*Araw(:,3)-Araw(:,2).^2;
% Eignevalues calculation
L1 = (T + sqrt(T .^ 2 - 4 .* D)) ./ 2; % Largest eigenvalue
L2 = (T - sqrt(T .^ 2 - 4 .* D)) ./ 2; % Smallest eigenvalue
% Initialize the conjunction plane eigenvector arrays
V1 = NaN([Nvec 2]);
V2 = NaN([Nvec 2]);
% Eigenvectors for the subset of covariance matrices that have
% non-zero off-diagonal values
c0 = Araw(:,2) ~= 0;
V1(c0,1) = L1(c0)-Araw(c0,3);
V2(c0,1) = L2(c0)-Araw(c0,3);
V1(c0,2) = Araw(c0,2);
V2(c0,2) = Araw(c0,2);
V1(c0,:) = V1(c0,:)./repmat(sqrt(V1(c0,1).^2+V1(c0,2).^2),[1 2]);
V2(c0,:) = V2(c0,:)./repmat(sqrt(V2(c0,1).^2+V2(c0,2).^2),[1 2]);
% Eigenvectors for A matrices with zero off-diagonal values
c0 = ~c0;
if any(c0)
ca = Araw(:,3) <= Araw(:,1);
c1 = c0 & ca;
V1(c1,1) = 1; V2(c1,1) = 0;
V1(c1,2) = 0; V2(c1,2) = 1;
c1 = c0 & ~ca;
V1(c1,1) = 0; V2(c1,1) = 1;
V1(c1,2) = 1; V2(c1,2) = 0;
end
% Find instances where the determinant is not accurately calculated
% due to floating point representation errors
floatErrIdx = (Araw(:,1).*Araw(:,3) == Araw(:,1).*Araw(:,3)-Araw(:,2).^2) & ...
Araw(:,2).^2 ~= 0;
% Check for "b" values that are close to 0, but aren't exactly 0. The
% eig2x2 algorithm starts to break down with abs(b) < 1e-2
bVal0Idx = abs(Araw(:,2)) < 1e-2 & Araw(:,2) ~= 0;
% Manually run eig() for any of the special checks
c0 = floatErrIdx | bVal0Idx;
if sum(c0) > 0
fix = find(c0);
for i = 1:length(fix)
idx = fix(i);
tempA = [Araw(idx,1) Araw(idx,2); Araw(idx,2) Araw(idx,3)];
[V, D] = eig(tempA, 'vector');
[D, ind] = sort(D);
V = V(:, ind);
V2(idx,:) = V(:,1)';
V1(idx,:) = V(:,2)';
L2(idx) = D(1);
L1(idx) = D(2);
end
end
end
% ----------------- END OF CODE -----------------
%
% Please record any changes to the software in the change history
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer | Date | Description
% ---------------------------------------------------
% L. Baars | 05-19-2022 | Initial development
% E. White | 07-12-2023 | Added compliant documentation, fixed one
% instance of division to be componentwise
% D. Hall | 11-27-2024 | Fixed issue for diagonal covariances that
% produce out-of-order eigenvectors
% L. Baars | 11-29-2024 | Fixed the issue where floating point
% precision calculation errors cause
% imaginary eig2x2 outputs.
% =========================================================================
%
% Copyright (c) 2023-2024 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
|