Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an expression \"solveGauss()\" and \"pivotRow()\" in order to solve any gi

ID: 3775712 • Letter: W

Question

Write an expression "solveGauss()" and "pivotRow()" in order to solve any given set of equations using Gaussian Elimination.

//==================================================================
// linAlgEq.cpp   
// methods for the class
//==================================================================

#include"linAlgEq.h"

using namespace std;

//------------------------------------------------------------------
// constructor
//------------------------------------------------------------------
LinAlgEq::LinAlgEq(int nn)
{
   initMatrices(nn);
}

//------------------------------------------------------------------
// destructor
//------------------------------------------------------------------
LinAlgEq::~LinAlgEq()
{
   freeMemory();
}

//------------------------------------------------------------------
// initMatrices: allocate memory and initialize with data
//------------------------------------------------------------------
void LinAlgEq::initMatrices(int nn)
{
   if (nn < 1) {
       cerr << "LinAlgEq: There must be a positive number of equations." << endl
           << " Setting to the default number: 3" << endl;
       nn = 3;
   }

   n = nn;
   nActive = nn;

   int i, j;
   M = new double*[n];
   A = new double*[n];
   for (i = 0; i<n; ++i) {
       M[i] = new double[n + 1];
       A[i] = new double[n];
       for (j = 0; j <= n; ++j) {
           if (i == j) {
               M[i][j] = 1.0;
               A[i][j] = 1.0;
           }
           else {
               M[i][j] = 0.0;
               if (j != n)
                   A[i][j] = 0.0;
           }
       }
   }

   B = new double[n];
   x = new double[n];
   for (i = 0; i<n; ++i) {
       B[i] = 0.0;
       x[i] = 0.0;
   }
}

//------------------------------------------------------------------
// freeMemory: frees up all allocated memory
//------------------------------------------------------------------
void LinAlgEq::freeMemory()
{
   int i;

   for (i = 0; i<n; ++i) {
       delete[](M[i]);
       delete[](A[i]);
   }
   delete[] M;
   delete[] A;

   delete[] B;
   delete[] x;
}

//------------------------------------------------------------------
// resize: resizes the system of equations
//------------------------------------------------------------------
void LinAlgEq::resize(int nn)
{
   freeMemory();
   initMatrices(nn);
}

//------------------------------------------------------------------
// setNumActiveEquations: sets number of active equations to solve
//------------------------------------------------------------------
void LinAlgEq::setNumActiveEquations(int jj)
{
   if (jj < 1 || jj > n) {
       cerr << "LinAlgEq::setNumActiveEquations: out of range "
           << jj << endl;
       return;
   }

   nActive = jj;
}

//------------------------------------------------------------------
// printSys: prints the system of equations to the screen
//------------------------------------------------------------------
void LinAlgEq::printSys() const
{
   cout << endl << "Coefficient Matrix" << endl;
   int i, j;

   for (i = 0; i<nActive; ++i) {
       for (j = 0; j<nActive; ++j) {
           cout << setw(8) << A[i][j] << " ";
       }
       cout << endl;
   }
   cout << endl << endl << "Right Hand Side" << endl;

   for (i = 0; i<nActive; ++i)
       cout << B[i] << endl;
   cout << endl;
}

//------------------------------------------------------------------
// setA: set element (i,j) of the coefficient matrix to value val
//------------------------------------------------------------------
void LinAlgEq::setA(int i, int j, double val)
{
   if (i<0 || i >= n || j<0 || j >= n) {
       cerr << "LinAlgEq::setA: Matrix indices out of bounds" << endl;
       return;
   }

   M[i][j] = val;
   A[i][j] = val;
}

//------------------------------------------------------------------
// setB: set element i of the right hand side
//------------------------------------------------------------------
void LinAlgEq::setB(int i, double val)
{
   if (i<0 || i >= n) {
       cerr << "LinAlgEq::setB: Vector indices out of bounds" << endl;
       return;
   }

   M[i][n] = val;
   B[i] = val;
}

//------------------------------------------------------------------
// getSol: retreives element i of the solution
//------------------------------------------------------------------
double LinAlgEq::getSol(int i) const
{
   if (i<0 || i >= nActive) {
       cerr << "LinAlgEq::getSol: Vector indices out of bounds" << endl;
       return(0.0);
   }

   return(x[i]);
}

