here i have 2 files
and im getting a wrong echlon form of the matrix...please help;

Linear_Equation.java:

package mypack;

import java.io.*;

public class Linear_Equation{
    public static void main(String args[])throws IOException{
        int i,j;
        matrix mat1=new matrix();
        int mtx[][]=new int[3][4];
        mat1.accept();
        mat1.display();
        mtx=mat1.echolonform();
        System.out.println("\nThe answer is:");
        for(i=0;i<3;i++)
        {
            System.out.println("\n");
            for(j=0;j<4;j++)
                System.out.print(" "+ mtx[i][j]);
        }   
    }


}

classfile.java:

package mypack;

import java.io.*;

class matrix
{
    int mat1[][]=new int[3][4];
    void accept()throws IOException
    {
        int i,j,ch;
        InputStreamReader instream = new InputStreamReader(System.in);
        BufferedReader stdin = new BufferedReader(instream);
        System.out.println("\nEnter the coefficients and the constants for the 3 equations  ");
        for(i=0;i<3;i++)
        {
            for(j=0;j<4;j++)
            {
                System.out.println("M["+i+"]["+j+"]=");
                ch=Integer.parseInt(stdin.readLine());
                mat1[i][j]=ch;
            }
        }   
    }
    void display()
    {
        int i,j;
        System.out.println("\nThe matrix is:\n");
        for(i=0;i<3;i++)
        {
            System.out.println("\n");
            for(j=0;j<4;j++)
                System.out.print(" "+ mat1[i][j]);
        }   
    }
    int[][] echolonform()
    {
        int i=0,j,k,n;
        for(i=0;i<3;i++)
        {
            int temp=mat1[i][i];
            for(j=0;j<4;j++)
                mat1[i][j]=mat1[i][j]/temp;
            for(k=0;k<3;k++)
            {
                temp=mat1[k][i];    
                if(i!=k)
                {
                    for(n=0;n<3;n++)
                    {
                        mat1[k][n]=mat1[k][n]-(temp*(mat1[i][n]));
                    }
                }
            }

        }
        return mat1;
    }
}

the output is::

Enter the coefficients and the constants for the 3 equations  
M[0][0]=
2
M[0][1]=
-3
M[0][2]=
1
M[0][3]=
1
M[1][0]=
1
M[1][1]=
2
M[1][2]=
1
M[1][3]=
-1
M[2][0]=
3
M[2][1]=
-1
M[2][2]=
3
M[2][3]=
4

The matrix is:



 2 -3 1 1

 1 2 1 -1

 3 -1 3 4
The answer is:


 1 0 0 0

 0 1 0 0

 0 0 1 1

the answer should be

1 0 0 -3
0 1 0 -1
0 0 1 4

please help!!

Recommended Answers

All 4 Replies

I repeat once again what I had said in the previous thread :-


The code tags go in square brackets ([) not angled brackets, like here:-

[code=java] // Your code goes here

[/code]

here i have 2 files
and im getting a wrong echlon form of the matrix...please help;

Linear_Equation.java:

package mypack;

import java.io.*;

public class Linear_Equation{
public static void main(String args[])throws IOException{
int i,j;
matrix mat1=new matrix();
int mtx[][]=new int[3][4];
mat1.accept();
mat1.display();
mtx=mat1.echolonform();
System.out.println("\nThe answer is:");
for(i=0;i<3;i++)
{
System.out.println("\n");
for(j=0;j<4;j++)
System.out.print(" "+ mtx[i][j]);
} 
}


}

classfile.java:

package mypack;

import java.io.*;

class matrix
{
int mat1[][]=new int[3][4];
void accept()throws IOException
{
int i,j,ch;
InputStreamReader instream = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(instream);
System.out.println("\nEnter the coefficients and the constants for the 3 equations ");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
System.out.println("M["+i+"]["+j+"]=");
ch=Integer.parseInt(stdin.readLine());
mat1[i][j]=ch;
}
} 
}
void display()
{
int i,j;
System.out.println("\nThe matrix is:\n");
for(i=0;i<3;i++)
{
System.out.println("\n");
for(j=0;j<4;j++)
System.out.print(" "+ mat1[i][j]);
} 
}
int[][] echolonform()
{
int i=0,j,k,n;
for(i=0;i<3;i++)
{
int temp=mat1[i][i];
for(j=0;j<4;j++)
mat1[i][j]=mat1[i][j]/temp;
for(k=0;k<3;k++)
{
temp=mat1[k][i]; 
if(i!=k)
{
for(n=0;n<3;n++)
{
mat1[k][n]=mat1[k][n]-(temp*(mat1[i][n]));
}
}
}

}
return mat1;
}
}

the output is::

Enter the coefficients and the constants for the 3 equations
M[0][0]=
2
M[0][1]=
-3
M[0][2]=
1
M[0][3]=
1
M[1][0]=
1
M[1][1]=
2
M[1][2]=
1
M[1][3]=
-1
M[2][0]=
3
M[2][1]=
-1
M[2][2]=
3
M[2][3]=
4

The matrix is:

2 -3 1 1

1 2 1 -1

3 -1 3 4
The answer is:


1 0 0 0

0 1 0 0

0 0 1 1


the answer should be
1 0 0 -3
0 1 0 -1
0 0 1 4


please help!!

First of all what is Echelon form Do you mean this ?
I remember reading about it during my college days but can no longer recollect it.

BTW better learn to indent your code. For now your programs are small, As soon as they cross a few hundred lines you will find yourself spending too much time on just figuring out the closing / opening brace of a block of code, or searching for code blocks of while, if etc.
You can refer here for the Sun Java Coding Conventions. Although you need not read all of it, just take note of the basic parts for ex, code indentations, variable and method naming conventions etc.

First of all what is Echelon form Do you mean this ?
I remember reading about it during my college days but can no longer recollect it.

BTW better learn to indent your code. For now your programs are small, As soon as they cross a few hundred lines you will find yourself spending too much time on just figuring out the closing / opening brace of a block of code, or searching for code blocks of while, if etc.
You can refer here for the Sun Java Coding Conventions. Although you need not read all of it, just take note of the basic parts for ex, code indentations, variable and method naming conventions etc.

ya i mean the same echelon form.
basically i have to solve linear equations in 3 variables.and my matrix has the coefficients and the constant so it is a 3*4 matrix which i have to get to the reduced echelon form in order to solve it as by the gaussian elimination method..
i would be very greatful if you could help me do this..
im not able to get the right logic behind it even though i have gone through the algorithm steps.
Thanks in advance!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.