Java 程序以矩阵形式表示线性方程

javaobject oriented programmingprogramming更新于 2024/3/21 21:00:17

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

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

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

什么是线性方程?

线性方程是一种方程,其中变量的最高幂为 1,也称为一次方程。

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

  • 点斜式

  • 标准形式

  • 斜率截距形式

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

Java 环境视图中的矩阵是什么?

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

矩阵形式有很多种:-

  • 行矩阵

  • 列矩阵

  • 空矩阵

  • 方矩阵

  • 对角矩阵

  • 上三角矩阵

  • 下三角矩阵

  • 对称矩阵

  • 反对称矩阵

以矩阵形式表示线性方程的算法:-

  • 步骤 1 − 为生成扫描仪类编程

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

  • 步骤 3 - 逐一放置所有计算和形成

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

  • 步骤 5 - 最后使用扫描仪类系统关闭程序,然后编译程序。

语法

data_type[维度][维度].....[第 N 个维度]
array_name = new data_type[数据大小][数据大小].......[第 N 个位置的数据大小];

在 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 代码处理问题解决的输入。


相关文章


有用资源