0% found this document useful (0 votes)
1 views

MATLAB CODE FOR CIAT _II

The document outlines various matrix decomposition techniques including Singular Value Decomposition (SVD), Principal Component Analysis (PCA), Hermitian and Unitary matrices, QR Decomposition, and the Gram-Schmidt process. It provides MATLAB code snippets for each method, demonstrating how to compute and display results such as singular values, eigenvalues, and orthonormal matrices. Additionally, it includes checks for matrix properties like Hermitian and Unitary.

Uploaded by

qwegames00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

MATLAB CODE FOR CIAT _II

The document outlines various matrix decomposition techniques including Singular Value Decomposition (SVD), Principal Component Analysis (PCA), Hermitian and Unitary matrices, QR Decomposition, and the Gram-Schmidt process. It provides MATLAB code snippets for each method, demonstrating how to compute and display results such as singular values, eigenvalues, and orthonormal matrices. Additionally, it includes checks for matrix properties like Hermitian and Unitary.

Uploaded by

qwegames00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

% Perform Singular Value Decomposition

% Define the matrix A


A = [1 1; 0 1; 1 0];

% Perform Singular Value Decomposition


[U, S, V] = svd(A);

% Display the results


disp('Matrix A:');
disp(A);

disp('Left singular vectors (U):');


disp(U);

disp('Singular values (S):');


disp(S);

disp('Right singular vectors (V):');


disp(V);

% Verify the decomposition


disp('Reconstructed A from U*S*V'':');
disp(U * S * V');

% Principal Component Analysis

% Define the given covariance matrix


A = [1 -2 0; -2 5 0; 0 0 2];

% Step 1: Compute eigenvalues and eigenvectors


[V, D] = eig(A);

% Step 2: Sort the eigenvalues in descending order


[eigenvalues_sorted, idx] = sort(diag(D), 'descend');

% Step 3: Reorder the eigenvectors accordingly


V_sorted = V(:, idx);

% Display the results


disp('Covariance Matrix:');
disp(A);

disp('Eigenvalues (Variances of Principal Components):');


disp(eigenvalues_sorted);

disp('Principal Components (Eigenvectors):');


disp(V_sorted);
% Hermitian and Unitary matrices
% Define your matrix here (you can test different examples)
A = [0.7071 0.7071i; -0.7071i 0.7071];

% Tolerance for floating-point comparison


tol = 1e-10;

% Compute the conjugate transpose


A_conj_transpose = A';

% Check if Hermitian: A == A'


isHermitian = norm(A - A_conj_transpose) < tol;

% Check if Unitary: A'*A == I and A*A' == I


I = eye(size(A));
isUnitary = norm(A_conj_transpose * A - I) < tol && norm(A * A_conj_transpose - I) <
tol;

% Display results
disp('Matrix A:');
disp(A);

if isHermitian
disp('A is a Hermitian matrix.');
else
disp('A is NOT a Hermitian matrix.');
end

if isUnitary
disp('A is a Unitary matrix.');
else
disp('A is NOT a Unitary matrix.');
end

% find the eigenvalues and eigenvectors of a Hermitian Matrix


A = [2 1+i; 1-i 3];

% Step 1: Compute eigenvalues and eigenvectors


[V, D] = eig(A)

% QR Decomposition

function [Q, R] = qr_decomposition(A)


% Simple QR decomposition using Gram-Schmidt process
[m, n] = size(A);
Q = zeros(m, n);
R = zeros(n, n);
for i = 1:n
v = A(:, i);
for j = 1:i-1
R(j, i) = Q(:, j)' * A(:, i);
v = v - R(j, i) * Q(:, j);
end
R(i, i) = norm(v);
Q(:, i) = v / R(i, i);
end
end

% Example Usage:
A = [1 0 0 ; 1 1 0; 1 1 1];
[Q, R] = qr_decomposition(A);
disp(Q);
disp(R);

%Gram Schmidt Process

function Q = gram_schmidt(A)
% GRAM_SCHMIDT performs the Gram-Schmidt orthogonalization process
% Input: A - an m x n matrix (m >= n) with linearly independent columns
% Output: Q - an m x n orthonormal matrix

[m, n] = size(A);
Q = zeros(m, n);

for i = 1:n
v = A(:, i);
for j = 1:i-1
proj = (Q(:, j)' * A(:, i)) * Q(:, j);
v = v - proj;
end
norm_v = norm(v);
if norm_v < 1e-10 % Check for zero norm
error('Matrix has dependent columns or numerical instability occurred');
end
Q(:, i) = v / norm_v;
end
end

% Example Usage:
A = [0 1 0; 1 0 1; 1 0 -1];
Q = gram_schmidt(A);
disp(Q);

You might also like