0% found this document useful (0 votes)
2 views

Practical Work N°3(Solution)

The document provides practical work exercises for 2nd-year undergraduate students in the Computer Science department at the University of Algiers 1, focusing on direct methods for solving linear systems. It includes implementations of Cramer's Rule, Gaussian elimination, Gauss-Jordan elimination, and LU decomposition in MATLAB, along with testing scripts for each method. Each exercise contains detailed code and explanations for solving linear equations using different numerical methods.

Uploaded by

abdraoufsoualmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Practical Work N°3(Solution)

The document provides practical work exercises for 2nd-year undergraduate students in the Computer Science department at the University of Algiers 1, focusing on direct methods for solving linear systems. It includes implementations of Cramer's Rule, Gaussian elimination, Gauss-Jordan elimination, and LU decomposition in MATLAB, along with testing scripts for each method. Each exercise contains detailed code and explanations for solving linear equations using different numerical methods.

Uploaded by

abdraoufsoualmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

University of Algiers 1

Computer Science department


Numerical Methods Practical Work
2nd-year undergraduate students

Practical Work N°3 (Solution)


DIRECT METHODS FOR SOLVING LINEAR SYSTEMS
Exercise 1:
function x = cramer_solver(A, b)
% Check if the determinant of A is non-zero
detA = det(A);
if detA ~= 0

% Number of variables
n = length(b);
x = zeros(n, 1);

% Calculate each variable using Cramer's Rule


for i = 1:n
Ai = A;
Ai(:, i) = b; % Replace the i-th column with vector b
x(i) = det(Ai) / detA;
end
else
error('The system has no unique solution using Cramer’s Rule.');
end
end
% Testing the function Ex1 (New script)
A = [2 -1 3; 4 1 -2; -1 2 1];
b = [5; -1; 4];
x = cramer_solver(A, b);
disp(A);
disp(b);
disp('the solution is x=');
disp(x);

Exercise 2:
function x = gaussian_elimination(A, b)
% Combine A and b into an augmented matrix
Ab = [A b]

n = length(b);

% Forward elimination
for i = 1:n
% Partial pivoting
[~, maxIdx] = max(abs(Ab(i:n, i)))
maxIdx = maxIdx + i - 1
if maxIdx ~= i
% Swap rows
Ab([i, maxIdx], :) = Ab([maxIdx, i], :);
end
% Make upper triangular
for j = i+1:n

factor = Ab(j, i) / Ab(i, i);


Ab(j, :) = Ab(j, :) - factor * Ab(i, :);
end

1
University of Algiers 1
Computer Science department
Numerical Methods Practical Work
2nd-year undergraduate students

disp(Ab);
end
AA=Ab(1:n,1:n);
bb=Ab(:,n+1);
disp(AA);
disp(bb);
% Back substitution
x = zeros(n, 1);
for i = n:-1:1
x(i)=(b(i)-AA(i,:)*x)/AA(i,i);
% or x(i) = (Ab(i, end) - Ab(i, 1:n) * x) / Ab(i, i);
fprintf('x%d = %f\n',i,x(i));
end
disp('The solution X = ');
disp(x);

% Testing the function Ex2 (New script)


A = [2 3 4 3; 9 10 5 7; 6 7 5 4; 10 9 2 10];
b = [-8; -20; -10; 6];
x = gaussian_elimination(A, b);

disp(A);
disp(b);
disp('the solution is x=');
disp(x);

Exercise 3:
function x = gauss_jordan_solver(A, b)
% Combine A and b into an augmented matrix
Ab = [A b];
n = length(b);

% Gauss-Jordan elimination
for i = 1:n
% Partial pivoting

[~, maxIdx] = max(abs(Ab(i:n, i)));


maxIdx = maxIdx + i - 1;
if maxIdx ~= i
% Swap rows
Ab([i, maxIdx], :) = Ab([maxIdx, i], :);
end

% Make the diagonal element 1


Ab(i, :) = Ab(i, :) / Ab(i, i);

% Make all other elements in column i zero


for j = 1:n
if j ~= i
Ab(j, :) = Ab(j, :) - Ab(j, i) * Ab(i, :);
end
end
end

% Extract the solution vector

2
University of Algiers 1
Computer Science department
Numerical Methods Practical Work
2nd-year undergraduate students

x = Ab(:, end);
end
% Testing the function Ex3 (New script)
A = [2 1 -1; -3 -1 2; -2 1 2];
b = [8; -11; -3];
x = gauss_jordan_solver(A, b);

disp(A);
disp(b);
disp('the solution is x=');
disp(x);

Exercise 4:
function x = lu_solver(A, b)
% LU decomposition
[L, U, P] = lu(A);
b=P*b;
% Solve L*y = b for y (forward substitution)
y = zeros(size(b));
n = length(b);
for i = 1:n
y(i) = (b(i) - L(i,:) *y) / L(i, i);
end

% Solve U*x = y for x (back substitution)


x = zeros(size(b));
for i = n:-1:1
x(i) = (y(i) - U(i,:) * x) / U(i, i);
end
end

% Testing the function Ex4 (New script)


A = [4 3 0 0; 3 4 -1 0; 0 -1 4 3; 0 0 3 4];
b = [24; 30; -24; -24];
x = lu_solver(A, b);

disp(A);
disp(b);
disp('the solution is x=');
disp(x);

You might also like