Solving Gaussian Elimination with Zero Pivot

  • Thread starter Thread starter philosafari
  • Start date Start date
  • Tags Tags
    Program
Click For Summary
SUMMARY

This discussion focuses on modifying a Gaussian elimination algorithm to handle zero pivots, which currently leads to division by zero errors. The provided C++ code implements a basic Gaussian elimination method but lacks functionality to swap rows when a zero pivot is encountered. The suggested solution involves adding a mechanism to check for zero pivots and swap rows accordingly to avoid undefined behavior during calculations.

PREREQUISITES
  • Understanding of Gaussian elimination algorithm
  • Proficiency in C++ programming
  • Familiarity with matrix operations
  • Knowledge of numerical stability in algorithms
NEXT STEPS
  • Implement row swapping in Gaussian elimination to handle zero pivots
  • Explore numerical stability techniques in linear algebra
  • Learn about LU decomposition as an alternative to Gaussian elimination
  • Investigate error handling in C++ for robust matrix operations
USEFUL FOR

Mathematicians, software developers, and engineers working with numerical methods and linear algebra who need to enhance the robustness of Gaussian elimination algorithms.

philosafari
Messages
1
Reaction score
0
I found this code on my computer a while ago, and I want to adjust the algorithm so that I can have a zero pivot, whereas now the program will try to divide by zero and gives the #undef message that we all know and love. Is there another type of Gaussian Elimination that I can look into? I only include the source code so that you can see what algorithm I'm currently using.
Code:
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <new>
using namespace std;

double** matrixAlloc(int, int);
double* vectorAlloc(int);
void matrixFree(double**, int);
void vectorFree(double*);
void matrixPrint(double**, int);
void vectorPrint(double*, int);

int main()
{
  double** a;
  double* b;
  double x, sum, r;
  int n;

  cout <<  "******* gaussian elimination *******" << endl << endl;
  cout << "Input number of equations n=" << endl;
  cin >> n;

  a = matrixAlloc(n, n);
  b = vectorAlloc(n);

  cout << "Input matrix coefficients a(i,j)=" << endl;
  for ( int i = 0; i < n; i++)
    for ( int j = 0; j < n; j++)
      cin >> a[i][j];

  cout << "Input right-hand side vector b(i)" << endl;
  for (int i = 0; i < n; i++)
    cin >> b[i];

  cout << endl << endl;
  cout << "Coefficient matrix A:" << endl;
  matrixPrint(a, n);
  cout << "Right-hand-side vector b:" << endl;
  vectorPrint(b, n);

  // convert to upper triangular form
  for (int k=0;k<n-1;k++)
  {
    if ( fabs(a[k][k])>=1.e-6)
    {
      for (int i=k+1;i<n;i++)
      {
        x = a[i][k]/a[k][k];
        for (int j=k+1;j<n;j++) a[i][j] = a[i][j] -a[k][j]*x;
          b[i] = b[i] - b[k]*x;
      }
    }
  }

  // back substituti n
  b[n-1]=b[n-1]/a[n-1][n-1];
  for ( int i = n-2; i >= 0; i--)
  {
    sum = b[i];
      for (int j = i+1; j < n; j++)
        sum = sum - a[i][j]*b[j];
    b[i] = sum/a[i][i];
  }

  cout << "Solution:" << endl;
  vectorPrint(b, n);
  
  matrixFree(a, n);
  vectorFree(b);

  cout << "Input \'q\' to quit ...\n";
  cin >> r;
  return 0;
}

double** matrixAlloc(int n, int m)
{
  double** p; 
  
  try {
    p = new double* [n];
    for ( int i = 0; i < n; i++ )
        p[i] = new double[m];
  }
  catch (bad_alloc e) {
    cout << "Exception occurred: "
         << e.what() << endl;
  }

  return p;
} 

double* vectorAlloc(int n)
{
  double* p;

  try {
    p = new double[n];
  }
  catch (bad_alloc e) {
    cout << "Exception occurred: "
         << e.what() << endl;
  }

  return p;
}

void matrixFree(double** p, int n)
{
  for ( int i = 0; i < n; i++)
    delete[] p[i];
  delete[] p;
  p = 0;
}

void vectorFree(double* p)
{
  delete[] p;
  p = 0;
}

void matrixPrint(double** p, int n)
{
  for ( int i = 0; i < n; i++)
  {
    for ( int j = 0; j < n; j++)
      cout << setiosflags(ios::showpoint | ios::fixed | ios::right) 
           << setprecision(4) 
           << setw(12) << p[i][j];
    cout << endl;
  }
}

void vectorPrint(double* p, int n)
{
  for ( int i = 0; i < n; i++)
    cout << setiosflags(ios::showpoint | ios::fixed | ios::right) 
         << setprecision(4)
         << setw(12) << p[i];
  cout << endl << endl;
}
 
Physics news on Phys.org
In the nested loops following
// convert to upper triangular form
ensure, if possible, that the k'th entry in the k'th row is nonzero.
One way this could be done, if possible, is by swapping a pair of rows.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K