Results 1 to 2 of 2
  1. #1
    manunited is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default ILL CONDITION MATRICES and gauss jordan elimination

    I have a problem with my output, here is the code that does gauss jordan elimination;
    I am having problems rounding to 7 decimal places, here is the code

    import java.text.DecimalFormat;
    public class GaussianElimination {
    private static final double EPSILON = 1e-7;

    // Gaussian elimination with partial pivoting
    public static double[] lsolve(double[][] A, double[] b) {
    int N = b.length;

    for (int p = 0; p < N; p++) {

    // find pivot row and swap
    int max = p;
    for (int i = p + 1; i < N; i++) {
    if (Math.abs(A[i][p]) > Math.abs(A[max][p])) {
    max = i;
    }
    }
    double[] temp = A[p]; A[p] = A[max]; A[max] = temp;
    double t = b[p]; b[p] = b[max]; b[max] = t;



    // pivot within A and b
    for (int i = p + 1; i < N; i++) {
    double alpha = A[i][p] / A[p][p];
    b[i] -= alpha * b[p];
    for (int j = p; j < N; j++) {
    A[i][j] -= alpha * A[p][j];
    }
    }
    }

    // back substitution
    double[] x = new double[N];
    for (int i = N - 1; i >= 0; i--) {
    double sum = 0.0;
    for (int j = i + 1; j < N; j++) {
    sum += A[i][j] * x[j];
    }
    x[i] = (b[i] - sum) / A[i][i];
    }
    return x;
    }


    // sample client
    public static void main(String[] args)
    {
    int N = 2;
    double[][] A = { { 2, 1 },
    { 3, 2 },
    };
    double[] b = { 18,30 };
    double[] x = lsolve(A, b);


    // print results
    for (int i = 0; i < N; i++)
    {
    DecimalFormat df = new DecimalFormat("#.#######");
    System.out.println(df.format(x[i]));
    }

    }

    }
    When i use the decimal format to round to 7 decimal places, it instead tound to a whole number, for example, the answer/output is (6,6), but i want it to truncate to 7 decimal places as 6.0000000,5.9999999

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: ILL CONDITION MATRICES and gauss jordan elimination

    I believe it is rounding it up because of the 9s after the 7th decimal. Perhaps try flooring it at the 7th spot before you format it.

    If you insert something like x[i] -= 0.00009; before the print statement, it may help you see it clearer.


    Also, please surround your code with [code][/code] tags.

Similar Threads

  1. Help with arrays and matrices!
    By chwex in forum New To Java
    Replies: 2
    Last Post: 02-15-2013, 12:34 AM
  2. Help with arrays and matrices!
    By chwex in forum New To Java
    Replies: 3
    Last Post: 02-13-2013, 11:46 AM
  3. Arrays and Matrices
    By Shaeman111 in forum New To Java
    Replies: 2
    Last Post: 11-10-2012, 07:24 AM
  4. Read two matrices from file
    By zenitis in forum New To Java
    Replies: 13
    Last Post: 05-20-2010, 03:03 AM
  5. How to multiply two matrices
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •