Question

Implement this pseudo code with Java. n is the size of the matrix for an n...

Implement this pseudo code with Java.

n is the size of the matrix for an n x n matrix.

k is the pivot.

i is the equation being eliminated.

j is the term you're working with.

Gaussian Elimination with Scaled Partial Pivoting

// Forward Elimination

function SPPFwdElimination(coeff : array(n,n), const : vector(n), ind : vector(n))
  scaling := new vector(n) // vector of scaling factors

// Initialize index and scaling vectors
  for i = 1 to n
  smax := 0

  for j = 1 to n
  smax := max(smax,|coeff[i][j]|) // find coefficient with greatest absolute value
  end for

  scaling[i] := smax   
  end for

  for k = 1 to n - 1
  rmax := 0
  maxInd := k

  for i = k to n
  r := |coeff[ind[i]][k] / scaling[ind[i]]| // ratio of coefficient to scaling factor
  if (r > rmax) then
  rmax := r
  maxInd := i
  end if
  end for
  swap(ind[maxInd], ind[k])

  for i = k + 1 to n
  mult := coeff[ind[i]][k] / coeff[ind[k]][k]
  
  for j = k + 1 to n
coeff
[ind[i]][j] := coeff[ind[i]][j] - mult * coeff[ind[k]][j]
  end for

  const[ind[i]] := const[ind[i]] - mult * const[ind[k]]
  end for
  end for
end function

// Back Substitution

function SPPBackSubst(coeff : array(n,n), const : vector(n), sol : vector(n), ind : vector(n))
  sol[n] := const[ind[n]] / coeff[ind[n]][n]
  for i = n - 1 to 1
  sum := const[ind[i]]
  for j = i + 1 to n
  sum := sum - coeff[ind[i]][j] * sol[j]
  end for
  sol[i] := sum / coeff[ind[i]][i]
  end for
end function

// SPP Gaussian Algorithm

function SPPGaussian(coeff : array(n,n), const : vector(n))
  sol := new array(n,n)
  ind := new vector(n)

  for i = 1 to n
  ind[i] := i
  end for

  call SPPFwdElimination(coeff,const,ind)
  call SPPBackSubst(coeff,const,sol,ind)
end function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package chirag;
import java.util.*;
import java.lang.Math;

public class dog {
  
   public static void SPPFwdElimination(int [][]coeff,int b[],int [] ind,int n) {
       int scaling[] =new int[n+1];

               //Initialize index and scaling vectors
               for(int i=1;i<=n;i++) {
               int smax = 0;

               for (int j =1;j<=n;j++) {
               smax = Math.max(smax,Math.abs(coeff[i][j])); // find coefficient with greatest absolute value
               }

               scaling[i]=smax;   
               }

               for( int k=1;k<=n - 1;k++) {
               int rmax=0;
               int maxInd=k;

               for (int i =k;i<=n;i++) {
                  
               int r = Math.abs(coeff[ind[i]][k] / scaling[ind[i]]); // ratio of coefficient to scaling factor
               if (r > rmax) {
               rmax = r;
               maxInd= i;
               }
               }
               int temp=ind[maxInd];
               ind[maxInd]=ind[k];
               ind[k]=temp;

               for(int i = k + 1;i<=n;i++) {
               int mult = coeff[ind[i]][k] / coeff[ind[k]][k];

               for(int j = k + 1;j<=n;j++) {
               coeff[ind[i]][j] = coeff[ind[i]][j] - mult * coeff[ind[k]][j];
               }

               b[ind[i]] = b[ind[i]] - (mult*b[ind[k]]);
               }
               }
   }
   public static void SPPBackSubst(int [][]coeff,int b[],int []sol,int ind[],int n) {
       sol[n] = b[ind[n]] / coeff[ind[n]][n];
               for(int i = n - 1;i>=1;i--) {
               int sum = b[ind[i]];
               for(int j = i + 1;j<=n;j++) {
               sum= sum - coeff[ind[i]][j] * sol[j];
               }
               sol[i] = sum / coeff[ind[i]][i];
               }
   }
public static void SPPGaussian(int [][]coeff,int []b,int n ) {
   int []sol= new int [n+1];
       int ind[] = new int[n+1];

           for (int i = 1;i<n;i++) {
               ind[i]=i;
           }
           SPPFwdElimination(coeff,b,ind,n);
           SPPBackSubst(coeff,b,sol,ind,n);
}
   public static void main(String[] args) {
       int n;
       Scanner sc = new Scanner(System.in);
       n=sc.nextInt();
       int coeff[][]=new int[n+1][n+1];
       int []b=new int[n+1];
       for(int i=1;i<=n;i++) {
           for(int j=1;j<=n;j++) {
               coeff[i][j]=sc.nextInt();
           }
           b[i]=sc.nextInt();
       }
      
       SPPGaussian(coeff,b,n);
   }

}

