0% found this document useful (0 votes)
14 views5 pages

Experiment No 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Experiment No 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No: 06

Experiment Name : Solving system of linear equations by Jacobi and Gauss-Seidel method using
MATLAB.
Objectives:

• To understand the iterative methods for solving systems of linear equations.


• To implement the Jacobi and Gauss-Seidel methods using MATLAB.
• To compare the convergence behavior of both methods.

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:

Algorithm for Jacobi Method

1. Start with an initial guess for the solution vector x^(0)


2. Set the maximum number of iterations and a convergence tolerance.
3. For each iteration k=1,2,…
a. For each equation i=1 to n:
𝑛
xi ^(k)=(1/aii)(bi−∑𝑗=1,𝑗≠1 a𝑖𝑗 𝑥𝑗 (𝑘−1) )

b. Store all new xi^(k) values for the next iteration.

4. Check for convergence:


If ∥x^(k)−x^(k−1)∥ <tolerance, stop.
5. Repeat until convergence or the maximum number of iterations is reached.
Algorithm for Gauss-Seidel Method

1. Start with an initial guess for the solution vector x^(0)


2. Set the maximum number of iterations and a convergence tolerance.
3. For each iteration k=1,2,…
a. For each equation i=1 to n:
𝑛 𝑛
xi ^(k)=(1/aii)(bi−∑𝑗=1 a𝑖𝑗 𝑥𝑗 (𝑘) − ∑𝑗=𝑖+1 a𝑖𝑗 𝑥𝑗 (𝑘−1) )

b. Store all new xi^(k) values for the next iteration.

4. Check for convergence:


If ∥x^(k)−x^(k−1)∥ <tolerance, stop.
5. Repeat until convergence or the maximum number of iterations is reached.

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 iter = 1:20


x_old = x;
x_new = zeros(n, 1);

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.

You might also like