gauss elimination of system of linear equations using java code

Ads

 
 

Share on Google+Share on Google+

Laxmi
gauss elimination of system of linear equations using java code
0 Answer(s)      6 years and 5 months ago
Posted in : Java Magazine

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

Ads
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
How to detect system failure using java code - Java Beginners
How to detect system failure using java code  I am doing a project... to detect the process that make failure. I have no idea about the project so please help me. And i need some site to download java source code
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
linear search - Java Beginners
linear search  How do i use a linear search for a 2 dimensional hard coded array? I need to check the first 2 letters in an item number... friend, Code to solve the problem : public class LinearSearch
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 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
Project on mailing system using Java/JSP
Project on mailing system using Java/JSP  I am making a project on mailing system in Java/JSP. How could I display the inbox messages on the home page of user? Please tell me about how to handle these messages in database
Linear Search in Java
Linear Search in Java In this section, we are going to find an element from an array using Linear Searching. Linear searching is a good way to find an element... it is used for small arrays. In the given code, we have allowed the user to enter
SMS alert system using Java - JSP-Servlet
SMS alert system using Java  Respected Sir/Mam, I need to develop an SMS alert application. Scope of the application: Basically... recipients. This can be developed using any kind of componentsThanks
JAVA - Java Beginners
JAVA  Hi, I need java code for Gauss-elimination method. I request you to send the code. i am very thankful to you in advance. Regards, Nagarjuna
java code using while loop
java code using while loop  
Linear Regression
Linear Regression  hi........ Can u give me a simple code for Linear Regression in java swings????????? thanks in advance plz reply
java code for PartialSearch using Map????
java code for PartialSearch using Map????  java code for Partial Search using Map
linear search
linear search  write a program to find string using linear search
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
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
how to browse the general files which are in system using java? - Java Beginners
how to browse the general files which are in system using java?  how to browse the general files which are in system using java? its just like in ms... and it shows all the word document files, like that i want open all image files using
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
Product Register System using Java
Product Register System In this section, you will learn how to create product register system. For this, we have categorized the products by code 1 (food), 2... given discount of  0% to product code 1 , 5% product code 2, 15% product
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
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
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
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
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
Error in MySQL Procedure Using JAVA Code
Error in MySQL Procedure Using JAVA Code  The following Java code (using Connector/J to create a stored procedure on MySQL 5.0) does not execute successfully. Identify the cause and available solutions. statement.execute
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
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 hashing
Linear hashing   hello,, What is linear hashing?   hii, In linear hashing, the table is gradually expanded by splitting the buckets in order until the table has doubled its size