Spectral
cl
close all
clear all
clc
%defining the matrix
a=[5 1 2;-3 9 4;1 2 -7]
n=size(a);
%splitting the matrix
d=[5 0 0;0 9 0;0 0 -7]
l=[0 0 0;-3 0 0;1 2 0]
u=[0 1 2;0 0 4;0 0 0]
%finding the eigen values
sum=0;
%finding the sum of diagnols
for i=1:3
s_one=a(i,i)+sum;
sum=s_one;
end
%calculating the minors
minor_one= (a(2,2)*a(3,3))-(a(2,3)*a(3,2));
minor_two= (a(1,1)*a(3,3))-(a(1,3)*a(3,1));
minor_three= (a(1,1)*a(2,2))-(a(2,1)*a(1,2));
sum_minor= minor_one+minor_two+minor_three;
%calculating the determinant of Matrix A
deta=det(a);
%the characteristic equation
%giving the equation in the form of an array and then solving them
p=[1 -sum sum_minor deta];
%eigen values
a=roots(p);
%finding the iterative matrix
inv_d=inv(d);
iter_jacobi=inv_d*(l+u);
itersum=l+d;
inv_ld=inv(itersum);
iter_gauss=inv_ld*u;
%calculating the spectral radius for jacobi method matrix
%finding the sum of diagnols
sum_one=0;
for i=1:3
s_one=iter_jacobi(i,i)+sum_one;
sum_one=s_one;
end
%%jacobi method%%
%calculating the minors
minor_onejacobi= (iter_jacobi(2,2)*iter_jacobi(3,3))-(iter_jacobi(2,3)*iter_jacobi(3,2));
minor_twojacobi= (iter_jacobi(1,1)*iter_jacobi(3,3))-(iter_jacobi(1,3)*iter_jacobi(3,1));
minor_threejacobi= (iter_jacobi(1,1)*iter_jacobi(2,2))-(iter_jacobi(2,1)*iter_jacobi(1,2));
sum_minorjacobi= minor_onejacobi+minor_twojacobi+minor_threejacobi;
%calculating the determinant of Matrix
deta_iter=det(iter_jacobi);
%giving the equation in the form of an array and then solving them
q=[1 -sum_one sum_minorjacobi deta_iter];
spetral_radius_iter=real(max(q));
%%Gauss Seidel Method%%
%calculating the minors
minor_onegauss= (iter_gauss(2,2)*iter_gauss(3,3))-(iter_gauss(2,3)*iter_gauss(3,2));
minor_twogauss= (iter_gauss(1,1)*iter_gauss(3,3))-(iter_gauss(1,3)*iter_gauss(3,1));
minor_threegauss= (iter_gauss(1,1)*iter_gauss(2,2))-(iter_gauss(2,1)*iter_gauss(1,2));
sum_minorgauss= minor_onegauss+minor_twogauss+minor_threegauss;
%calculating the determinant of Matrix
deta_itergauss=det(iter_gauss);
%finding the sum of diagnols
sum_two=0;
for i=1:3
s_two=iter_jacobi(i,i)+sum_two;
sum_two=s_two;
end
%giving the equation in the form of an array and then solving them
z=[1 -sum_two sum_minorgauss deta_itergauss];
spetral_radius_gauss=real(max(z));