# Thread: ILL CONDITION MATRICES and gauss jordan elimination

1. Member
Join Date
Sep 2013
Posts
1
Rep Power
0

## 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. Senior Member
Join Date
Jan 2009
Location
CA, USA
Posts
271
Rep Power
9

## 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.