gauss elimination of system of linear equations using java code

gauss elimination of system of linear equations using java code

Here is my code:

import java.util.Scanner;
import java.util.*;

public class gausselimination {
    private static final double EPSILON = 1e-10;
      int n;int N;
     double A[][];
     double B[];


    public  double[] lsolve(double[][] A, double[] B) 
  {


        for (int p = 0; p < n; p++) 
          {


            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;


            if (Math.abs(A[p][p]) <= EPSILON) {
                throw new RuntimeException("Matrix is singular or nearly singular");
            }


            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];
                }
            }
        }


        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];
        }

         System.out.println("the values of unknowns are:-");
        for (int i = 0; i < n; i++) 
            System.out.println("x" + i + "  " +x[i]);

        return x;
    }



    public  void input() 
{

    double k;
      Scanner s=new Scanner(System.in);
        System.out.println("enter no of equations ");
         N = s.nextInt();

         A = new double[N][];
    System.out.println("enter no of unknowns");
          n=s.nextInt();
        for(int l=0;l<N;l++){
    A[l]= new double[n];
    }
    System.out.println("enter the co-efficient of equations");
    for(int i=0;i<N;i++){
         System.out.println("enter the co-efficient of  equation  "+ (i+1));
    for(int j=0;j<n;j++){
     k=s.nextDouble();
          A[i][j]=k;
    }}
         B = new double[N];

    System.out.println("enter the RHS side of equations");
    for(int i=0;i<N;i++){
         System.out.println("enter the RHS of  equation  "+ (i+1));

     k=s.nextDouble();
          B[i]=k;
    }
}

   public static void main(String args[])

     {
        gausselimination g = new gausselimination(); 
         g.input();
         g.lsolve(g.A, g.B);



        }

}

Thanks

View Answers

Ads









Related Tutorials/Questions & Answers:
gauss elimination of system of linear equations using java code
gauss elimination of system of linear equations using java code  Here is my code: import java.util.Scanner; import java.util....); System.out.println("enter no of equations "); N = s.nextInt
Cramers rule for solving equations using java
Cramers rule for solving equations using java  My code: import... s=new Scanner(System.in); System.out.println("enter no of equations of matrix... double[N]; } System.out.println("enter the co-efficient of equations
Advertisements
Simple banking system using Java
Simple banking system using Java  I am trying to make a simple banking system that has only 3 interfaces which does not connect to the database... and the other is the branch, the third interface is the main class. Using inheritance
java code using swings
java code using swings  code that should be able to enter data of student details using all swings into the access database using jdbc connectivity
online voting system source code in java
online voting system source code in java  Please send me source code for online voting system in java. please replay as fast as. Thank you
java code using while loop
java code using while loop  
java code for PartialSearch using Map????
java code for PartialSearch using Map????  java code for Partial Search using Map
i need attendce management system source code in java
i need attendce management system source code in java  i need attendance management system source code in java
bird feeder system code
bird feeder system code  i want to know about "bird feeder a business solution system in java" please help me for details. how can develop this system please tell me
how to get an lan system ip and mac address in java code
how to get an lan system ip and mac address in java code  strong text
code for multiplication of matrix in java using methods
code for multiplication of matrix in java using methods  code for multiplication of matrix in java using methods
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
Linear Regression
Linear Regression  hi........ Can u give me a simple code for Linear Regression in java swings????????? thanks in advance plz reply
code for traffic control system simulation
code for traffic control system simulation  any one suggest which functions are used to develop a graphical view to traffic control system using awt and swing
Reading RDF file using Java code in Eclipse
Reading RDF file using Java code in Eclipse  Could you please tel me what this statement means - Model model = ModelFactory.createDefaultModel
java code using combobox,radiobutton,checkbox
java code using combobox,radiobutton,checkbox  hi, send me java code for entering student details into ms access database, the code should includes combo box,radiobutton and checkboxes pl send as early as possible
View source code of a html page using java ..
View source code of a html page using java ..  I could find the html source code of a web page using the following program, http://download.oracle.com/javase/1.4.2/docs/api/java/net/URLConnection.html i could get the html code
java plugin code for nutch using filter indexer
java plugin code for nutch using filter indexer  hello: i want... in the doc.add(name,value) . I don't know what is the error in it. Tthis is the code... = conf; } } I think that the error is in using parsedData but I don't know what I
linear search
linear search  write a program to find string using linear search
determinant of n*n matrix using java code
determinant of n*n matrix using java code  Here is my code: import java.util.Scanner.*; import java.util.*; public class determinantcode { double A[][]; double m[][]; int N; public input() { Scanner s=new
Linear search in java
Linear search in java In this section we will  know, what is linear search and how linear works. Linear search is also known as "sequential... or a string in array. Example of Linear Search in Java:public class LinearSearch
attendance management system source code
attendance management system source code  sir i want full ateendance management project please send me source code i am asking so many members... answer so please send code it's very urgent
tree using jsp code
tree using jsp code  i want to draw a tree structure of a family hierarchy using jsp code
java source code to send group mails using struts2
java source code to send group mails using struts2  code to send group mails using struts2
java source code to create mail server using struts2
java source code to create mail server using struts2  java source code to create mail server using struts2
download xml file from website using java code
download xml file from website using java code  how to download xml file from website using java code
java code to send email using gmail smtp server
java code to send email using gmail smtp server  please send me the java code to send email using gmail smtp server. and how to send verification code
how to covert JPG format to Binary formart using java code..
how to covert JPG format to Binary formart using java code..  convert JPG format to Binary formart How can i convert JPG format to Binary format using java code plz help me out
how to send sms on mobile and email using java code
how to send sms on mobile and email using java code  hi.... I am developing a project where I need to send a confirmation/updation msg on clients... the code for the same.... thanks in advance
how to take backup of phone to pc using java code
how to take backup of phone to pc using java code  i want to take the backup of phone data in my pc and also provide the restore functionality,and i want to do it using java program,so can anyone help me
uploading a file at another system in lan using jsp
uploading a file at another system in lan using jsp  Thanks for the code at "http://www.roseindia.net/jsp/fileupload/Sinleupload.xhtml.shtml". It is working when i gave address of the local system to savefile But if i want

Ads