Understanding linear systems to write computer programs to manually determine the Eigen Value and Spectral Radius of the given system of equations
Understanding linear systems to write computer programs to manually determine the Eigen Value and Spectral Radius of the given system of equations
## Matlab code (main code with functions code)
clear all;
close all;
clc;
n=10;
method =input('Enter the method no.')
%%
%%solving linear system and solving using itrative methods
%Diagonal matrix
D = [5 0 0;0 9 0;0 0 -7];
%Upper diagonal matrix
U = [0 1 2;0 0 4;0 0 0];
%Lower diagonal matrix
L = [0 0 0;-3 0 0;1 2 0];
B=[10 -14 33]';%RHS Residual
A =D+L+U;%Coefficient matrix
%Diagonal magnification
E =linspace(0.5,2,10);
x=[0 0 0 ]';
Dold = D;
xold=x;
Tjac=inv(D)*(L+U);
Eigen_value_jac=eig(Tjac);
rho_jac_sor = max(abs(real(Eigen_value_jac)));
for i=1:length(E)
D = E(i)*Dold;
%Diagonal magnification
%Jacobi method
if method==1
Tjac=inv(D)*(L+U);
[jac_it(i),x,error]=jacobi_iter(D,L,U,B,x);
Eigen_value_jac=eig(Tjac);
rho_jac(i) = max(abs(real(Eigen_value_jac)));
end
%Gauss seidel method
if method==2
Tgs=inv(D+L)*(U);
[GS_it(i),x,error]=GS_iter(D,L,U,B,x);
Eigen_value_gs=eig(Tgs);
rho_gs(i) = max(abs(real(Eigen_value_gs)));
end
%Sucessive over relaxation method
if method==3
w=0.95;
Tsor=(inv(D+w*L)*((w-1)*D+(w*U)));
[sor_it(i),x,error]=sor_iter(D,L,U,B,x);
Eigen_value_sor=eig(Tsor);
rho_sor(i) = max(abs(real(Eigen_value_sor)));
end
end
if method==1
figure(1)
subplot(3,1,1)
plot(E,rho_jac);
xlabel('Diagonal magnification');
ylabel('Spectral radius');
title('Jacobi iterative solver');
legend('Diagonal magnification vs spectral radius');
hold on
subplot(3,1,2)
loglog(rho_jac,jac_it,'r');
xlabel('Spectral radius');
ylabel('No.of iteration');
legend('Spectral radius vs No.of iteration','location','northwest');
end
if method==2
figure(1)
subplot(3,1,1)
plot(E,rho_gs);
xlabel('Diagonal magnification');
ylabel('Spectral radius');
title('Gauss seidel method');
legend('Diagonal magnification vs Spectral radius');
hold on
subplot(3,1,2)
loglog(rho_gs,GS_it,'r');
xlabel('Spectral radius');
ylabel('No.of iteration');
legend('Spectral radius vs No.of iteration','location','northwest');
end
if method==3
figure(1)
subplot(3,1,1)
plot(E,rho_sor);
xlabel('Diagonal magnification');
ylabel('Spectral radius');
title('Sucessive over relaxation');
legend('Diagonal magnification vs Spectral radius');
hold on
subplot(3,1,2)
loglog(rho_sor,sor_it,'r');
xlabel('Spectral radius');
ylabel('No.of iteration');
legend('Spectral radius vs No.of iteration','location','northwest');
end
------------------------------------------------------------------------------------------
%% function jacobi
function [jac_it,x,error]=jacobi_iter(D,L,U,B,x)
x=[0 0 0]';
jac_it=0;
error=9e9;
tol=1e-4;
xold = x;
while error>tol
jac_it=jac_it+1;
%Updated value
x = (inv(D)*(L+U)*xold) +inv(D)*B;
error=max(abs(xold-x));%error
xold = x;
end
end
----------------------------------------------------------------------------------------------
%% function Gauss seidel
function [GS_it,x,error]=GS_iter(D,L,U,B,x)
x=[0 0 0]';
GS_it=0;
error=9e9;
tol=1e-4;
xold = x;
while error>tol
GS_it=GS_it+1;
%Updated value
x = (inv(D+L)*(U)*xold)+inv(D+L)*B;
error=max(abs(xold-x));%error
xold = x;
end
end
------------------------------------------------------------------------------------------------
%% function Successive Over Relaxation
function [sor_it,x,error]=sor_iter(D,L,U,B,x,w)
x=[0 0 0]';
sor_it=0;
error=9e9;
tol=1e-4;
xold = x;
while error>tol
sor_it=sor_it+1;
w=0.75;
%Updated value
x = (inv(D+L*w)*((w-1)*D+(w*U))*xold)+inv(D+w*L)*B;
error=max(abs(xold-x));%error
xold = x;
end
end
-------------------------------------------------------------------------------------------------
Results :-
Plots of jacobi method:
Plots of Gauss seidel method:-
Plots of Successive Over Relaxation:-