Write a user-defined MATLAB function that solves a system of n linear equations,
ID: 2084864 • Letter: W
Question
Write a user-defined MATLAB function that solves a system of n linear equations, [a] [x] =[b],
with the Gauss-Seidel iteration method. For the function name and arguments use x= GaSdl
(a, b) where a is the matrix of coefficients, b is the right-hand-side column of constants, and
x is the solution. Then, use the GaSdl function, with a zero starting guess, to solve the system:
9x1 - x2 + 2x3 = 6
-x1 + 11x2 - x3 + 3x4 = 28
x1 - x2 + 10x3 - x4 = 11
3x2 - x3 + 8x4 = 15
The decision to stop the iteration is based on the criterion of infinity norm of the residual : ||r(k) – r(k-1)|| < 10-3
The residual is defined as [rk] = [b]-[a] [xkNS] obtained from the numerical solution of XNS
after k iterations.
Explanation / Answer
function x=GaSdl(a ,b)
%function declaration is accrding to the question
%Here since a & b are already given in question
x1=0; x2=0; x3=0; x4=0;
%zero initial guess according to the question
% we now create variables to store data from iterations
k1=(b(1)-a(1,2)*x2-a(1,3)*x3-a(1,4)*x4)/a(1,1);
k2=(b(2)-a(2,1)*x1-a(2,3)*x3-a(2,4)*x4)/a(2,2);
k3=(b(3)-a(3,1)*x1-a(3,2)*x2-a(3,4)*x4)/a(3,3);
k4=(b(4)-a(4,1)*x1-a(4,2)*x2-a(4,3)*x3)/a(4,4);
%These are iterations for corresponding variables according to gauss siedel
%method
while abs(k1-x1)>10^(-3) && abs(k2-x2)>10^(-3) && abs(k3-x3)>10^(-3) && abs(k4-x4)>10^(-3)
%the condiition is set according to question to get accuracy upto 3rd
%decimal point
x1=k1; x2=k2; x3=k3; x4=k4;
%The values of variables are set to founnd data from previos iteration
k1=(b(1)-a(1,2)*x2-a(1,3)*x3-a(1,4)*x4)/a(1,1);
k2=(b(2)-a(2,1)*x1-a(2,3)*x3-a(2,4)*x4)/a(2,2);
k3=(b(3)-a(3,1)*x1-a(3,2)*x2-a(3,4)*x4)/a(3,3);
k4=(b(4)-a(4,1)*x1-a(4,2)*x2-a(4,3)*x3)/a(4,4);
%The iterations continue
end
x=[x1, x2,x3,x4 ];
Answers I got:
x1=1.02764092220515
x2=2.30797322092188
x3=-0.986596972283314
x4=0.887079033985658
Please let me know ,if any further doubts, in the comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.