Results 1 to 2 of 2
 09272013, 10:13 PM #1Member
 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 = 1e7;
// 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
 09282013, 04:35 PM #2Senior Member
 Join Date
 Jan 2009
 Location
 CA, USA
 Posts
 268
 Rep Power
 8
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

Help with arrays and matrices!
By chwex in forum New To JavaReplies: 2Last Post: 02152013, 01:34 AM 
Help with arrays and matrices!
By chwex in forum New To JavaReplies: 3Last Post: 02132013, 12:46 PM 
Arrays and Matrices
By Shaeman111 in forum New To JavaReplies: 2Last Post: 11102012, 08:24 AM 
Read two matrices from file
By zenitis in forum New To JavaReplies: 13Last Post: 05202010, 03:03 AM 
How to multiply two matrices
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:50 PM
Bookmarks