Experiment No.
4
Objective: Exercises to solve equations by Gauss elimination, Gauss Jordan Method
and Gauss Siedel in Scilab.
Equipment: A computer with Scilab installed.
Procedure:
Exercise 1: Gauss Elimination in Scilab
1. Formulate a system of linear equations with a 3x3 coefficient matrix and a 3x1
constant matrix.
2. Write a Scilab script to implement Gauss elimination for solving the system.
3. Execute the script and display the intermediate steps, including the transformed augmented
matrix and the final solution vector.
Code :
function x=gauss_elimination(A, b)
[n, m] = size(A);
if n ~= m then
disp("Matrix A is not square!");
return;
end
augmented_matrix = [A, b]; // Create augmented matrix [A|b]
// Forward elimination
for i = 1:n
// Pivoting (optional, but improves numerical stability)
[max_val, max_row] = max(abs(augmented_matrix(i:n, i)));
max_row = max_row + i - 1;
augmented_matrix([i, max_row], :) = augmented_matrix([max_row, i], :); // Swap rows
// Eliminate the variable below the pivot
for j = i+1:n
factor = augmented_matrix(j, i) / augmented_matrix(i, i);
augmented_matrix(j, :) = augmented_matrix(j, :) - factor * augmented_matrix(i, :);
end
end
// Back substitution
x = zeros(n, 1); // Initialize solution vector x
for i = n:-1:1
sum = 0;
// Perform the summation of the known x(j) values
for j = i+1:n
sum = sum + augmented_matrix(i, j) * x(j);
end
// Corrected indexing to get the last column (b values)
x(i) = (augmented_matrix(i, m + 1) - sum) / augmented_matrix(i, i);
end
end
// Example usage
A = [3, -2, 5; 1, 1, -3; 2, 3, 4];
b = [1; 12; 8];
x = gauss_elimination(A, b);
disp(x);
Output :
Exercise 2: Gauss-jordan method in Scilab
1. Create a new system of linear equations with a 4x4 coefficient matrix and a 4x1 constant
matrix.
2. Develop a Scilab script to apply the Gauss-Jordan method to solve the system.
3. Run the script and output the reduced row-echelon form and the solution vector.
Code :
function x=gauss_jordan(A, b)
[n, m] = size(A);
if n ~= m then
disp("Matrix A is not square!");
return;
end
augmented_matrix = [A, b]; // Create augmented matrix [A|b]
// Forward elimination to get to row echelon form (RREF)
for i = 1:n
// Pivoting (to improve numerical stability)
[max_val, max_row] = max(abs(augmented_matrix(i:n, i)));
max_row = max_row + i - 1;
augmented_matrix([i, max_row], :) = augmented_matrix([max_row, i], :); // Swap rows
// Normalize pivot row
augmented_matrix(i, :) = augmented_matrix(i, :) / augmented_matrix(i, i);
// Eliminate all other entries in the current column
for j = 1:n
if i ~= j
factor = augmented_matrix(j, i);
augmented_matrix(j, :) = augmented_matrix(j, :) - factor * augmented_matrix(i, :);
end
end
end
// The solution is now in the last column of the augmented matrix
x = augmented_matrix(:, m + 1);
end
// Example usage with new matrix
A = [2, 3, -1; 4, 1, 2; -2, 5, 3]; // Coefficient matrix
b = [1; 2; 3]; // Right-hand side vector
x = gauss_jordan(A, b); // Solve the system
disp(x); // Display the solution
Output :
Exercise 3: Gauss-Seidel Method in Scilab
1. Define a system of linear equations with a 3x3 coefficient matrix and a 3x1 constant
matrix.
2. Implement a Scilab script for the Gauss-Seidel iterative method.
3. Execute the script, providing an initial guess for the solution vector, and observe the
convergence of the method to the solution.
Code :
function x=gauss_seidel(A, b, tol, max_iter)
n = length(b); // Number of variables (size of the system)
x = zeros(n, 1); // Initial guess (x_0 = 0)
// Iterative process
for k = 1:max_iter
x_old = x; // Store old values of x to check convergence
for i = 1:n
sum = b(i); // Start with the right-hand side value
// Perform sum for the i-th row
for j = 1:n
if i ~= j
sum = sum - A(i, j) * x(j);
end
end
// Update the value of x_i
x(i) = sum / A(i, i);
end
// Check for convergence (if the change in x is less than the tolerance)
if norm(x - x_old, "inf") < tol // Use "inf" for infinity norm
disp("Convergence reached in " + string(k) + " iterations.");
return;
end
end
disp("Maximum iterations reached.");
end
// Example usage with new matrix
A = [4, 1, -1; 3, 5, 2; 2, 1, 3]; // Coefficient matrix
b = [3; 12; 7]; // Right-hand side vector
tol = 1e-6; // Convergence tolerance
max_iter = 100; // Maximum number of iterations
x = gauss_seidel(A, b, tol, max_iter); // Solve the system
disp(x); // Display the solution
Output :
Exercises:
1. Formulate a system of linear equations based on a real-world problem (e.g., a system of
linear equations representing a network or a physical system).
2. Choose an appropriate method (Gauss elimination, Gauss-Jordan, or Gauss-Seidel) to
.solve the system.
3. Implement the chosen method in Scilab and analyze the results in the context of the real-
world application.
Conclusion:
Reflect on the exercises, discuss the strengths and limitations of each method, and consider
the practical implications of using these techniques for solving linear systems. This set of
exercises will provide hands-on experience in implementing and comparing different
methods for solving linear equations using Scilab.