//------------------------------------------------------------------
// restoreAugMat: when solveGauss is called, the augmented matrix
// is altered. This method restores the augmented matrix M to its
// original state, based on the values in matrices A and B.
//------------------------------------------------------------------
void LinAlgEq::restoreAugMat()
{
   int i, j;

   for (i = 0; i<n; ++i) {
       M[i][n] = B[i];
       x[i] = 0.0;
       for (j = 0; j<n; ++j)
           M[i][j] = A[i][j];
   }
}

//------------------------------------------------------------------
// solveGauss: Solve the first nActive equations for the first
// nActive unknowns by Gauss elimination
//------------------------------------------------------------------
void LinAlgEq::solveGauss()
{
  
}

//------------------------------------------------------------------
// pivotRow: perform partial (row) pivoting on the specified row
//------------------------------------------------------------------
void LinAlgEq::pivotRow(int j)

{
}

//------------------------------------------------------------------
// checkSol: Checks the solution to the system of equations by
// computing an L^2 relative error as follows. Let e = A*x - b.
// Then the relative error is the relative error is the L^2 norm
// of e divided by the L^2 norm of b.
//------------------------------------------------------------------
double LinAlgEq::checkSol() const
{
   double sum = 0.0;
   double sum2 = 0.0;
   double sumb = 0.0;
   int i, j;  

   for (i = 0; i<nActive; ++i) {
       sum = 0.0;
       for (j = 0; j<nActive; ++j) {
           sum += A[i][j] * x[j];
       }
       sum2 += (sum - B[i])*(sum - B[i]);
       sumb += B[i] * B[i];
   }
   return(sqrt(sum2 / sumb));

}

================================================================================================

//==================================================================
// app.cpp
// Application that tests objects of class LinAlgEq
//
//==================================================================

#include "linAlgEq.h"
#include <iostream>

using namespace std;

int main()
{
   LinAlgEq sys(4);

   sys.setA(0, 0, 3.1);
   sys.setA(0, 1, 2.8);
   sys.setA(0, 2, 1.9);
   sys.setA(0, 3, -5.5);
   sys.setA(1, 0, -3.3);
   sys.setA(1, 1, 9.1);
   sys.setA(1, 2, 102.2);
   sys.setA(1, 3, -1.1);
   sys.setA(2, 0, -85.1);
   sys.setA(2, 1, -1.0);
   sys.setA(2, 2, 3.0);
   sys.setA(2, 3, -0.2);
   sys.setA(3, 0, 1.1);
   sys.setA(3, 1, -7.2);
   sys.setA(3, 2, 8.0);
   sys.setA(3, 3, 0.3);

   sys.setB(0, 1.0);
   sys.setB(1, 2.0);
   sys.setB(2, 3.0);
   sys.setB(3, 4.0);

   int nEq = 4;
   sys.setNumActiveEquations(nEq);

   sys.printSys();

   sys.solveGauss();

   cout << "Solution:" << endl;
   for (int i = 0; i<nEq; ++i) {
       cout << sys.getSol(i) << endl;
   }

   cout << endl << "Relative error = " << sys.checkSol() << endl;


   cout << endl << "Press enter when finished." << endl;
   char cc = cin.get();

   return(0);
}

===============================================================================================================

//==================================================================
// linAlgEq.h   
// A class for representing and solving systems of linear algebraic
// equations: A x = B,
// where A is an nxn real matrix, B is an nx1 matrix.
//==================================================================

#pragma once

#include <iostream>
#include <iomanip>
#include <math.h>

class LinAlgEq {
public:
   LinAlgEq(int nn = 3);
   ~LinAlgEq();
   void setA(int i, int j, double val);
   void setB(int j, double val);
   void setNumActiveEquations(int jj);
   void solveGauss();
   double getSol(int i) const;
   void resize(int nn);
   void printSys() const;
   double checkSol() const;
   void restoreAugMat();
private:
   int n; // number of equations
   int nActive; // number of active equations
   double** M; // augmented system matrix
   double** A; // original A matrix
   double* B; // original B matrix
   double* x; // solution vector

   void initMatrices(int nn);
   void freeMemory();
   void pivotRow(int i);
};

Explanation / Answer

/************* Gauss elimination for solving linear equations *************/ #include #include #include #include int main() { int n,i,j,k,temp; float a[10][10],c,d[10]={0}; clrscr(); coutn; cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote