首页 > Java > java教程 > 正文

用Java编写的程序,用矩阵形式表示线性方程组

王林
发布: 2023-08-19 09:02:08
转载
767人浏览过

用java编写的程序,用矩阵形式表示线性方程组

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

立即学习Java免费学习笔记(深入)”;

  • 点斜式

  • 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

    豆包AI编程
    豆包AI编程

    豆包推出的AI编程助手

    豆包AI编程 483
    查看详情 豆包AI编程
  • 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];
登录后复制

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
登录后复制

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();
   }
}
登录后复制

输出

###### 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)
登录后复制

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();
	}
}
登录后复制

输出

====== 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--------------
登录后复制

结论

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.

以上就是用Java编写的程序,用矩阵形式表示线性方程组的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号