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

solve using c/c++ or matlab //this code could be modified to solve it #include <

ID: 1846090 • Letter: S

Question

solve using c/c++ or matlab


                    //this code could be modified to solve it                 

                                     #include <stdlib.h>
                    #include <math.h>
                    #include <stdio.h>
                    
                    
                    
                    //solve the linear system of equations A * X = rhs by using the Gauss elimination technique
                    // A, rhs, and n are input variables
                    // n is the number of equations in the system
                    // X is the output vector
                    
                    void Solve(double **A, double *rhs, double *X, int n)
                    {
                    double **w = new double*[n];
                    for (int i = 0; i < n; i++) w[i] = new double[n];
                    int **index = new int*[n];
                    for (int i = 0; i < n; i++) index[i] = new int[3];
                    
                    int nv = 1;
                    int irow = 0;
                    int icol = 0;
                    double pivot;
                    
                    for (int i = 0; i < n; i++){
                    w[i][0] = rhs[i];
                    index[i][2] = 0;
                    }
                    
                    double determ = 1;
                    for (int i = 0; i < n; i++){
                    double big = 0;
                    for (int j = 0; j < n; j++){
                    if (index[j][2] != 1)
                    for (int k = 0; k < n; k++)
                    {
                    if (index[k][2] > 1)
                    {
                    printf("Error at Gauss-Jordan: Matrix is singular ");
                    exit(1);
                    }
                    if (index[k][2] < 1)
                    if (fabs(A[j][k]) > big)
                    {
                    irow = j;
                    icol = k;
                    big = fabs(A[j][k]);
                    }
                    }
                    }
                    
                    index[icol][2] += 1;
                    index[icol][0] = irow;
                    index[icol][1] = icol;
                    
                    if (irow != icol)
                    {
                    determ = -determ;
                    for (int l = 0; l < n; l++)
                    {
                    double temp = A[irow][l];
                    A[irow][l] = A[icol][l];
                    A[icol][l] = temp;
                    }
                    if (nv > 0)
                    for (int l = 0; l < nv; l++)
                    {
                    double temp = w[irow][l];
                    w[irow][l] = w[icol][l];
                    w[icol][l] = temp;
                    }
                    }
                    
                    pivot = A[icol][icol];
                    determ *= pivot;
                    A[icol][icol] = 1;
                    
                    for (int l = 0; l < n; l++)
                    A[icol][l] /= pivot;
                    if (nv != 0)
                    for (int l = 0; l < nv; l++)
                    w[icol][l] /= pivot;
                    for (int l1 = 0; l1 < n; l1++){
                    if (l1 != icol)
                    {
                    double t = A[l1][icol];
                    A[l1][icol] = 0;
                    for (int l = 0; l < n; l++)
                    A[l1][l] -= A[icol][l]*t;
                    if (nv != 0)
                    for (int l = 0; l < nv; l++)
                    w[l1][l] -= w[icol][l]*t;
                    }
                    }
                    }
                    
                    
                    for (int i = 0; i < n; i++){
                    int l = n-i-1;
                    if (index[l][0] != index[l][1]){
                    irow = index[l][0];
                    icol = index [l][1];
                    for (int k = 0; k < n; k++)
                    {
                    double temp = A[k][irow];
                    A[k][irow] = A[k][icol];
                    A[k][icol] = temp;
                    }
                    }
                    }
                    
                    for (int k = 0; k < n; k++)
                    if (index[k][2] != 1){
                    printf("Error at Gauss-Jordan: Matrix is singular ");
                    exit(1);
                    }
                    
                    for (int i = 0; i < n; i++)
                    X[i] = w[i][0];
                    
                    for (int i = 0; i < n; i++) delete [](w[i]);
                    for (int i = 0; i < n; i++) delete [](index[i]);
                    delete []w;
                    delete []index;
                    
                    }
                    
                    
                    /////////////////////////////////////////////////////////////////////////
                    // MAIN PROGRAM
                    /////////////////////////////////////////////////////////////////////////
                    
                    //Solve the following system of equations
                    // x + 2*y + 3*z = 1
                    // 3*x + 4*y + 5*z = 2
                    // 5*x + 6*y + 7*z = 3
                    int main()
                    {
                    int n = 3;
                    
                    //make some memory allocations
                    double **A = new double*[n];
                    for (int i = 0; i < n; i++) A[i] = new double[n];
                    double *rhs = new double[n];
                    double *res = new double[n];
                    
                    //set the matrix
                    for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                    A[i][j] = i+2*j+1;
                    
                    //set the right-hand-side
                    for (int i = 0; i < n; i++)
                    rhs[i] = i;
                    
                    //call function 'Solve' to solve the equations
                    Solve (A,rhs,res, n);
                    
                    //print the result on the screen
                    for (int i = 0; i < n; i++)
                    printf("res[%d] = %e ", i,res[i]);
                    
                    //delete the memory allocated at the beginning
                    for (int i = 0; i < n; i++) delete [](A[i]);
                    delete []A;
                    delete []rhs;
                    delete []res;
                    
                    return 0;
                    }

Solve the following system or equations by using a linear solver (variable x describe the potential in p-n junction am applied voltage of approximately 3 V)

Explanation / Answer

#include <stdlib.h>

#include <math.h>
#include <stdio.h>



//solve the linear system of equations A * X = rhs by using the Gauss elimination technique
// A, rhs, and n are input variables
// n is the number of equations in the system
// X is the output vector

void Solve(double **A, double *rhs, double *X, int n)
{
double **w = new double*[n];
for (int i = 0; i < n; i++) w[i] = new double[n];
int **index = new int*[n];
for (int i = 0; i < n; i++) index[i] = new int[3];

int nv = 1;
int irow = 0;
int icol = 0;
double pivot;

for (int i = 0; i < n; i++){
w[i][0] = rhs[i];
index[i][2] = 0;
}

double determ = 1;
for (int i = 0; i < n; i++){
double big = 0;
for (int j = 0; j < n; j++){
if (index[j][2] != 1)
for (int k = 0; k < n; k++)
{
if (index[k][2] > 1)
{
printf("Error at Gauss-Jordan: Matrix is singular ");
exit(1);
}
if (index[k][2] < 1)
if (fabs(A[j][k]) > big)
{
irow = j;
icol = k;
big = fabs(A[j][k]);
}
}
}

index[icol][2] += 1;
index[icol][0] = irow;
index[icol][1] = icol;

if (irow != icol)
{
determ = -determ;
for (int l = 0; l < n; l++)
{
double temp = A[irow][l];
A[irow][l] = A[icol][l];
A[icol][l] = temp;
}
if (nv > 0)
for (int l = 0; l < nv; l++)
{
double temp = w[irow][l];
w[irow][l] = w[icol][l];
w[icol][l] = temp;
}
}

pivot = A[icol][icol];
determ *= pivot;
A[icol][icol] = 1;

for (int l = 0; l < n; l++)
A[icol][l] /= pivot;
if (nv != 0)
for (int l = 0; l < nv; l++)
w[icol][l] /= pivot;
for (int l1 = 0; l1 < n; l1++){
if (l1 != icol)
{
double t = A[l1][icol];
A[l1][icol] = 0;
for (int l = 0; l < n; l++)
A[l1][l] -= A[icol][l]*t;
if (nv != 0)
for (int l = 0; l < nv; l++)
w[l1][l] -= w[icol][l]*t;
}
}
}


for (int i = 0; i < n; i++){
int l = n-i-1;
if (index[l][0] != index[l][1]){
irow = index[l][0];
icol = index [l][1];
for (int k = 0; k < n; k++)
{
double temp = A[k][irow];
A[k][irow] = A[k][icol];
A[k][icol] = temp;
}
}
}

for (int k = 0; k < n; k++)
if (index[k][2] != 1){
printf("Error at Gauss-Jordan: Matrix is singular ");
exit(1);
}

for (int i = 0; i < n; i++)
X[i] = w[i][0];

for (int i = 0; i < n; i++) delete [](w[i]);
for (int i = 0; i < n; i++) delete [](index[i]);
delete []w;
delete []index;

}


/////////////////////////////////////////////////////////////////////////
// MAIN PROGRAM
/////////////////////////////////////////////////////////////////////////

//Solve the following system of equations
// x + 2*y + 3*z = 1
// 3*x + 4*y + 5*z = 2
// 5*x + 6*y + 7*z = 3
int main()
{
int n = 3;

//make some memory allocations
double **A = new double*[n];
for (int i = 0; i < n; i++) A[i] = new double[n];
double *rhs = new double[n];
double *res = new double[n];

//set the matrix
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
A[i][j] = i+2*j+1;

//set the right-hand-side
for (int i = 0; i < n; i++)
rhs[i] = i;

//call function 'Solve' to solve the equations
Solve (A,rhs,res, n);

//print the result on the screen
for (int i = 0; i < n; i++)
printf("res[%d] = %e ", i,res[i]);

//delete the memory allocated at the beginning
for (int i = 0; i < n; i++) delete [](A[i]);
delete []A;
delete []rhs;
delete []res;

return 0;
}