Solving Gaussian Elimination with Zero Pivot

  • Thread starter philosafari
  • Start date
  • Tags
    Program
In summary, The code is for Gaussian Elimination and the user wants to adjust the algorithm to avoid division by zero error. They ask if there is another type of Gaussian Elimination they can look into. The code includes functions for matrix allocation, freeing, and printing, as well as solving the system of equations using back substitution. The user also mentions a specific condition they want to ensure in the algorithm.
  • #1
philosafari
1
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
  • #2
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.
 

1. What is Gaussian Elimination with Zero Pivot?

Gaussian Elimination with Zero Pivot is a method used in linear algebra to solve systems of linear equations. It involves transforming a matrix into row-echelon form and then back-substituting to find the values of the variables.

2. When is Gaussian Elimination with Zero Pivot used?

Gaussian Elimination with Zero Pivot is used when solving systems of linear equations that have a leading coefficient of zero in one of the rows. This is known as a "zero pivot" and traditional Gaussian Elimination cannot be used in this case.

3. How does Gaussian Elimination with Zero Pivot work?

The process of Gaussian Elimination with Zero Pivot involves performing row operations on a matrix to transform it into row-echelon form. This includes swapping rows, multiplying rows by a scalar, and adding one row to another. Once in row-echelon form, the values of the variables can be determined through back-substitution.

4. What are the benefits of using Gaussian Elimination with Zero Pivot?

Gaussian Elimination with Zero Pivot allows for the solution of systems of linear equations that traditional Gaussian Elimination cannot solve. It also allows for the efficient and accurate computation of solutions for large systems of equations.

5. Can Gaussian Elimination with Zero Pivot be used for any type of matrix?

No, Gaussian Elimination with Zero Pivot can only be used on square matrices (matrices with an equal number of rows and columns) that have at least one leading coefficient of zero. It cannot be used on rectangular matrices or matrices without a leading zero pivot.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
749
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Replies
10
Views
951
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top