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

I\'m having trouble computing the magnitude of residual of solution (error vecto

ID: 3783819 • Letter: I

Question

I'm having trouble computing the magnitude of residual of solution (error vector) after gaussian elimination for matrices. My program is supposed to solve for matrices of the form A*x=b, where A and b are given. My code does this just fine, but I'm having trouble computing the error vector. I know the formula should be b-A*x, but I'm having trouble getting it right with the vectors I have. Any suggestions?

Here's my code:


#include <iostream>
#include <fstream>
#include <cmath>
#include <vector>
#include "assignment1.h"
using namespace std;

vector<double> gaussianElimination ( vector< vector<double> > A ) {
int n = A.size();

for ( int i = 0; i < n; i++ ) {
double maxElement = abs ( A[i][i] );
int maxRow = i;
for ( int k = i + 1; k < n; k++ ) {
if ( abs ( A[k][i] ) > maxElement ) {
maxElement = abs ( A[k][i] );
maxRow = k;
}
}

for ( int k = i; k < n + 1; k++ ) {
double tmp = A[maxRow][k];
A[maxRow][k] = A[i][k];
A[i][k] = tmp;
}

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];
}
}
}
}

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;
}

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] << " ";
outputFile << A[i][j] << " ";
if ( j == n - 1 ) {
cout << "| ";
outputFile << "| ";
}
}
cout << " ";
outputFile << " ";
}
cout << endl;
outputFile << endl;
}

int main() {
int n;
cin >> n;
vector<double> line ( n + 1, 0 );
vector< vector<double> > A ( n,line );

// Read data from the input file
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 the data from the input file
print(A);

//Compute the solution
vector<double> x ( n );
x = gaussianElimination ( A );


// Print the result to the screen and the output file
cout << "The unknown matrix: ";
outputFile << "The unknown matrix: ";
for ( int i = 0; i < n; i++ ) {
cout << "| " << x[i] << " | ";
outputFile << "| " << x[i] << " | ";
}
cout << endl;
outputFile << endl;

  

return 0;
}

Explanation / Answer

#include<iostream>

#define MAX 10

using namespace std;

int lcm(int x,int y);

int main()

{

int i,j,k,r,c,a[MAX][MAX],b[MAX][MAX],det=1,l,d1,d2;

cout<<" Enter the number of Rows of the Matrix: ";

cin>>r;

cout<<" Enter the number of Columns of the Matrix: ";

cin>>c;

if(r==c)

{

cout<<" Enter the Elements of the Matrix: ";

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

cin>>a[i][j];

}

}

for(i=0;i<r-1;i++)

{

for(j=i+1;j<r;j++)

{

l=lcm(a[i][i],a[j][i]);

if(l!=0&&(a[i][i]!=0&&a[j][i]!=0))

{

l=(a[i][i]*a[j][i])/l;

d1=l/a[i][i];

d2=l/a[j][i];

a[j][i]=0;

for(k=i+1;k<r;k++)

{

a[j][k]=(d2*a[j][k])-(d1*a[i][k]);

}

}

}

}

cout<<" The given Matrix after Gauss Elimination is: ";

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

cout<<a[i][j]<<" ";

}

cout<<" ";

}

}

else

{

cout<<" This is not a Square Matrix!!! ";

}

return 0;

}

int lcm(int x,int y)

{

int t;

while (y != 0)

{

t=y;

y=x%y;

x=t;

}

return x;

}

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