Java Program to Represent Linear Equations in Matrix Form


Java is an object oriented programming language which is used to solve and implement programs. In this segment of Java programming we are going to learn and discover about certain programs by which we can represent linear equations in Matrix form. To do these programs at first, we have to learn about linear equations and Matrix forms , their types and how they are solved by simple mathematical methods and then by Java programming.

In this article we will learn how to integrate a scanner class to take an input from the user by a java build code. Where the array will initialize to store some variables as an input for the problem matrix. Then it will converted into a loop by which the problem equation will be solved.

How to work with a linear equation through a Matrix form:

What Is Linear Equation?

Linear equation is a type of equation in which the highest power of a variable is 1 which is also known as a one-degree equation.

There are 3 major types of linear equations:-

  • Point slope form

  • Standard form

  • Slope intercept form

There are certain methods to solve linear equations like elimination method, substitution method, cross multiplication method and Matrix method.

What Is a Matrix in the view of Java Environment?

Matrix is the arrangement of given numbers in rows and columns. Matrix is totally depends on how many rows and columns are present in the given set. These can have different integers, variables, a combined form of either the things or some special letters like alpha, beta, gamma etc.

There are so many types of matrix forms:-

  • row matrix

  • column matrix

  • null matrix

  • square matrix

  • diagonal matrix

  • upper triangular matrix

  • lower triangular matrix

  • symmetric matrix

  • antisymmetric matrix

Algorithm to Represent Linear Equation In A Matrix Form:-

  • Step 1 − Generate a scanner class for programming

  • Step 2 − take three different variables

  • Step 3 − Putting all the calculations and formations one by one

  • Step 4 − print all the variables and integers in S.O.P

  • Step 5 − close the program with the scanner class system in the end and then compile the program.

Syntax

data_type[The Dimension][The Dimension].....[Nth number of dimension] 
array_name = new data_type[Size of data][size of data].......[size of data at Nth Position];

In the Java language this sequence of equations and Matrix sets up differently. We have to insert a program in which input will be given in linear equations and output will be in Matrix format or vice versa. To do these we have to go through many examples and steps in the following −

Approach

  • Approach 1 − Conducting the linear equations for the 3 coefficients

Conduct the linear equations for the 3 coefficients:

For an example a representation is also shown below:- System of Linear Equation 3x + 5y + 8z = 24 8x + 10y + 12z = 30 2x + 4y + 5z = 5

Matrix representation

      3.   5.   8                  x                           24
 A =  8.  10.  12            X =   y                   B  =    30
      2.   4.   5.                 z                            5

For better understanding to represent the linear equations in Matrix form, we have provided a program to learn this set of coding below -

Example 1

import java.util.Scanner;

public class matrix07tutorialspoint {
	public static void main(String args[]){
      
      System.out.println("###### 3 variable linear equation ######");	
      char[] variable = { 'x', 'y', 'z' };
      Scanner sc = new Scanner(System.in);	
      System.out.println("Enter input as the coefficients of 3 variable");
      System.out.println("Enter in the specific format shown");
      System.out.println("ex + fy + gz = j");
      int[][] matrix = new int[3][3];
      int[][] constt = new int[3][1];

      
      for (int k = 0; k < 3; k++) {
      	
         for (int j = 0; j < 3; j++) {      
            matrix[k][j] = sc.nextInt();
      	 }	
      	 constt[k][0] = sc.nextInt();
      }
      System.out.println("Matrix representation of above linear equations is: ");
      for (int k = 0; k < 3; k++) {	
         for (int j = 0; j < 3; j++) {      
            System.out.print(" " + matrix[k][j]);
      	 }

      	 System.out.print(" " + variable[k]);
      	 System.out.print(" = " + constt[k][0]);
      	 System.out.println();
      }
      sc.close();
   }
}

Output

###### 3 variable linear equation ######
Enter input as the coefficients of 3 variable
Enter in the specific format shown
ex + fy + gz = j
Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:941)
	at java.base/java.util.Scanner.next(Scanner.java:1598)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2263)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2217)
	at matrix07tutorialspoint.main(matrix07tutorialspoint.java:20)

Example 2

import java.util.Scanner;

public class ARBRDDTutorialpoint {
   public static void main(String args[]){
      System.out.println("====== n variable of a linear equation ======");
      
      char[] variable= { 'e', 'f', 'g', 'x', 'y', 'z', 'v' };
      System.out.println("Enter the number of variables");
      Scanner sc = new Scanner(System.in);
      int num = sc.nextInt();
      System.out.println("Enter the coefficients variable as we need to perform");
      System.out.println("To get the result enter the input in the format shown below");
      System.out.println("ex + fy + gz + ... = o");
	
      
      int[][] matrix = new int1[num][num];
      int[][] constt = new int1[num][1];
      for (int k = 0; k < num; k++) {
         for (int j = 0; j < num; j++) {
            matrix[k][j] = sc.nextInt();
      	 }
      	 constt[k][0] = sc.nextInt();
      }
      
      System.out.println("Matrix representation of above linear equations are: ");
      for (int i = 0; i < num; i++) {
         for (int j = 0; j < num; j++) {
            System.out.print(" " + matrix[i][j]);
      	 }
      	 System.out.print(" " + variable[i]);
      	 System.out.print(" = " + constt[i][0]);
      	 System.out.println();
      }
      sc.close();
	}
}

Output

====== n variable of a linear equation ======
Enter the number of variables
4
Enter the coefficients variable as we need to perform
To get the result enter the input in the format shown below
ex + fy + gz + ... = o
10 11 12 13
14 15 16 16
18 19 20 21
22 23 24 25

--------OUTPUT INCOMPLETE ------- PLEASE CHECK--------------

Conclusion

Multidimensional arrays are used to store the input data in a row-column format. They can commonly use to store the 3D data.

From this article, we have learnt how to represent a linear equation in a matrix form and get problem solved input processed by the Java code.

Updated on: 11-Apr-2023

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements