Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: ILL CONDITION MATRICES

1. ## ILL CONDITION MATRICES

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  Reply With Quote

3. ## Re: ILL CONDITION MATRICES

When posting code, please use the highlight tags. Also, code should be in the form of an SSCCE- you've got a bunch of extra stuff we don't need to see. Your problem can be boiled down to this code:

```import java.text.DecimalFormat;

public class Main{
public static void main(String[] args){

DecimalFormat df = new DecimalFormat("#.#######");
double d = 1.23456789;

String f = df.format(d);

System.out.println(f);
}
}```

This prints out 1.2345679. What would you like it to print out instead?  Reply With Quote

4. ## Re: ILL CONDITION MATRICES

I actually want to print the output of the sample i have , i.e the solution to the matrix where A=[2 1;3 2] and b=[18;30] and and A^-1*b=[6;6] but i want the answer to 7 decimal places.Thanks  Reply With Quote

5. ## Re: ILL CONDITION MATRICES

What number are you trying to print? Plug it into my example program without any of your extra code, and we'll go from there.  Reply With Quote

6. ## Re: ILL CONDITION MATRICES

This is the the question we are ask to do
The following systems of equations is easy to solve
2x+ y = 18
3x + 2y = 30
by graphing, by substitution, or elimination.The solution (6,6) is easily arrived at using a graphing calculator using matrix algebra to solve the matrix equation.
Ax=b
A= 2 1
3 2

b= 18
30

Ax=b so x=A^-1*b= 6,6

The literature contains numerous algorithms for solving ill conditioned systems.In order to illustrate the shortfalls of floating point arithmetic for this projects you will limited to algorithms implementing the gauss- jordan method.
A.) Write a java program that uses an algorithm implementing the Gauss- jordan method to solve the system of linear equations given in the introduction.Your output must show the output of every calculation to no more than 7 decimal places.You must create and use a function that cuts off the result of each calculation at exactly seven decimal places and uses this cutoff value in any susbsequesnt calculation.
B.) Use the following ten step procedure to solve the problem:
1)Problem
2)Input
3)Output
4)Discussion
5)Assumptions
6)Top Down Design
7)Variables
8)Pseudocode
9)Flowchart
10)a Java source code
b) program output  Reply With Quote