Add a comment
Know the answer?
Add Answer to:
Implement this pseudo code with Java. n is the size of the matrix for an n...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function that solves the matrix equation Ax = b using Gaussian Elimination. Your function...

    Write a function that solves the matrix equation Ax = b using Gaussian Elimination. Your function should accept as input a n-by-n matrix A and an n-by-1 vector b, and it should produce a n-by-1 vector x that satisfies Ax = b. Gaussian Elimination has two parts: forwards elimination and backwards substitution. You'll need to use both to solve the problem. It's okay to rigidly follow the pseudocode in the book. Using C++ Don't just use a library call, even...

  • Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public...

    Using java fix the code I implemented so that it passes the JUnit Tests. MATRIX3 public class Matrix3 { private double[][] matrix; /** * Creates a 3x3 matrix from an 2D array * @param v array containing 3 components of the desired vector */ public Matrix3(double[][] array) { this.matrix = array; } /** * Clones an existing matrix * @param old an existing Matrix3 object */ public Matrix3(Matrix3 old) { matrix = new double[old.matrix.length][]; for(int i = 0; i <...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • The pscudocode shown below solves a system of n linear algebraic equations using Gauss-Jordan 125] elimination. DOFOR -1,n DOPOR 1 = k + 1,n + 1 END DO ae 1 DOFOR 1 = 1, n k THEN IF i DOFOR j- k+...

    The pscudocode shown below solves a system of n linear algebraic equations using Gauss-Jordan 125] elimination. DOFOR -1,n DOPOR 1 = k + 1,n + 1 END DO ae 1 DOFOR 1 = 1, n k THEN IF i DOFOR j- k+1,n+ 1 ENDDO END IF END DO END DO DOFOR m-1,n END DO Write a Matlab function program GaussJordan(A,n) which implements this algorithm and a) returns the solution. Here A is the augmented matrix consisting of the coefficient matrix...

  • When asked to describe an algorithm you are expected to give a clear pseudo-code description of...

    When asked to describe an algorithm you are expected to give a clear pseudo-code description of the algorithm 1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...

  • % Create a matrix which has 150 rows and 200 columns % Use the rand function...

    % Create a matrix which has 150 rows and 200 columns % Use the rand function to generate that matrix with radom values of entries % are between 0 and 1 a = rand(150,200); b = zeros(1,300); for i = 1:150     for j = 1:200         if rand() < 0.9 % 90% of rand() value less than 0.9             a(i,j) = 0; % Assign 0 for dead cells         else a(i,j) = 1; % Assign 1 for living cells...

  • definition of Markov matrix and related theorems are showed below 8.4.2Show that the matrix (8.4.21) is a Markov matrix which is not regular. Is A stable? Definition 8.7 Let A = (aij) A satisfies R(n...

    definition of Markov matrix and related theorems are showed below 8.4.2Show that the matrix (8.4.21) is a Markov matrix which is not regular. Is A stable? Definition 8.7 Let A = (aij) A satisfies R(n, n) so that aij-0 for i, j = I, . . . , n. If j-1 243 8.4 Markov matrices that is, the components of each row vector in A sum up to 1, then A is called a Markov or stochastic matrix. If there...

  • Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of...

    Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of bitmap (in bits) to be used */ int k, /* chunk length to be matched */ const char *qs, /* query docoument (X)*/ int m, /* query document length */ const char *ts, /* to-be-matched document (Y) */ int n /* to-be-matched document length*/) { /*if the to-be-matched document is less than k length, return false*/ if (n < k) return 0; /*start our...

  • MATLAB Write a code where the elements of a squared matrix size (order) n are equal...

    MATLAB Write a code where the elements of a squared matrix size (order) n are equal to the sum of its row and column position (I + j)

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT