Java程式:矩陣形式表示線性方程


在本節Java程式設計中,我們將學習和探索一些程式,這些程式可以將線性方程表示為矩陣形式。要編寫這些程式,我們首先必須瞭解線性方程和矩陣形式、它們的型別以及如何用簡單的數學方法求解它們。

我們將學習如何整合Scanner類(來自java.util包)以使用Java內建程式碼從使用者處獲取輸入。陣列將初始化為儲存一些變數作為問題矩陣的輸入。然後,它將被轉換為迴圈,透過該迴圈求解問題方程。

什麼是線性方程?

線性方程是一種方程,其中變數的最高冪為1,也稱為一元一次方程。

線性方程主要有三種類型:

  • 點斜式

  • 標準式

  • 斜截式

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

在Java環境中,什麼是矩陣?

矩陣是將給定數字按行和列排列。它取決於給定集合中存在多少行和列。這些可以包含不同的整數、變數、兩者組合的形式或一些特殊的字母,例如α、β、γ等。

矩陣形式有很多型別:

  • 行矩陣

  • 列矩陣

  • 零矩陣

  • 方陣

  • 對角矩陣

  • 上三角矩陣

  • 下三角矩陣

  • 對稱矩陣

  • 反對稱矩陣

表示線性方程的步驟

以下是將線性方程表示為矩陣形式的步驟:

  • 步驟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 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.