A2
A2
Lab Assignment # 2
Registration FA21-BEE-233
Number
Class BEE-7B
Script file of Gauss Seidel method:
function [x] = GaussSeidel(A, b, xo, N)
A = [5 -2 1; -3 10 -1; 1 1 5];
b = [-15; 45; -7];
xo = [0; 0; 0];
N = 15;
L = length(b);
x = xo;
for i = 1:N
for k = 1:L
x(k) = (b(k) - A(k, [1:k-1]) * x([1:k-1]) - A(k, [k+1:L]) * xo([k+1:L])) /
A(k, k);
end
xo = x
end
x
end
Output:
Explanation:
This MATLAB code implements the Gauss-Seidel iterative method to solve a
system of linear equations Ax=b. It starts with an initial guess xo = [0;0;0] and
refines the solution over 15 iterations. The input matrix A contains the coefficients
of the equations, b represents the constants on the right-hand side, and xo is the
initial guess for the solution. For each iteration, the code updates each variable x(k)
by solving the corresponding equation, using the most recently updated values for
the variables already calculated in the current iteration and the previous iteration's
values for the remaining variables. After updating all variables, the current solution
x is stored and used for the next iteration. The final result, x, gives an approximate
solution to the system of equations after the specified number of iterations.