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


在本节Java编程中,我们将学习和探索一些程序,这些程序可以将线性方程表示为矩阵形式。要编写这些程序,我们首先必须了解线性方程和矩阵形式、它们的类型以及如何用简单的数学方法求解它们。

我们将学习如何集成Scanner类(来自java.util包)以使用Java内置代码从用户处获取输入。数组将初始化为存储一些变量作为问题矩阵的输入。然后,它将被转换为循环,通过该循环求解问题方程。

什么是线性方程?

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

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

  • 点斜式

  • 标准式

  • 斜截式

可以使用多种方法求解线性方程,例如消元法、代入法、交叉相乘法和矩阵法。

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

矩阵是将给定数字按行和列排列。它取决于给定集合中存在多少行和列。这些可以包含不同的整数、变量、两者组合的形式或一些特殊的字母,例如α、β、γ等。

矩阵形式有很多类型:

  • 行矩阵

  • 列矩阵

  • 零矩阵

  • 方阵

  • 对角矩阵

  • 上三角矩阵

  • 下三角矩阵

  • 对称矩阵

  • 反对称矩阵

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

表示线性方程的步骤

以下是将线性方程表示为矩阵形式的步骤:

  • 步骤1 - 生成用于编程的Scanner类

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

  • 步骤3 - 按顺序进行所有计算和运算

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

  • 步骤5 - 在程序末尾使用Scanner类系统关闭程序,然后编译程序。

语法

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语言中,这一系列方程和矩阵的设置方式不同。我们必须插入一个程序,其中输入将以线性方程的形式给出,输出将以矩阵形式给出,反之亦然。为此,我们必须在以下内容中学习许多示例和步骤:

进行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--------------

更新于:2024-11-23

389 次浏览

启动您的职业生涯

通过完成课程获得认证

开始学习
广告