implement the NAiVE-GAUSS method for solving the following NxN = 4x4 (unsymmetri
ID: 3605863 • Letter: I
Question
implement the NAiVE-GAUSS method for solving the following NxN = 4x4 (unsymmetrical) Simultaneous Linear Equations (SLE) [A] * {x) {b) Where 0 [A] = -1 and bj Your submitted computer program(s) and its output should have the following information (b) All computer program(s)' output should print all given/input information, such as the information shown in Eqs. (2) (c) Print out (on hard paper) the “UPPER triangular" matrix, and the modified/updated right-hand-side (RHS) vector b, at the end of Phase I of Naive-Gauss method. (d) Print out (on hard paper) the “BACK-substitution" for the solution vector {x), at the end of Phase II of Naive- Gauss (e) Print out (on hard paper) ALL the "multiplier factors' "nag (f) Based on your computer program's outputs/results obtained by Naive-Gauss method, please print out (on hard paper) the matrices L-Lower triangular matrix, and IU]-Upper triangular matrix, that can be utilized in the L U method (g) The list of your computer program(s)' source code must be printed/included, and your computer program should be able to solve SLE with “ any size, say = N ” for the unknown vectorx)Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n,i,j,k;
cout.precision(4); //set precision
cout.setf(ios::fixed);
cout<<" Enter the no. of equations ";
cin>>n; //input the no. of equations ( this answers part g of question)
float a[n][n+1],x[n],b[n][n+1]={0}; //declare an array to store the elements of augmented-matrix
cout<<" Enter the elements of the augmented-matrix row-wise: "; //Enter values given in equation 2
for (i=0;i<n;i++)
for (j=0;j<=n;j++)
cin>>a[i][j];
for (i=0;i<n;i++) //print the matrix given in equation 2 (this answers part b of question)
{
for (j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<" ";
}
for (i=0;i<n;i++) //Partial Pivotisation
for (k=i+1;k<n;k++)
if (abs(a[i][i])<abs(a[k][i]))
for (j=0;j<=n;j++)
{
double temp=a[i][j];
a[i][j]=a[k][j];
a[k][j]=temp;
}
cout<<" The matrix after Pivotisation is: ";
for (i=0;i<n;i++) //print the new matrix
{
for (j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<" ";
}
for (i=0;i<n-1;i++) //loop to perform the gauss elimination
for (k=i+1;k<n;k++)
{
double t=a[k][i]/a[i][i];
if (t!=0)
{
cout<<" Magic number "<<t<<"is multiplied to each column in Row number "<<k; // (this answers part e)
}
for (j=0;j<=n;j++)
{
a[k][j]=a[k][j]-t*a[i][j]; //make the elements below the pivot equal to zero (Upper triangular matrix)
if(a[k][j]==0) // elements in lower traingular matrix b that are replaced by the multipliers
b[k][j]=t;
}
}
cout<<" The upper triangular matrix is as follows: ";
for (i=0;i<n;i++) //Upper traingular matrix which can be used in LU method(this answers part c and part f)
{
for (j=0;j<=n;j++)
cout<<a[i][j]<<setw(16);
cout<<" ";
}
for (i=n-1;i>=0;i--) //back-substitution
{ //x is an array whose values correspond to the values of x,y,z..
x[i]=a[i][n]; //make the variable to be calculated equal to the rhs of the last equation
for (j=i+1;j<n;j++)
if (j!=i) //then subtract all the lhs values except the coefficient of the variable which is being calculated
x[i]=x[i]-a[i][j]*x[j];
x[i]=x[i]/a[i][i]; //now finally divide the rhs by the coefficient of the variable to be calculated
}
cout<<" The values of the variables are as follows: ";
for (i=0;i<n;i++)
cout<<x[i]<<endl; // Print the values of x, y,z,.... vector {x} ( this answers part d of question)
for(i=0;i<n;i++) //making the diagonal of Lower triangular matrix as 1
{
for (j=0;j<=n;j++)
if(i==j)
b[i][j]=1;
}
cout<<" The Lower traingular matrix used for LU method is: ";
for (i=0;i<n;i++) //print the Lower triangular matrix b ( this answers part f of question)
{
for (j=0;j<=n;j++)
cout<<b[i][j]<<setw(16);
cout<<" ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.