You are on page 1of 1

function x = naive_gauss_elimination(A, b)

% Augmenting the matrix A with the column vector b


augmented_matrix = [A, b];

% Perform Gaussian Elimination


[rows, cols] = size(augmented_matrix);
for pivot = 1:min(rows, cols - 1)
% Make the pivot element equal to 1
augmented_matrix(pivot, :) = augmented_matrix(pivot, :) /
augmented_matrix(pivot, pivot);

% Eliminate other elements in the current column


for i = 1:rows
if i ~= pivot
factor = augmented_matrix(i, pivot);
augmented_matrix(i, :) = augmented_matrix(i, :) - factor *
augmented_matrix(pivot, :);
end
end
end

% Extract the solution vector from the last column


x = augmented_matrix(:, end);
end

call file
% Example system of linear equations
A = [2, -1, 1; -3, -1, 2; -2, 1, 2];
b = [8; -11; -3];

% Solve the system using Naive Gauss Elimination


solution = naive_gauss_elimination(A, b);

% Display the solution


disp('Solution:');
disp(solution);

You might also like