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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ILL CONDITION MATRICES

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Ill keep asking till i get an answer
    By iamgonge in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2013, 08:58 AM
  2. java help with inversing matrices
    By gspease839 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: February 9th, 2013, 08:34 AM
  3. Replies: 1
    Last Post: November 27th, 2012, 10:19 AM
  4. Solving linear equation systems including sparse matrices
    By GregXel in forum Java Theory & Questions
    Replies: 1
    Last Post: November 24th, 2012, 01:05 PM
  5. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 09:17 AM