Home > Java > javaTutorial > Program written in Java to represent a system of linear equations in matrix form

Program written in Java to represent a system of linear equations in matrix form

王林
Release: 2023-08-19 09:02:08
forward
713 people have browsed it

Program written in Java to represent a system of 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.

如何通过矩阵形式处理线性方程:

什么是线性方程?

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:-

  • 点斜式

  • Standard form

  • Slope intercept form

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

在Java环境中,矩阵是什么?

矩阵是将给定的数字按行和列排列的方式。矩阵完全取决于给定集合中有多少行和列。这些可以包含不同的整数、变量,也可以是这些元素的组合形式,或者一些特殊的字母,如alpha、beta、gamma等。

There are so many types of matrix forms:-

  • row matrix

  • 列矩阵

  • null matrix

  • 方阵

  • diagonal matrix

  • upper triangular matrix

  • lower triangular matrix

  • 对称矩阵

  • 反对称矩阵

将线性方程表示为矩阵形式的算法:

  • 第一步 - 为编程生成一个扫描器类

  • Step 2 − take three different variables

  • 步骤3 - 逐一进行所有计算和形成

  • 第四步 - 打印所有变量和整数在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];
Copy after login

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

  • 方法一−为3个系数进行线性方程求解

Conduct the linear equations for the 3 coefficients:

例如,下面还展示了一个表示:

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
Copy after login

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();
   }
}
Copy after login

输出

###### 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)
Copy after login

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();
	}
}
Copy after login

输出

====== 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--------------
Copy after login

结论

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.

The above is the detailed content of Program written in Java to represent a system of linear equations in matrix form. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template