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

C++ code please use the following code to solve this problem: To solve the n x n

ID: 3581364 • Letter: C

Question

C++ code

please use the following code to solve this problem:

To solve the n x n linear system
E1j:    a11 x1 + a12 x2 + … + a1n xn = b1
E2:    a21 x1 + a11 x2 + … + a2n xn = b2
..
En:    an1 x1 + an2 x2 + … + ann xn = bn

INPUT number of unknowns and equations n; augmented matrix A = (aij), that
is, where ai,n+1 = bi and where 1 <= i <= n and 1 ? j ? n + 1

OUTPUT solution x1, x2, … , xn or message that linear system has no unique
solution
*/

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const int MAXSIZE = 101;

int gaussian(int, double [][MAXSIZE+1], double []);

int main()
{
    int n;
    double a[MAXSIZE][MAXSIZE+1];   // coefficient 2D array (matrix)
    double x[MAXSIZE];              // solutions array
   
    // read number of unkowns and equations
    cout << "Enter number of linear equations (up to " << MAXSIZE-1 << "): ";
    cin >> n;
    while(n<1 || n>=MAXSIZE){
        cout << "Invalid number of equations, try again: "<<endl;
        cout << "Enter number of linear equations (up to " << MAXSIZE-1 << "): ";
        cin >> n;
    }
   
    // read coefficients - NOTE: will use indices 1 to n
    cout << " Input values of A and B for the equation A*X=B ";
    for(int i = 1; i<=n; ++i){
        cout << "Enter the coefficients for Eq.["<<i<<"] ";
        for(int j = 1; j<=n; ++j){
            cout << "A["<<i<<"]["<<j<<"]: ";
            cin >> a[i][j];
        }
        cout << "B["<<i<<"]: ";
        cin >> a[i][n+1];
    }
   
    // Call Gaussian Elimination and Back substitution
    int error = gaussian(n,a,x);
   
    // Step 11 If gaussian function successful, OUTPUT (xi , …, xn ) values
    //      from main function (see sample run)
    cout << showpoint ;
    if(!error){
        cout <<" Solution: ";
        for(int i = 1; i<=n; ++i){
            cout << "X["<<i<<"] = " << setw(10)<< x[i] << endl;
        }
    }
   
    return 0;
}

// Sawps two equations (swap their coefficients)
void swapEquations(int n, double a[][MAXSIZE+1], int i, int j)
{
    double t;
    for(int k=1; k<=n+1; ++k){
        t = a[j][k];
        a[j][k] = a[i][k];
        a[i][k] = t;
    }
}

//
// GAUSSIAN ELIMINATION AND BACK SUBSTITUTION METHOD
//

int gaussian(int n, double a[][MAXSIZE+1], double x[])
{
    // Step 1    For i = 1, …, n-1 do Step 2-4.     (Elimination Process.)
    for(int i=1; i<n; ++i){
       
        // Step 2 Search for the first integer p, from i and to n
        //    so that ap,i ? 0
        //    If no integer p is be found
        //    then OUTPUT (“no unique solution exists”) and STOP.
        int p = i;
        while(p<=n && a[p][i]==0)
            ++p;
        if(p>n){
            cout << "no unique solution exists (1) ";
            return 1;
        }
       
        // Step 3 If p > i then swap the equations for p and i (Ep) <-> (Ei).
        //      Do this by creating and calling a function that takes two arrays,
        //      and swap each of the elements of the arrays.
        if(p !=i ){
            swapEquations(n,a,p,i);
        }
       
        // Step 4 For j = i + 1 , … , n do Steps 5 and 6
        for(int j=i+1; j<=n; ++j){
           
            //      Step 5 Set m = aji/aii
            double m = a[j][i] / a[i][i];
           
            //      Step 6 Perform (Ej) = (Ej - mji Ei)
            //          (that is, ajk = ajk - mji aik for k = i up to n+1)
            for(int k=i; k<=n+1; ++k)
                a[j][k] = a[j][k] - m*a[i][k];
        }
    }
   
    // Step 7 If ann = 0 then OUTPUT (“no unique solution exists”)
    //      RETURN failure flag
    if(fabs(a[n][n]) < 1.E-16){     // this is used because of precision errors
        cout << "no unique solution exists (2) ";
        return 1;
    }
   
    // Step 8 Set xn = an,n+1 / ann.     (Start backward substitution.)
    x[n] = a[n][n+1]/a[n][n];
   
    // Step 9 For i = n-1, …, 1
    //      set xi = [ai,n+1 - ?( from j=i+1 to n) aij xj ] / aii
    for(int i=n-1; i>=1; --i){
        x[i] = a[i][n+1];
        for(int j=i+1; j<=n; ++j)
            x[i] -= a[i][j]*x[j];
        x[i] /= a[i][i];
    }
   
    // Step 10 RETURN success flag
    return 0;   // 0 means no errors
}


Thank you :)

Electrical Circuit An electrical circuit consists of electronic devices connected by wires that carry currents. Some of these devices are voltage sources (such as batteries) and resistors (such as light bulbs) that limits the flow of the current. Consider the circuit shown on the right containing only constant voltage sources 20 S2 12 V with known voltages in units of volts (V) and 10 2 6 V 15 2 resistors with known resistances in units of 40 S2 ohms (2). The currents li, n units of in amperes (A), are determined by the values 30 2 of the voltages and resistances and can be 8 V determined analytically 20 Q 45 S2 25 2 The solution of this circuit results in eight equations involving the eight currents. 14 -11 -13 0 1513 4012 1011 12 3015 15 2014 6 2516 2017 4012 -8 3015 45 8 25I 8 Problem 3.1 Find the values of the eight currents H, le. Show your results to at least three decimal places.

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int MAXSIZE = 101;
int gaussian(int, double [][MAXSIZE+1], double []);
int main()
{
int n;
double a[MAXSIZE][MAXSIZE+1]; // coefficient 2D array (matrix)
double x[MAXSIZE]; // solutions array
  
cout << "Enter number of linear equations (up to " << MAXSIZE-1 << "): ";
cin >> n;
while(n<1 || n>=MAXSIZE){
cout << "Invalid number of equations, try again: "<<endl;
cout << "Enter number of linear equations (up to " << MAXSIZE-1 << "): ";
cin >> n;
}
  

cout << " Input values of A and B for the equation A*X=B ";
for(int i = 1; i<=n; ++i){
cout << "Enter the coefficients for Eq.["<<i<<"] ";
for(int j = 1; j<=n; ++j){
cout << "A["<<i<<"]["<<j<<"]: ";
cin >> a[i][j];
}
cout << "B["<<i<<"]: ";
cin >> a[i][n+1];
}
  

int error = gaussian(n,a,x);
  

cout << showpoint ;
if(!error){
cout <<" Solution: ";
for(int i = 1; i<=n; ++i){
cout << "X["<<i<<"] = " << setw(10)<< x[i] << endl;
}
}
  
return 0;
}

void swapEquations(int n, double a[][MAXSIZE+1], int i, int j)
{
double t;
for(int k=1; k<=n+1; ++k){
t = a[j][k];
a[j][k] = a[i][k];
a[i][k] = t;
}
}

int gaussian(int n, double a[][MAXSIZE+1], double x[])
{

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

int p = i;
while(p<=n && a[p][i]==0)
++p;
if(p>n){
cout << "no unique solution exists (1) ";
return 1;
}

if(p !=i ){
swapEquations(n,a,p,i);
}
  

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

double m = a[j][i] / a[i][i];
  

for(int k=i; k<=n+1; ++k)
a[j][k] = a[j][k] - m*a[i][k];
}
}

if(fabs(a[n][n]) < 1.E-16){ // this is used because of precision errors
cout << "no unique solution exists (2) ";
return 1;
}
  

x[n] = a[n][n+1]/a[n][n];
  

for(int i=n-1; i>=1; --i){
x[i] = a[i][n+1];
for(int j=i+1; j<=n; ++j)
x[i] -= a[i][j]*x[j];
x[i] /= a[i][i];
}
}

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