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

WRITE A METHOD THAT TO IMPLEMENT BACK AND FORWARD SUBSTITUTION OF THE GUASSIAN M

ID: 3638301 • Letter: W

Question

WRITE A METHOD THAT TO IMPLEMENT BACK AND FORWARD SUBSTITUTION OF THE GUASSIAN METHOD

public Matrix Backsubstitution(Matrix A, Matrix b){

//Create a matrix to store our result


//Find the value for the last unknown


//Put the value of the last unknown in the last position of the result


//Substitute backward until I reach the first unknown



//Return the results of the back substitution
return result;

}

public Matrix Forwardsubstitution(Matrix A, Matrix b){

//Create a matrix to store our result


//You insert code here





//Return the results of the forward substitution
return result;
}

THIS IS THE REST OF THE CODE

package Solvers;
import java.util.Vector;
import Components.*;
import util.Matrix;

public class Solver {
Vector<CircuitElement> elements=new Vector<CircuitElement>();
int i_numNodes;
int i_numResistors=0;
int i_numIdealCurrentSource=0;
int i_numRealCurrentSource=0;
int i_numRealVoltageSource=0;
int i_numIdealVoltageSource=0;
int i_systemSize;

Matrix A ;
Matrix b ;
Matrix x;

public Solver(int nodes){
i_numNodes=nodes;
}

public void addResistor(int minus, int plus, double resistance, String name) {

LinearResistor newr = new LinearResistor(minus, plus, resistance,name);
elements.add(newr);
}

public void addIdealCurrentSource(int minus, int plus, double current, String name){

IdealCurrentSource newic = new IdealCurrentSource(minus, plus, current,name);
elements.add(newic);
}



public void printInfo() {
for (int i = 0; i < elements.size(); i++) {
((CircuitElement)(elements.elementAt(i))).printInfo();
}

}

public void extractUnknowns() {
for (int i = 0; i < elements.size(); i++) {
((CircuitElement)(elements.elementAt(i))).extractUnknowns(x);
}

}

public void printSolution() {
for (int i = 0; i < elements.size(); i++) {
((CircuitElement)(elements.elementAt(i))).printSolution();
}

}

public void createSystemMatrix(){

for (int i = 0; i < elements.size(); i++) {
if(((CircuitElement)(elements.elementAt(i))).getType().compareToIgnoreCase("Linear Resistor")==0)
{
i_numResistors++;
}
if(((CircuitElement)(elements.elementAt(i))).getType().compareToIgnoreCase("Ideal Current Source")==0)
{
i_numIdealCurrentSource++;
}
if(((CircuitElement)(elements.elementAt(i))).getType().compareToIgnoreCase("Real Current Source")==0)
{
i_numRealCurrentSource++;
}

if(((CircuitElement)(elements.elementAt(i))).getType().compareToIgnoreCase("Ideal Voltage Source")==0)
{
i_numIdealVoltageSource++;
}
if(((CircuitElement)(elements.elementAt(i))).getType().compareToIgnoreCase("Real Voltage Source")==0)
{
i_numRealVoltageSource++;
}
}

i_systemSize=i_numNodes-1;
A = new Matrix(i_systemSize, i_systemSize);
b = new Matrix(i_systemSize, 1);
x = new Matrix(i_systemSize, 1);

for (int i = 0; i < elements.size(); i++) {
((CircuitElement)elements.elementAt(i)).applyMatrixStamp(A,b);
}

System.out.println("Resistor"+" "+i_numResistors);
System.out.println("Ideal Current Source"+" "+i_numIdealCurrentSource);
System.out.println("Ideal Voltage Source"+" "+i_numIdealVoltageSource);
System.out.println("Real Current Source"+" "+i_numRealCurrentSource);
System.out.println("Real Voltage Source"+" "+i_numRealVoltageSource);

i_systemSize=A.getNumColumns();
System.out.println("i_systemSize"+" "+i_systemSize);
System.out.println(" ");
}


public void printSystemMatrix(){
System.out.println("A=");
A.printMatrix();
System.out.println("b=");
b.printMatrix();

}



public void SolveByGaussianElimination(){
Matrix Ab=new Matrix(A.getNumRows(), A.getNumColumns() + b.getNumColumns());

Ab=Matrix.GaussianElimination(A, b);
System.out.println("*********Using Gaussian Elimination***********");


System.out.println("new A=");
A=Ab.extractColumnWidth(Ab.getNumColumns()-1);
A.printMatrix();

System.out.println("new b=");
b=Ab.extractColumn(Ab.getNumColumns());
b.printMatrix();

x=Backsubstitution(A, b);
System.out.println("Solution x=");
x.printMatrix();


}

public void SolveByLUDecomposition(){
System.out.println("*********Using LU Decomposition***********");
Matrix L=new Matrix(i_numNodes-1, i_numNodes-1);
Matrix U=new Matrix(i_numNodes-1, i_numNodes-1);
Matrix y=new Matrix(i_numNodes-1, i_numNodes-1);


//Step 1: Factorizing the A matrix into a lower and upper triangle matrix
L=A.computeLUFactorization().extractLowerTriangularMatrix();
U=A.computeLUFactorization().extractUpperTriangularMatrix();



System.out.println("L=");
L.printMatrix();
System.out.println("U=");
U.printMatrix();


System.out.println("A=");
A.printMatrix();

System.out.println("LU=");
Matrix.multiplyMatrices(L,U).printMatrix();

// Step 2:Solve Ly=b for y using forward substitution
//You should insert code here



System.out.println("y=");
y.printMatrix();

//Step 3: Solve Ux=y for x using backward substitution
//You should insert code here


System.out.println("Solution x=");
x.printMatrix();
}

public Matrix Backsubstitution(Matrix A, Matrix b){

//Create a matrix to store our result


//Find the value for the last unknown


//Put the value of the last unknown in the last position of the result


//Substitute backward until I reach the first unknown



//Return the results of the back substitution
return result;

}

public Matrix Forwardsubstitution(Matrix A, Matrix b){

//Create a matrix to store our result


//You insert code here





//Return the results of the forward substitution
return result;
}
}

Explanation / Answer

/******************************************************************************/ /* Perform Gauss-Jordan elimination with row-pivoting to obtain the solution to * the system of linear equations * A X = B * * Arguments: * lhs - left-hand side of the equation, matrix A * rhs - right-hand side of the equation, matrix B * nrows - number of rows in the arrays lhs and rhs * ncolsrhs- number of columns in the array rhs * * The function uses Gauss-Jordan elimination with pivoting. The solution X to * the linear system winds up stored in the array rhs; create a copy to pass to * the function if you wish to retain the original RHS array. * * Passing the identity matrix as the rhs argument results in the inverse of * matrix A, if it exists. * * No library or header dependencies, but requires the function swaprows, which * is included here. */ // swaprows - exchanges the contents of row0 and row1 in a 2d array void swaprows(double** arr, long row0, long row1) { double* temp; temp=arr[row0]; arr[row0]=arr[row1]; arr[row1]=temp; } // gjelim void gjelim(double** lhs, double** rhs, long nrows, long ncolsrhs) { // augment lhs array with rhs array and store in arr2 double** arr2=new double*[nrows]; for (long row=0; row