Java 表示矩阵形式中的线性方程

Java 表示矩阵形式中的线性方程

Java是一种面向对象的编程语言,用于解决和实现程序。在这个Java编程的部分中,我们将学习和发现一些程序,通过这些程序我们可以将线性方程表示为矩阵形式。要先学习这些程序,我们必须了解线性方程和矩阵形式,它们的类型以及它们如何通过简单的数学方法和Java编程解决。

在本文中,我们将学习如何集成一个扫描器类来通过Java代码从用户那里获取输入。数组将初始化为将一些变量作为问题矩阵的输入存储。然后将其转换为循环,通过该循环将解决问题方程。

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

什么是线性方程

线性方程是一种变量的最高次数为1的方程,也被称为一次方程。

线性方程有3种主要类型:

  • 点斜式

  • 标准形式

  • 斜截式

有一些方法可以解决线性方程,如消元法、代入法、交叉乘法法和矩阵法。

在Java环境中,矩阵是什么意思

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

有很多类型的矩阵形式:

  • 行矩阵

  • 列矩阵

  • 零矩阵

  • 方阵

  • 对角矩阵

  • 上三角矩阵

  • 下三角矩阵

  • 对称矩阵

  • 反对称矩阵

用于表示线性方程的矩阵形式的算法

  • 步骤1 – 为编程生成一个扫描器类

  • 步骤2 – 获取三个不同的变量

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

  • 步骤4 – 在S.O.P中打印所有变量和整数

  • 步骤5 – 在末尾使用扫描器类系统关闭程序,然后编译程序

语法

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

在Java语言中,这个方程组和矩阵的设置方式不同。我们需要插入一个程序,输入是线性方程,输出是矩阵格式,或者反过来。为了做到这些,我们需要参考以下的许多示例和步骤 –

方法

  • 方法1 – 进行3个系数的线性方程

进行3个系数的线性方程:

例如,下面也显示了一个表示:

线性方程组

3x + 5y + 8z = 24

8x + 10y + 12z = 30

2x + 4y + 5z = 5

矩阵表示

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

为了更好地理解如何用矩阵形式表示线性方程组,我们提供了一个学习编码的程序如下 –

示例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();
   }
}

输出

###### 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)

示例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();
    }
}

输出

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

结论

多维数组被用来以行列格式存储输入数据。它们通常用于存储3D数据。

通过本文,我们学习了如何以矩阵形式表示线性方程,并通过Java代码处理输入的问题。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程