⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chase.java

📁 数学:数值分析:线性方程组 线性方程组的多种解法
💻 JAVA
字号:
package linearEquationGroup;

////the method is only used to solve 3 diagonal-line equation group
import java.util.Scanner;

public class Chase {
	public static void main(String[] args) {
		int n;// the number of the unknown elements
		double[][] l, u;
		double[] a, b, c, d, z, x;

		Scanner input = new Scanner(System.in);

		// input the number of the unknown elements
		System.out.println("input the number of the unknown elements:");
		n = input.nextInt();
		l = new double[n + 1][n + 1];
		u = new double[n + 1][n + 1];
		a = new double[n + 1];
		b = new double[n + 1];
		c = new double[n + 1];
		d = new double[n + 1];
		z = new double[n + 1];
		x = new double[n + 1];

		// input the matrix
		System.out
				.println("\nfrom left to right,then up to down,input the matrix's elements "
						+ "including d1...d"
						+ n
						+ ",and don't input 0(use space to seperate):");
		for (int i = 1; i < n + 1; i++)
			if (i == 1) {
				b[i] = input.nextDouble();
				c[i] = input.nextDouble();
				d[i] = input.nextDouble();
			} else if (i == n) {
				a[i] = input.nextDouble();
				b[i] = input.nextDouble();
				d[i] = input.nextDouble();
			} else {
				a[i] = input.nextDouble();
				b[i] = input.nextDouble();
				c[i] = input.nextDouble();
				d[i] = input.nextDouble();
			}

		// confirm
		System.out.println("\nwhat you input is:");
		for (int i = 1; i < n + 1; i++)
			if (i == 1)
				System.out.println("(" + b[i] + "x1)+(" + c[i] + "x2)=(" + d[i]
						+ ")");
			else if (i == n)
				System.out.println("(" + a[i] + "x" + (n - 1) + ")+(" + b[i]
						+ "x" + n + ")=(" + d[i] + ")");
			else
				System.out.println("(" + a[i] + "x" + (i - 1) + ")+(" + b[i]
						+ "x" + i + ")+(" + c[i] + "x" + (i + 1) + ")=(" + d[i]
						+ ")");

		// compute li(i-1),lii,ui(i+1),zi
		a[1] = u[0][1] = z[0] = a[1] = 0;
		for (int i = 1; i < n + 1; i++) {
			if (i != 1)
				l[i][i - 1] = a[i];
			l[i][i] = b[i] - a[i] * u[i - 1][i];
			if (i != n)
				u[i][i + 1] = c[i] / l[i][i];
			z[i] = (d[i] - a[i] * z[i - 1]) / l[i][i];
		}

		// compute xi
		x[n] = z[n];
		for (int i = n - 1; i > 0; i--)
			x[i] = z[i] - u[i][i + 1] * x[i + 1];

		// output the chart
		System.out.println("\nthe chart is:");
		for (int i = 1; i < n + 1; i++)
			if (i == 1)
				System.out.println(l[i][i] + "		" + u[i][i + 1]);
			else if (i == n)
				System.out.println(l[i][i - 1] + "		" + l[i][i]);
			else
				System.out.println(l[i][i - 1] + "		" + l[i][i] + "		"
						+ u[i][i + 1]);

		// output the charts
		System.out.println("\n\nthe chart related to zi and di is:");
		for (int i = 1; i < n + 1; i++)
			if (i == 1)
				System.out.println("(" + l[i][i] + "z1)=" + "(" + d[i] + ")");
			else
				System.out.println("(" + a[i] + "z" + (i - 1) + ")+(" + l[i][i]
						+ "z" + i + ")=(" + d[i] + ")");
		System.out.println();
		for (int i = 1; i < n + 1; i++)
			System.out.print("z" + i + "=" + z[i] + "	");
		System.out.println("\n\nthe chart related to xi and zi is:");
		for (int i = 1; i < n + 1; i++)
			if (i == n)
				System.out.println("(x" + n + ")=(" + z[i] + ")");
			else
				System.out.println("(x" + i + ")+(" + u[i][i + 1] + "x"
						+ (i + 1) + ")=(" + z[i] + ")");
		System.out.println();
		for (int i = n; i > 0; i--)
			System.out.print("x" + i + "=" + x[i] + "	");
		System.out.println();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -