This must be done in c You are to implement the Gaussian Eli

This must be done in c++.

You are to implement the Gaussian Elimination Algorithm to solve matrix problems provided in a data.txt file The format of the file should be as follows n A[n][n] b[n] that is for file 2 12 34 10 20 would represent A*x = b |1 2 3 4|* [x0 x1] = [10 20] Your solver should be designed to solve diagonally dominant matrices, so rotations are not required. You must use a dynamic matrix object. Your algorithm should take the provided matrix, reduce it to upper row echelon form, then compute the solution with a back-solver Then compute the magnitude of the residual of your solution. ||b - a*x||

Solution

#include <iostream>

#include <cmath>

#include <vector>

using namespace std;

void print(vector< vector<double> > A) {

    int n = A.size();

    for (int i=0; i<n; i++) {

        for (int j=0; j<n+1; j++) {

            cout << A[i][j] << \"\\t\";

            if (j == n-1) {

                cout << \"| \";

            }

        }

        cout << \"\ \";

    }

    cout << endl;

}

vector<double> gauss(vector< vector<double> > A) {

    int n = A.size();

    for (int i=0; i<n; i++) {

        // Search for maximum in this column

        double maxEl = abs(A[i][i]);

        int maxRow = i;

        for (int k=i+1; k<n; k++) {

            if (abs(A[k][i]) > maxEl) {

                maxEl = abs(A[k][i]);

                maxRow = k;

            }

        }

        // Swap maximum row with current row (column by column)

        for (int k=i; k<n+1;k++) {

            double tmp = A[maxRow][k];

            A[maxRow][k] = A[i][k];

            A[i][k] = tmp;

        }

        // Make all rows below this one 0 in current column

        for (int k=i+1; k<n; k++) {

            double c = -A[k][i]/A[i][i];

            for (int j=i; j<n+1; j++) {

                if (i==j) {

                    A[k][j] = 0;

                } else {

                    A[k][j] += c * A[i][j];

                }

            }

        }

    }

    // Solve equation Ax=b for an upper triangular matrix A

    vector<double> x(n);

    for (int i=n-1; i>=0; i--) {

        x[i] = A[i][n]/A[i][i];

        for (int k=i-1;k>=0; k--) {

            A[k][n] -= A[k][i] * x[i];

        }

    }

    return x;

}

int main() {

    int n;

    cin >> n;

    vector<double> line(n+1,0);

    vector< vector<double> > A(n,line);

    // Read input data

    for (int i=0; i<n; i++) {

        for (int j=0; j<n; j++) {

            cin >> A[i][j];

        }

    }

    for (int i=0; i<n; i++) {

        cin >> A[i][n];

    }

    // Print input

    print(A);

    // Calculate solution

    vector<double> x(n);

    x = gauss(A);

    // Print result

    cout << \"Result:\\t\";

    for (int i=0; i<n; i++) {

        cout << x[i] << \" \";

    }

    cout << endl;

}

This must be done in c++. You are to implement the Gaussian Elimination Algorithm to solve matrix problems provided in a data.txt file The format of the file sh
This must be done in c++. You are to implement the Gaussian Elimination Algorithm to solve matrix problems provided in a data.txt file The format of the file sh
This must be done in c++. You are to implement the Gaussian Elimination Algorithm to solve matrix problems provided in a data.txt file The format of the file sh

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site