How to resolve the algorithm Gaussian elimination step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Gaussian elimination step by step in the C programming language

Table of Contents

Problem Statement

Solve   Ax=b   using Gaussian elimination then backwards substitution. A   being an   n by n   matrix. Also,   x and b   are   n by 1   vectors. To improve accuracy, please use partial pivoting and scaling.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Gaussian elimination step by step in the C programming language

This C program implements the Gaussian elimination method to solve a system of linear equations. The main steps involved in the program are:

  1. Declaring and Initializing Variables: The program starts by declaring and initializing the necessary variables. It declares an array a to store the coefficients of the linear equations, an array b to store the constants, and an array x to store the solution. The program also declares integer variables i, j, col, row, max_row, and dia for use in the elimination process.

  2. Swapping Rows: The swap_row function is used to swap two rows in the augmented matrix [a|b]. It takes as input two row indices r1 and r2, the matrix a, the vector b, and the number of columns n. It swaps the corresponding rows in both a and b.

  3. Gaussian Elimination: The gauss_eliminate function performs the Gaussian elimination process to transform the augmented matrix into an upper triangular matrix. It iteratively eliminates non-zero elements below the main diagonal. Here's a breakdown of the elimination process:

    • For each diagonal element A(dia, dia), it finds the maximum absolute value in the column and swaps the corresponding row to make the maximum element the new diagonal element.
    • It then subtracts multiples of the current diagonal row from the rows below it to zero out the elements below the diagonal.
  4. Back Substitution: After the Gaussian elimination process, the system of equations is in upper triangular form. The gauss_eliminate function performs back substitution to solve for the unknowns x. It iterates from the last row to the first row, solving for each variable in terms of the remaining variables.

  5. Printing Results: Finally, the program prints the solution values stored in the x array.

In summary, this program demonstrates a step-by-step implementation of the Gaussian elimination method to solve a system of linear equations. It involves row swapping, elimination, and back substitution to transform the augmented matrix into an upper triangular form and then solve for the solution.

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define mat_elem(a, y, x, n) (a + ((y) * (n) + (x)))

void swap_row(double *a, double *b, int r1, int r2, int n)
{
	double tmp, *p1, *p2;
	int i;

	if (r1 == r2) return;
	for (i = 0; i < n; i++) {
		p1 = mat_elem(a, r1, i, n);
		p2 = mat_elem(a, r2, i, n);
		tmp = *p1, *p1 = *p2, *p2 = tmp;
	}
	tmp = b[r1], b[r1] = b[r2], b[r2] = tmp;
}

void gauss_eliminate(double *a, double *b, double *x, int n)
{
#define A(y, x) (*mat_elem(a, y, x, n))
	int i, j, col, row, max_row,dia;
	double max, tmp;

	for (dia = 0; dia < n; dia++) {
		max_row = dia, max = A(dia, dia);

		for (row = dia + 1; row < n; row++)
			if ((tmp = fabs(A(row, dia))) > max)
				max_row = row, max = tmp;

		swap_row(a, b, dia, max_row, n);

		for (row = dia + 1; row < n; row++) {
			tmp = A(row, dia) / A(dia, dia);
			for (col = dia+1; col < n; col++)
				A(row, col) -= tmp * A(dia, col);
			A(row, dia) = 0;
			b[row] -= tmp * b[dia];
		}
	}
	for (row = n - 1; row >= 0; row--) {
		tmp = b[row];
		for (j = n - 1; j > row; j--)
			tmp -= x[j] * A(row, j);
		x[row] = tmp / A(row, row);
	}
#undef A
}

int main(void)
{
	double a[] = {
		1.00, 0.00, 0.00,  0.00,  0.00, 0.00,
		1.00, 0.63, 0.39,  0.25,  0.16, 0.10,
		1.00, 1.26, 1.58,  1.98,  2.49, 3.13,
		1.00, 1.88, 3.55,  6.70, 12.62, 23.80,
		1.00, 2.51, 6.32, 15.88, 39.90, 100.28,
		1.00, 3.14, 9.87, 31.01, 97.41, 306.02
	};
	double b[] = { -0.01, 0.61, 0.91, 0.99, 0.60, 0.02 };
	double x[6];
	int i;

	gauss_eliminate(a, b, x, 6);

	for (i = 0; i < 6; i++)
		printf("%g\n", x[i]);

	return 0;
}


  

You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the PL/I programming language
You may also check:How to resolve the algorithm Soundex step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Hoon programming language
You may also check:How to resolve the algorithm Array length step by step in the Potion programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the BCPL programming language