Assignment2, Matlab_220104080
Assignment2, Matlab_220104080
% Normalize y
y = y / norm(y);
% 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
>>
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
% 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
% Normalize eigenvector
x_new = x_new / norm(x_new);
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
>>
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