Week 6 - Understanding linear systems
PROBLEM STATEMENT:
cofficient matrix:
`A=[[5,1, 2],[-3,9,4],[1,2,-7]]`
solution matrix
`x=[[x1],[x2],[x3]]`
right side matrix
`B=[[10],[-14],[33]]`
we have a 3 gernal linear sytems the equations
AX=B
`X=[A^-]B`
Eigen values of given matrix=`[A-lamdaI]`
`A=[[5,1, 2],[-3,9,4],[1,2,-7]]`-`lamda[[1,0,0],[0,1,0],[0,0,1]]`
we get the equtions A
ITERATIVE MATRIX
JACOBI:
TJAC=`-(D^-1)*(L+U)`
GAUSSIDEAL:
`GAUS=`-(D+L)^-1)*U`
SOR:
`TSOR=-(D+omegaL)^-1*((1-omega)*L)*U`
MATLAB CODE:
clc clear allclose all%Defining the matrix A L= [0 0 0;-3 0 0;1 2 0];U =[0 1 2;0 0 4;0 0 0];D=[5 0 0 ;0 9 0;0 0 -7];A= L + U + D;B=[10;-14;33]; % iterative methods% jacobiTjac=-inv(D)*(L+U);% spctral radiusspctral_radius_jacobi=max(real(eig(Tjac)));% define tolarance & errortolarance=1e-5;error=9e9;xold=[0;0;0];%iterative method[error_jacobi,jacobi_iter,Spectral_radius_jacobi]=jacobi_method(L,U,D,B)[error_gauss,gauss_iter,Spectral_radius_gauss]=gauss_method(L,U,D,B)[error_SOR,SOR_iter,Spectral_radius_SOR]=SOR_method(L,U,D,B)figure(1)plot(1:jacobi_iter-1,error_jacobi)title_text = sprintf('Jacobi Iteration Number = %d', jacobi_iter);title(title_text)xlabel('Iteration')ylabel('error')figure(2)plot(1:gauss_iter-1,error_gauss)title_text = sprintf('gauss Iteration Number = %d', gauss_iter);title(title_text)xlabel('Iteration')ylabel('error')figure(3)plot(1:SOR_iter-1,error_SOR)title_text = sprintf('SOR Iteration Number = %d', SOR_iter);title(title_text)xlabel('Iteration')ylabel('error')FUNCTION : JACOBI
function [error_jacobi,jacobi_iter,Spectral_radius_jacobi]=jacobi_method(L,U,D,B)%Defining the matrix A L= [0 0 0;-3 0 0;1 2 0];U =[0 1 2;0 0 4;0 0 0];D=[5 0 0 ;0 9 0;0 0 -7];A= L + U + D;B=[10;-14;33]; % define tolarance & error_jacobierror_jacobi=9e9;tolerance=1e-5;xold=[0;0;0];jacobi_iter=1;c=inv(D)*B;% Iterative matrixTjac=-inv(D)*((L+U));% spectral radiusSpectral_radius_jacobi=max(abs(real(eig(Tjac))));while (error_jacobi>tolerance) x=Tjac*xold +(c); error_jacobi(jacobi_iter) = max(abs(x-xold)); xold=x; jacobi_iter=jacobi_iter+1;endendFUNCTION GAUASS:
function [error_gauss,gauss_iter,Spectral_radius_gauss]=gauss_method(L,U,D,B)%Defining the matrix A L= [0 0 0;-3 0 0;1 2 0];U =[0 1 2;0 0 4;0 0 0];D=[5 0 0 ;0 9 0;0 0 -7];A= L + U + D;B=[10;-14;33]; % define tolarance & error_gausserror_gauss=9e9;tolerance=1e-5;xold=[0;0;0];gauss_iter=1;c=inv(D+L)*B;% Iterative matrixTgauss=-inv(D+L)*U;% spectral radiusEigen_value=eig(Tgauss);Spectral_radius_gauss=max(abs(real(Eigen_value)));while (error_gauss>tolerance) x=Tgauss*xold +(c); error_gauss(gauss_iter) = max(abs(x-xold)); xold=x; gauss_iter=gauss_iter+1;endend FUNCTION SOR:
function [error_SOR,SOR_iter,Spectral_radius_SOR]=SOR_method(L,U,D,B)%Defining the matrix A L= [0 0 0;-3 0 0;1 2 0];U =[0 1 2;0 0 4;0 0 0];D=[5 0 0 ;0 9 0;0 0 -7];A= L + U + D;B=[10;-14;33]; % define tolarance & error_sorerror_SOR=9e9;tolerance=1e-5;xold=[0;0;0];SOR_iter=1;omega=1.1;c=inv(D+omega*L)*B;% Iterative matrixTSOR=-inv(D+(omega*L))*(((1-omega)*L)+U);% spectral radiusSpectral_radius_SOR=max(abs(real(eig(TSOR))));while (error_SOR>tolerance) x=TSOR*xold + (c); error_SOR(SOR_iter) = max(abs(x-xold)); xold=x; SOR_iter=SOR_iter+1;endendRESULT
RESULT FOR GAUSS:
RESULT FOR SOR:
SPACRTRAL RADIUS: