Skip to main content

Manual Code for Eigenvalue Decomposition in MATLAB


MATLAB Code for Manual Eigenvalue Decomposition

clc;
clear;
close all;

A = [1,2];
R = A' * A;
array_length = length(A);
eigenvalues = manual_eigenvalue_decomposition(R, array_length);
disp('Eigenvalues:');
disp(eigenvalues);

eigenvectors = find_eigenvectors(R, eigenvalues);
disp('Eigenvector Matrix:');
disp(eigenvectors);

% Manual Eigenvalue Decomposition Function
function eigenvalues = manual_eigenvalue_decomposition(A, n)
eigenvalues = zeros(n, 1); % Initialize eigenvalues as a column vector
for i = 1:n
% Start with a random vector
v = randn(n, 1);

% Power iteration to find the eigenvector corresponding to the largest eigenvalue
for j = 1:10 % Iteration count (power iteration steps)
v = A * v; % Multiply by matrix A
v = v / norm(v); % Normalize the vector
end

% Eigenvalue is the Rayleigh quotient
eigenvalues(i) = (v' * A * v) / (v' * v);

% Deflate the matrix to find the next eigenvector
A = A - eigenvalues(i) * (v * v');
end
end

function eigenvectors = find_eigenvectors(A, eigenvalues)
n = size(A, 1); % Get the size of the matrix A
num_eigenvalues = length(eigenvalues); % Get the number of eigenvalues
eigenvectors = zeros(n, num_eigenvalues); % Initialize eigenvectors matrix

for i = 1:num_eigenvalues
lambda = eigenvalues(i); % Take the eigenvalue

% Solve (A - lambda * I) * v = 0
eig_matrix = A - lambda * eye(n); % (A - lambda * I)

% Find the null space (eigenvector corresponding to the eigenvalue)
v = null(eig_matrix);

% If there are multiple eigenvectors, select the first one
if size(v, 2) > 1
v = v(:, 1);
end

% Normalize the eigenvector
eigenvectors(:, i) = v / norm(v);
end
end

 

Output

Eigenvalues:
    5.0000
   -0.0000

Eigenvector Matrix:
    0.4472   -0.8944
    0.8944    0.4472 

 

MATLAB Code for Manual Eigenvalue Decomposition with Scaling of the Input Matrix R

clc;
clear;
close all;

A = [1,2];
R = (A' * A)/2;
array_length = length(A);
eigenvalues = manual_eigenvalue_decomposition(R, array_length);
disp('Eigenvalues:');
disp(eigenvalues);

eigenvectors = find_eigenvectors(R, eigenvalues);
disp('Eigenvector Matrix:');
disp(eigenvectors);

% Manual Eigenvalue Decomposition Function
function eigenvalues = manual_eigenvalue_decomposition(A, n)
eigenvalues = zeros(n, 1); % Initialize eigenvalues as a column vector
for i = 1:n
% Start with a random vector
v = randn(n, 1);

% Power iteration to find the eigenvector corresponding to the largest eigenvalue
for j = 1:10 % Iteration count (power iteration steps)
v = A * v; % Multiply by matrix A
v = v / norm(v); % Normalize the vector
end

% Eigenvalue is the Rayleigh quotient
eigenvalues(i) = (v' * A * v) / (v' * v);

% Deflate the matrix to find the next eigenvector
A = A - eigenvalues(i) * (v * v');
end
end

function eigenvectors = find_eigenvectors(A, eigenvalues)
n = size(A, 1); % Get the size of the matrix A
num_eigenvalues = length(eigenvalues); % Get the number of eigenvalues
eigenvectors = zeros(n, num_eigenvalues); % Initialize eigenvectors matrix

for i = 1:num_eigenvalues
lambda = eigenvalues(i); % Take the eigenvalue

% Solve (A - lambda * I) * v = 0
eig_matrix = A - lambda * eye(n); % (A - lambda * I)

% Find the null space (eigenvector corresponding to the eigenvalue)
v = null(eig_matrix);

% If there are multiple eigenvectors, select the first one
if size(v, 2) > 1
v = v(:, 1);
end

% Normalize the eigenvector
eigenvectors(:, i) = v / norm(v);
end
end


Output 

Eigenvalues:
    2.5000
   -0.0000

Eigenvector Matrix:
    0.4472   -0.8944
    0.8944    0.4472

 

Conclusion

The eigenvalues change, while the eigenvectors remain the same when scaling the input matrix for eigenvalue decomposition.


Copy the MATLAB Code above from here


Further Reading

[1] Singular Value Decomposition in Multi-Antenna Communication

People are good at skipping over material they already know!

View Related Topics to







Admin & Author: Salim

profile

  Website: www.salimwireless.com
  Interests: Signal Processing, Telecommunication, 5G Technology, Present & Future Wireless Technologies, Digital Signal Processing, Computer Networks, Millimeter Wave Band Channel, Web Development
  Seeking an opportunity in the Teaching or Electronics & Telecommunication domains.
  Possess M.Tech in Electronic Communication Systems.


Contact Us

Name

Email *

Message *

Popular Posts

MATLAB code for MSK

๐Ÿ“˜ Overview ๐Ÿงฎ MATLAB Codes ๐Ÿงฎ Theory ๐Ÿงฎ Simulator for MSK ๐Ÿ“š Further Reading  Copy the MATLAB Code from here % The code is developed by SalimWireless.com clc; clear; close all; % Define a bit sequence bitSeq = [0, 1, 0, 0, 1, 1, 1, 0, 0, 1]; % Perform MSK modulation [modSignal, timeVec] = modulateMSK(bitSeq, 10, 10, 10000); % Plot the modulated signal subplot(2,1,1); samples = 1:numel(bitSeq); stem(samples, bitSeq); title('Original message signal'); xlabel('Time (s)'); ylabel('Amplitude'); % Plot the modulated signal subplot(2,1,2); samples = 1:10000; plot(samples / 10000, modSignal(1:10000)); title('MSK modulated signal'); xlabel('Time (s)'); ylabel('Amplitude'); % Perform MSK demodulation demodBits = demodMSK(modSignal, 10, 10, 10000); % Function to perform MSK modulation function [signal, timeVec] = modulateMSK(bits, carrierFreq, baudRate, sampleFreq) % Converts a binary bit sequence in...

MATLAB Code for BER performance of QPSK with BPSK, 4-QAM, 16-QAM, 64-QAM, 256-QAM, etc

๐Ÿ“˜ Overview ๐Ÿงฎ MATLAB Codes ๐Ÿงฎ Theory ๐Ÿงฎ Are QPSK and 4-PSK same? ๐Ÿ“š Further Reading   QPSK offers double the data rate of BPSK while maintaining a similar bit error rate at low SNR when Gray coding is used. It shares spectral efficiency with 4-QAM and can outperform 4-QAM or 16-QAM in very noisy channels. QPSK is widely used in practical wireless systems, often alongside QAM in adaptive modulation schemes [Read more...]   MATLAB Code clear all; close all; % Set parameters for QAM snr_dB = -20:2:20; % SNR values in dB qam_orders = [4, 16, 64, 256]; % QAM modulation orders % Loop through each QAM order and calculate theoretical BER figure; for qam_order = qam_orders     % Calculate theoretical BER using berawgn for QAM     ber_qam = berawgn(snr_dB, 'qam', qam_order);     % Plot the results for QAM     semilogy(snr_dB, ber_qam, 'o-', 'DisplayName', sprintf('%d-QAM', qam_order));  ...

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...

๐Ÿ“˜ Overview of BER and SNR ๐Ÿงฎ Simulator for m-ary QAM and m-ary PSK ๐Ÿงฎ MATLAB Codes ๐Ÿ“š Further Reading Modulation Constellation Diagrams BER vs. SNR BER vs SNR for M-QAM, M-PSK, QPSk, BPSK, ... What is Bit Error Rate (BER)? The abbreviation BER stands for bit error rate, which indicates how many corrupted bits are received (after the demodulation process) compared to the total number of bits sent in a communication process. It is defined as,  In mathematics, BER = (number of bits received in error / total number of transmitted bits)  On the other hand, SNR refers to the signal-to-noise power ratio. For ease of calculation, we commonly convert it to dB or decibels.   What is Signal the signal-to-noise ratio (SNR)? SNR = signal power/noise power (SNR is a ratio of signal power to noise power) SNR (in dB) = 10*log(signal power / noise power) [base 10] For instance,...

Comparisons among ASK, PSK, and FSK | And the definitions of each

๐Ÿ“˜ Overview ๐Ÿงฎ Simulator ๐Ÿงฎ Noise Sensitivity, Bandwidth, Complexity, etc. ๐Ÿงฎ MATLAB Codes ๐Ÿงฎ Some Questions and Answers ๐Ÿ“š Further Reading Modulation ASK, FSK & PSK Constellation MATLAB Simulink MATLAB Code Comparisons among ASK, PSK, and FSK    Comparisons among ASK, PSK, and FSK   Simulator for Calculating Bandwidth of ASK, FSK, and PSK The baud rate represents the number of symbols transmitted per second. Both baud rate and bit rate are same for binary ASK, FSK, and PSK. Select Modulation Type: ASK FSK PSK Baud Rate or Bit Rate (bps): Frequency Deviation (Hz) for FSK: Calculate Bandwidth Comparison among ASK,  FSK, and PSK Performance Comparison: 1. Noise Sensitivity:    - ASK is the most sensitive to noise due to its r...

Differences between Baseband and Passband Modulation Techniques

๐Ÿ“˜ Overview ๐Ÿงฎ Difference betwen baseband and passband ๐Ÿงฎ Baseband modulation techniques ๐Ÿงฎ Passband modulation techniques ๐Ÿ“š Further Reading   1. Frequency Translation Baseband Modulation: The signal occupies the lower end of the frequency spectrum, close to DC (0 Hz). Noise at these frequencies (such as 1/f noise or flicker noise) can significantly impact the signal.  Passband Modulation: The signal is shifted to a higher frequency range by modulating it with a carrier frequency. This translation can help to avoid low-frequency noise and interference, which are often more prevalent and stronger in the baseband. 2. Bandpass Filtering Baseband Modulation: The filtering of baseband signals is often limited by the need to preserve the low-frequency components of the signal. This makes it difficult to filter out low-frequency noise effectively. Passband Modulation: The modulated signal can be passed through a bandpass filter centered around t...

Constellation Diagrams of ASK, PSK, and FSK

๐Ÿ“˜ Overview ๐Ÿงฎ Simulator for constellation diagrams of ASK, FSK, and PSK ๐Ÿงฎ Theory ๐Ÿงฎ MATLAB Codes ๐Ÿ“š Further Reading BASK (Binary ASK) Modulation: Transmits one of two signals: 0 or -√Eb, where Eb​ is the energy per bit. These signals represent binary 0 and 1.    BFSK (Binary FSK) Modulation: Transmits one of two signals: +√Eb​ ( On the y-axis, the phase shift of 90 degrees with respect to the x-axis, which is also termed phase offset ) or √Eb (on x-axis), where Eb​ is the energy per bit. These signals represent binary 0 and 1.  BPSK (Binary PSK) Modulation: Transmits one of two signals: +√Eb​ or -√Eb (they differ by 180 degree phase shift), where Eb​ is the energy per bit. These signals represent binary 0 and 1.    Simulator for BASK, BPSK, and BFSK Constellation Diagrams SNR (dB): 15 Add A...

Theoretical BER vs SNR for binary ASK and FSK

  Theoretical Ber vs SNR for Amplitude Shift Keying (ASK) The theoretical bit error rate (BER) for binary Amplitude Shift Keying (ASK) as a function of the signal-to-noise ratio (SNR) can be derived using the following expression: If we map the binary signals to 1 and -1 in ASK , the probability of bit error will be: BER = Q(√(2*SNR))   If we map the binary signals to 0 and 1 in ASK , the probability of bit error will be:    BER = Q(√(SNR/2))   Where: Q(x) is the Q-function, which is the tail probability of the standard normal distribution. SNR is the signal-to-noise ratio. N0 is the noise power spectral density. Where Q is the Q function In mathematics Q(x) = 0.5 * erfc(x/ √ 2)   Calculate the Probability of Error using Q-function for ASK: For ASK with amplitudes 0 and 1 : When bit '0' is transmitted, the received signal is noise only . When bit '1' is transmitted, the received signal is 1 + noise . The receiver makes a decision at the threshold...

MATLAB Code for QAM (Quadrature Amplitude Modulation)

๐Ÿ“˜ Overview of QAM ๐Ÿงฎ MATLAB Code for 4-QAM ๐Ÿงฎ MATLAB Code for 16-QAM ๐Ÿงฎ MATLAB Code for m-ary QAM (4-QAM, 16-QAM, 32-QAM, ...) ๐Ÿ“š Further Reading   One of the best-performing modulation techniques is QAM [↗] . Here, we modulate the symbols by varying the carrier signal's amplitude and phase in response to the variation in the message signal (or voltage variation). So, we may say that QAM is a combination of phase and amplitude modulation. Additionally, it performs better than ASK or PSK [↗] . In fact, any constellation for any type of modulation, signal set (or, symbols) is structured in a way that prevents them from interacting further by being distinct by phase, amplitude, or frequency. MATLAB Script (for 4-QAM) % This code is written by SalimWirelss.Com % This is an example of 4-QAM. Here constellation size is 4 % or total number of symbols/signals is 4 % We need 2 bits once to represent four constellation points % QAM modulation is the combina...