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

Assignment2, Matlab_220104080

The document details MATLAB code for computing eigenvalues and eigenvectors using inverse iteration and Rayleigh quotient methods, demonstrating convergence within a specified tolerance. It also includes rank approximations of a matrix using Singular Value Decomposition (SVD), showing results for rank 1, 2, and 3 approximations along with their respective norms. The estimated second largest eigenvalue is approximately 2.507035, with eigenvectors provided for both methods.

Uploaded by

Ramu Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment2, Matlab_220104080

The document details MATLAB code for computing eigenvalues and eigenvectors using inverse iteration and Rayleigh quotient methods, demonstrating convergence within a specified tolerance. It also includes rank approximations of a matrix using Singular Value Decomposition (SVD), showing results for rank 1, 2, and 3 approximations along with their respective norms. The estimated second largest eigenvalue is approximately 2.507035, with eigenvectors provided for both methods.

Uploaded by

Ramu Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Santanu

RamuBarikKumar Assignment 2 220104084


220104080
MATLAB Command Window Page 1
Q.1
>> M = [3 0 0; 0 2 0; 0 0 1]; % Mass matrix
K = [7 -3 0; -3 6 -3; 0 -3 3]; % Stiffness matrix

% Step 1: Compute Eigenvalues (only for reference, not used in iteration)


lambda_all = eig(K, M);
lambda_all = sort(lambda_all, 'descend'); % Sort eigenvalues in descending order
lambda_2 = lambda_all(2); % Second largest eigenvalue

% Step 2: Set initial values for inverse iteration


mu = lambda_2 - 0.2; % Shift mu further away to allow more iterations
x = [1; 1; 1]; % Initial eigenvector
tol = 1e-6; % Convergence tolerance
max_iter = 1000; % Maximum iterations

errors = zeros(max_iter, 1); % Store errors for convergence plot


lambda_prev = Inf; % Initial previous eigenvalue
converged = false; % Convergence flag

% Perform Inverse Iteration


for i = 1:max_iter
% Solve (K - mu * M) * y = M * x
A = (K - mu * M);

% Check if A is singular (to avoid division by zero errors)


if abs(det(A)) < 1e-10
warning('Matrix is nearly singular, stopping iteration.' );
break;
end

% Solve for new x


y = A \ (M * x);

% Normalize y
y = y / norm(y);

% Compute the Rayleigh quotient for eigenvalue estimate


lambda_i = (y' * K * y) / (y' * M * y);

% Compute relative error


error = abs((lambda_i - lambda_prev) / lambda_prev);
errors(i) = error;

% Check convergence
if error < tol
fprintf('Converged in %d iterations.\n' , i);
fprintf('Estimated second largest eigenvalue: %.6f\n' , lambda_i);
fprintf('Eigenvector:\n');
disp(y);
converged = true;
MATLAB Command Window Page 2

break;
end

% Update variables
lambda_prev = lambda_i;
x = y;
end

% If did not converge within max_iter


if ~converged
fprintf('Did not converge within %d iterations.\n' , max_iter);
end

% Trim unused error values


errors = errors(1:i);

% Plot error vs iteration number


figure;
semilogy(1:length(errors), errors, '-o', 'LineWidth', 1.5);
xlabel('Iteration Number');
ylabel('Relative Error');
title('Convergence of Inverse Iteration Method' );
grid on;
Converged in 5 iterations.
Estimated second largest eigenvalue: 2.507035
Eigenvector:
0.6823
-0.1186
-0.7213

>>
Convergence of Inverse Iteration Method
10-1

10-2

10-3
Relative Error

10-4

10-5

10-6

10-7
2 2.5 3 3.5 4 4.5 5
Iteration Number
MATLAB Command Window Page 1
19 February, 2025 2:56:48 AM
Q.2
>> % Given Matrices
M = [3 0 0; 0 2 0; 0 0 1]; % Mass matrix
K = [7 -3 0; -3 6 -3; 0 -3 3]; % Stiffness matrix

% Compute all eigenvalues (for validation)


lambda_all = eig(K, M);
lambda_all = sort(lambda_all, 'descend'); % Sort eigenvalues in descending order
lambda_2 = lambda_all(2); % Second largest eigenvalue

% Initial Guess
mu = lambda_2 - 0.2; % Start close to the second eigenvalue for better convergence
x = [1; 1; 1]; % Initial eigenvector guess
tol = 1e-6; % Convergence tolerance
max_iter = 1000; % Maximum iterations

errors = zeros(max_iter, 1); % Store errors


lambda_prev = Inf; % Initialize previous eigenvalue

% Rayleigh Quotient Iteration


