Understanding linear systems
Understanding linear systems
Aim:-To understand the linear systems by solving linear equation by using different iterative techniques.
Theory:-Matlab can solve the linear systems very easily by using inbuilt function,but in this case we are using iterative techniques to solve the linear systems to understand the linear systems and also to check the consistency of iterative techniques.
Following are the generalised equation for different iterative techniques that we will solve by using matlab:-
JACOBI METHOD:-
x^(k+1)=D^(−1)[(−L−U)x^(k)+B]
GAUSS SEIDAL METHOD:-
x^(k+1)=(L+D)^−1[−Ux^k+B]
SOR METHOD:-
x^(k+1)=(L+1/w D)^−1[(1/wD−D−U)x^k+B]
Now,to understand the linear systems and consistency of iterative techniues we will take a matrix as follows and then determine some parameters which as follows:-
A=[5 1 2 ; -3 9 4; 1 2 -7]
X=[x1;x2;x3]
B=[10 -14 33]
Parameters
- Eigen values of iteration matrix.
- Spectral radius of iteration matrix.
- Solve the system using Jacobi, Gauss-seidel and SOR methods
- Compute the iteration Matrix for the above mentioned iterative methods.
- Increasing the Spectral radius to 1.1 by changing the diagonal terms suitably.
MATLAB CODE:-Following code is the main code in which we have follow the following steps
- First,we have decompose the matrix A and then assign the matrix B and range of magnification factor by using linspace command.
- In the step 2,we have computed iteration matrix,eigen value and spectral radius for different itertive method.
- Then we have called different iterative method,by using function calling.After then results have been plotted.
Main code:-
clear all
close all
clc
%A=[5 1 2;-3 9 4;1 2 -7]
%Decomposition of matrix A
L=[0 0 0;3 0 0;1 2 0];
D1=[5 0 0;0 9 0;0 0 -7];
U=[0 1 2;0 0 4;0 0 0];
%coefficient of matrix A
%A=L+D+U;
%R.H.S matrix
B=[10;-14;33];
w=1.25;
mag_fac=linspace(0.5,5,10)
for i=1:length(mag_fac)
D=D1*mag_fac(i);
%Computing iteration matrix for jacobi,gauss_seidal and sor
T_jac=inv(D)*(L+U);
T_gs=inv(D+L)*U;
T_sor=inv(D+w*L)*((1-w)*D-w*U);
%Computing eigen_value of iteration matrix for jacobi,gauss_seidal and sor
eigen_value_jac=roots(poly(T_jac));
eigen_value_gs=roots(poly(T_gs));
eigen_value_sor=roots(poly(T_sor));
%Computing spectral_radius of iteration matrix for jacobi,gauss_seidal and sor
spectral_radius_jac(i)=max(abs(eigen_value_jac))
spectral_radius_gs(i)=max(abs(eigen_value_gs));
spectral_radius_sor(i)=max(abs(eigen_value_sor));
%Calling iterative method
[jac_iter(i),x_jac]=jacobi(D,L,U,B);
[gs_iter(i),x_gs]=gauss_seidal(D,L,U,B);
[sor_iter(i),x_sor]=sor(D,L,U,B,w);
endfor
%Plotting for jacobi
figure(1)
subplot(2,1,1)
hold all
plot(mag_fac,spectral_radius_jac,'-o','markerfacecolor','r','markeredgecolor','r','color','c','linewidth',2)
xlabel('mag_fac')
ylabel('spectral_radius_jac')
title('JACOBI METHOD')
grid on
subplot(2,1,2)
plot(spectral_radius_jac,jac_iter,'color','c','linewidth',2)
xlabel('spectral_radius_jac')
ylabel('jac_iter')
title('JACOBI METHOD')
grid on
figure(2)
subplot(2,1,1)
hold all
plot(mag_fac,spectral_radius_gs,'-o','markerfacecolor','r','markeredgecolor','r','color','g','linewidth',2)
xlabel('mag_fac')
ylabel('spectral_radius_gs')
title('GAUSS SEIDAL METHOD')
grid on
subplot(2,1,2)
plot(spectral_radius_gs,gs_iter,'color','g','linewidth',2)
xlabel('spectral_radius_gs')
ylabel('gs_iter')
title('GAUSS SEIDAL METHOD')
grid on
figure(3)
subplot(2,1,1)
hold all
plot(mag_fac,spectral_radius_sor,'-o','markerfacecolor','r','markeredgecolor','r','color','bl','linewidth',2)
xlabel('mag_fac')
ylabel('spectral_radius_sor')
title('SOR METHOD')
grid on
subplot(2,1,2)
plot(spectral_radius_sor,sor_iter,'color','bl','linewidth',2)
xlabel('spectral_radius_jac')
ylabel('sor_iter')
title('SOR METHOD')
grid on
Function code:-Following are three different function for different iterative techniques in each techniques we have solve the equation respect to their techniques by using while loop.Basically,while loop will solve the equation again and again till the we get converge result.
JACOBI CODE:-
function [jac_iter,x_jac]=jacobi(D,L,U,B)
error=10e9;
tol=1e-3;
x_old=[0;0;0];
jac_iter=1;
while(error>tol)
x_jac=inv(D)*(-L-U)*x_old+inv(D)*B;
error=max(abs(x_old-x_jac));
x_old=x_jac;
jac_iter=jac_iter+1
end
end
GAUSS SEIDAL CODE:-
function [gs_iter,x_gs]=gauss_seidal(D,L,U,B)
error=9e9;
tol=1e-4;
x_old=[0;0;0];
gs_iter=1;
while(error>tol)
x_gs=(inv(D+L))*(-U*x_old+B);
error=max(abs(x_old-x_gs));
x_old=x_gs;
gs_iter=gs_iter+1;
end
end
SUCCESSIVE OVER RELAXATION CODE:-
function [sor_iter,x_sor]=sor(D,L,U,B,w)
error=9e9;
tol=1e-4;
x_old=[0;0;0];
sor_iter=1;
while(error>tol)
x_sor=inv(D-w*L)*((1-w)*D+w*U)*x_old+w*inv(D-w*L)*B;
error=max(abs(x_old-x_sor));
x_old=x_sor;
sor_iter=sor_iter+1
end
end
Plots for jacobi method:-
Plots for gauss seidal method:-
Plots for sor method:-
Conclusion:-
- The above graph shows the relation between diagonal magnification vs spectral radius and spectral radius vs number of iterations.Each comparsion is made for three iterative techniques.
- From the graphs we can say that as the diagonal magnification increases,spectral radius decreases or we can say that they are inversely proportional to each other.
- And aslo as spectral radius increases number of iteration increases,and the result get converge.So, we can say that spectral radius and number of iteration directly proportional to each other.