# ILL CONDITION MATRICES

Printable View

• September 27th, 2013, 08:50 AM
soso84
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
• September 27th, 2013, 09:06 AM
KevinWorkman
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:

Code java:

```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?
• September 27th, 2013, 09:22 AM
soso84
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
• September 27th, 2013, 09:50 AM
KevinWorkman
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.
• September 27th, 2013, 09:54 AM
soso84
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