for i = 1:max_iter
% Solve (K - mu * M) * x = M * x
A = (K - mu * M);

% Check if matrix is nearly singular


if abs(det(A)) < 1e-10
warning('Matrix is nearly singular, stopping iteration.' );
break;
end

% Solve the system


x_new = A \ (M * x);

% Normalize eigenvector
x_new = x_new / norm(x_new);

% Compute Rayleigh Quotient


lambda = (x_new' * K * x_new) / (x_new' * M * x_new);

% Compute relative error (Avoid division by infinity in first step)


if i > 1
error = abs((lambda - lambda_prev) / lambda_prev);
else
error = abs(lambda - lambda_prev);
end

errors(i) = error;
MATLAB Command Window Page 2
19 February, 2025 2:56:48 AM

% Check convergence
if error < tol
fprintf('Converged in %d iterations.\n' , i);
fprintf('Estimated second largest eigenvalue: %.6f\n' , lambda);
fprintf('Eigenvector:\n');
disp(x_new);
break;
end

% Update values for next iteration


lambda_prev = lambda;
mu = lambda; % Update μ using Rayleigh quotient
x = x_new;
end

% Trim unused error values


errors = errors(1:i);

% Plot error vs iteration number


figure;
semilogy(1:length(errors), errors, '-o', 'LineWidth', 1.5);
xlabel('Iteration Number');
ylabel('Relative Error');
title('Convergence of Rayleigh Quotient Iteration Method' );
grid on;
Converged in 4 iterations.
Estimated second largest eigenvalue: 2.507035
Eigenvector:
0.6824
-0.1185
-0.7213

>>
Convergence of Rayleigh Quotient Iteration Method
100

10-2
Relative Error

10-4

10-6

10-8

10-10
2 2.2 2.4 2.6 2.8 3 3.2 3.4 3.6 3.8 4
Iteration Number
MATLAB Command Window Page 1
19 February, 2025 3:04:23 AM

Q.3 >> A = [1 2 3 4 5 6;
0 3 0 7 9 11;
1 4 0 10 0 17;
0.8 7.3 -0.4 16.5 9.6 28;
1 0 1 4 6.2 -12.3];
% Rank 1 approximation
[U, S, V] = svd(A);
A_rank1 = U(:,1) * S(1,1) * V(:,1)';
norm_rank1 = norm(A - A_rank1);
% Rank 2 approximation
A_rank2 = U(:,1:2) * S(1:2,1:2) * V(:,1:2)';
norm_rank2 = norm(A - A_rank2);
% Rank 3 approximation
A_rank3 = U(:,1:3) * S(1:3,1:3) * V(:,1:3)';
norm_rank3 = norm(A - A_rank3);
% Display the results
disp('Rank 1 approximation:');
disp(A_rank1);
disp(['Norm of A - Areduced: ', num2str(norm_rank1)]);
disp('Rank 2 approximation:');
disp(A_rank2);
disp(['Norm of A - Areduced: ', num2str(norm_rank2)]);
disp('Rank 3 approximation:');
disp(A_rank3);
disp(['Norm of A - Areduced: ', num2str(norm_rank3)])
Rank 1 approximation:
0.2143 1.7261 0.0205 3.8582 2.0558 7.0376
0.3831 3.0853 0.0367 6.8965 3.6748 12.5797
0.4945 3.9822 0.0473 8.9012 4.7430 16.2365
0.8769 7.0620 0.0839 15.7854 8.4112 28.7937
-0.1739 -1.4005 -0.0166 -3.1305 -1.6681 -5.7103

Norm of A - Areduced: 14.1854


Rank 2 approximation:
0.4246 2.0110 0.3539 5.2213 4.4128 5.5245
0.6997 3.5144 0.5387 8.9489 7.2236 10.3014
0.2672 3.6742 -0.3130 7.4282 2.1960 17.8716
0.9799 7.2015 0.2472 16.4529 9.5654 28.0527
0.6558 -0.2761 1.2990 2.2484 7.6323 -11.6808

Norm of A - Areduced: 5.3206


Rank 3 approximation:
0.1910 1.8421 0.4566 4.1621 5.2911 5.8969
0.2515 3.1902 0.7358 6.9160 8.9093 11.0161
0.8355 4.0851 -0.5629 10.0051 0.0591 16.9657
0.9887 7.2079 0.2433 16.4929 9.5322 28.0387
MATLAB Command Window Page 2
19 February, 2025 3:04:23 AM

1.0406 0.0022 1.1298 3.9932 6.1854 -12.2942

Norm of A - Areduced: 2.9509


>>

You might also like