0% found this document useful (0 votes)
38 views7 pages

Solving Linear Equations with MATLAB

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

Solving Linear Equations with MATLAB

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

Rajshahi University of Engineering & Technology

Department of Electrical & Electronic Engineering


Course No: EEE 3110
Course Title: Computational Methods in Engineering Sessional

Experiment No: 06
Experiment Name: Solving system of linear equations by Jacobi and Gauss-Seidel
method using MATLAB.
SUBMITTED BY SUBMITTED TO

Name: Al-Momin Hosen Sushanto Bosak


Roll: 2101102 Lecturer
Section: B Electrical & Electronic Engineering
Dept.: Electrical & Electronic Engineering Rajshahi University of Engineering &
Session: 2021-2022 Technology (RUET)

Date of Submission: 12/05/2025


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

Objectives:
 To understand how Jacobi and Gauss-Seidel methods solve systems of linear equations.
 To use MATLAB to apply both methods and solve example problems.
 To compare how fast each method gives the solution and how starting values affect the result.

Theory:
A system of linear equations can be written as Ax = b, where A is the matrix of coefficients, x is the
unknowns, and b is the constant vector. For small systems, direct methods like Gaussian elimination
work well, but for big systems, they are slow and need lots of memory. That’s why iterative methods
like Jacobi and Gauss-Seidel are used more. These methods start with a guess for the solution and
keep updating it step by step. Jacobi method updates all variables at once using the old values from
last step. On the other hand, Gauss-Seidel updates the values one by one and uses the new values as
soon as they are calculated, which makes it faster.

Both methods need the matrix A to be diagonally dominant or symmetric positive definite to make
sure they will give the right answer. Jacobi method is simple and easy to do in parallel, but it takes
more steps to reach the correct answer. Gauss-Seidel is a little bit faster because it uses the new
values directly in the same loop. In MATLAB, both methods can be written using loops, and we can
also check how many steps they take to reach close to the real answer. A good starting guess also
help in faster result.

Algorithm:
Jacobi Method
1. Given a system of linear equations Ax = b, where A is an n×n matrix and x, b are n×1
vectors.
2. Rearrange the equations to express each variable xi explicitly:
n
3. xi(k+1) = (1 / aii) * (b− ∑ aij x j k )
j=0 , j ≠ 1

4. Start with an initial guess x^(0).


5. Set iteration counter k = 0.
6. For each iteration k, update all xi using only the values from the previous iteration.
7. Check if the solution has converged by evaluating if the difference between successive
approximations is below a tolerance:
8. ||x^(k+1) - x^(k)|| < tolerance
9. Repeat steps until convergence or maximum iterations reached.

Gauss-Seidel Method
1. Given a system of linear equations Ax = b.
2. Rearrange the equations to express each variable xi explicitly:
n n
xi^(k) = (1 / aii) * (bi -∑ aij xj − ∑ aij xj ¿ ¿
k k−1
3.
j=1 j=i +1

4. Set iteration counter k = 1


5. For each iteration k, update xi sequentially using the most recent values as soon as they are
available.
6. Repeat the iteration until the solution satisfies the convergence criterion or reaches the
maximum number of iterations.

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');

max_iter = 20;
for iter = 1:max_iter
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 == max_iter
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);
max_iter = 20;

fprintf('\nGauss-Seidel Method:\n');
fprintf('Iteration\t x1\t\t x2\t\t x3\t\t Error\n');

for iter = 1:max_iter


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 lab, we solved a system of linear equations using Jacobi and Gauss-Seidel methods in
MATLAB. Both methods gave correct solutions when the matrix was diagonally dominant. We saw
that Gauss-Seidel converges faster because it uses updated values during each step, while Jacobi is
simpler but takes more steps. The choice of method depends on the matrix and how accurate we
want the answer. MATLAB helped us clearly see how both methods work. Also, we learned that the
first guess and matrix properties affect the result. These methods are useful for solving large
problems, and their result was very helpful for us. It give good understanding.

You might also like