Ask Question
20 March, 19:42

Write a function that solves the matrix equation Ax = b using Gaussian Elimination (book section 6.2). 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.

+1
Answers (1)
  1. 20 March, 20:10
    0
    See explaination

    Step-by-step explanation:

    public class GaussElim{

    private static final double eps = 1e-10; % set epsilon value

    public static doublic[] fun (double[][] A, double[] b) {

    int n=b. length; %calculate length of vector b.

    for (int j=0; j
    int max=j; %find and swap pivot row.

    for (int i=j+1; i
    if (Math. abs (A[i][j]) >Math. abs (A[max][j])) {

    max=i;

    }

    }

    double[] t1 = A[j]; %swap

    A[j]=A[max];

    A[max]=t1;

    double t = b[j]; %swap

    b[j]=b[max];

    b[max]=t;

    if (Math. abs (A[j][j]) <=eps) {

    throw new ArithmeticException ("Matrix is singular."); % if matrix A is a singular matrix then throw error.

    }

    for (int i=j+1; i
    double alpha = A[i][j]/A[j][j];

    b[i]=b[i]-alpha*b[j];

    for (int k=j; k
    A[i][k]=A[i][k]-alpha*A[j][k];

    }

    }

    }

    double[] x=new double[n]; % back substitution starts here

    for (int i=n-1; i>=0; i--) {

    double sum=0.0;

    for (int j=i+1; j
    sum=sum+A[i][j]*x[j];

    }

    x[i] = (b[i]-sum) / A[i][i];

    }

    return x;

    }

    public static void main (String[] args) {

    int n=3;

    double[][] A={{1,2,1},{4,2,0},{-1,5,-3}};

    double[] b={5,3,21};

    double[] x=fun (A, b);

    for (int i=0; i
    StdOut. println (x[i]);

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a function that solves the matrix equation Ax = b using Gaussian Elimination (book section 6.2). Your function should accept as input ...” in 📗 Mathematics if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers