Experiment No 6
Experiment No 6
Experiment Name : Solving system of linear equations by Jacobi and Gauss-Seidel method using
MATLAB.
Objectives:
Theory:
A system of linear equations can be written in matrix form as Ax=b , where A is a coefficient
matrix, x is a vector of unknowns, and b is a constant vector. When direct methods like Gaussian
elimination are computationally expensive for large systems, iterative methods such as Jacobi and
Gauss-Seidel are preferred for solving such systems.
In the Jacobi method, each variable is updated independently using the values from the previous
iteration. It is simple and parallelizable but converges slowly and only under certain conditions
In the Gauss-Seidel method, each variable is updated immediately and reused in the next
computation step within the same iteration. This often results in faster convergence compared to
Jacobi, although both require a good initial guess and may not converge for all matrices.These
methods are implemented in MATLAB to explore their convergence behavior and effectiveness
in solving linear systems.
Algorithm:
MATLAB Code:
Jacobi Method:
A = [3, -0.1, -0.2; 0.1, 7, -0.3; 0.3, -0.2, 10];
b = [7.85; -19.3; 71.4];
x_ex = A\b;
disp('Exact solution using inverse matrix method:');
disp(x_ex');
n = length(b);
x = zeros(n, 1);
fprintf('\nJacobi Method:\n');
fprintf('Iteration\t x1\t\t x2\t\t x3\t\t Error\n');
for i = 1:n
sum_terms = A(i,1:i-1) * x_old(1:i-1) + A(i,i+1:n) * x_old(i+1:n);
x_new(i) = (b(i) - sum_terms) / A(i,i);
end
x = x_new;
error = norm(x - x_ex, inf);
fprintf('%d\t\t %.6f\t %.6f\t %.6f\t %.6e\n', iter, x(1), x(2), x(3), error);
if all(abs(x - x_ex) < 1e-5)
fprintf('\nJacobi method converged to exact solution after %d iterations.\n', iter);
break;
end
end
if iter == 20
fprintf('\nJacobi method did not converge to exact solution within %d iterations.\n', max_iter);
end
fprintf('\nVerification of solution:\n');
eq1 = 3*x(1) - 0.1*x(2) - 0.2*x(3);
eq2 = 0.1*x(1) + 7*x(2) - 0.3*x(3);
eq3 = 0.3*x(1) - 0.2*x(2) + 10*x(3);
fprintf('Equation 1: %.6f = 7.85\n', eq1);
fprintf('Equation 2: %.6f = -19.3\n', eq2);
fprintf('Equation 3: %.6f = 71.4\n', eq3);
Gauss-Seidel Method:
A = [3, -0.1, -0.2; 0.1, 7, -0.3; 0.3, -0.2, 10];
b = [7.85; -19.3; 71.4];
x_ex = A\b;
disp('Exact solution using inverse matrix method:');
disp(x_ex);
n = length(b);
x = zeros(n, 1);
fprintf('\nGauss-Seidel Method:\n');
fprintf('Iteration\t x1\t\t x2\t\t x3\t\t Error\n');
for iter = 1:20
x_old = x;
for i = 1:n
x(i) = (b(i) - (A(i,1:i-1) * x(1:i-1)) - (A(i,i+1:n) * x_old(i+1:n))) / A(i,i);
end
error = norm(x - x_ex, inf);
fprintf('%d\t\t %.6f\t %.6f\t %.6f\t %.6e\n', iter, x(1), x(2), x(3), error);
if all(abs(x - x_ex) < 1e-5)
fprintf('\nGauss-Seidel method converged to exact solution after %d iterations.\n', iter);
break;
end
end
if iter == max_iter
fprintf('\nGauss-Seidel method did not converge to exact solution within %d iterations.\n',
max_iter);
end
fprintf('\nVerification of solution:\n');
eq1 = 3*x(1) - 0.1*x(2) - 0.2*x(3);
eq2 = 0.1*x(1) + 7*x(2) - 0.3*x(3);
eq3 = 0.3*x(1) - 0.2*x(2) + 10*x(3);
fprintf('Equation 1: %.6f = 7.85\n', eq1);
fprintf('Equation 2: %.6f = -19.3\n', eq2);
fprintf('Equation 3: %.6f = 71.4\n', eq3);
Output
Jacobi Method:
Gauss-Seidel Method:
Discussion:
In this experiment, a system of linear equations was solved using the Jacobi and Gauss-Seidel
methods in MATLAB. The results from both methods were obtained and compared. It was
observed that the Gauss-Seidel method converged faster than the Jacobi method. The solutions
were verified and found to be accurate. The effect of initial guesses and the importance of
convergence criteria were also noticed during the implementation.