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

📄 linearequationgrouptest.java

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

//LinearEquationGroup class testing
import java.util.Scanner;

public class LinearEquationGroupTest {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		LinearEquationGroup erGroup;
		int n;
		double a[][];
		double b[];
		double errAllowed;
		double[] xIni;
		double relaxationParameter;
		String contin,another;

		System.out.println("\n----------------------------the program solves a linear equation group" +
				"----------------------------\n");
		do {
			// input the equation group
			System.out.println("\ninput the number of the equation:");
			n = input.nextInt();
			a = new double[n + 1][n + 1];
			b = new double[n + 1];
			System.out
					.println("\nleft to right,up to down,input the eqution group's left matrix "
							+ "(use space to seperate):");
			for (int i = 1; i < n + 1; i++)
				for (int j = 1; j < n + 1; j++)
					a[i][j] = input.nextDouble();
			System.out
					.println("\nup to down,input the eqution group's right value "
							+ "(use space to seperate):");
			for (int i = 1; i < n + 1; i++)
				b[i] = input.nextDouble();
			erGroup = new LinearEquationGroup(n, a, b);

			// confirm the equation group
			System.out.println("\nthe equation group that you input is:");
			System.out.println(erGroup.confirm());
			
			System.out.println("\ninput \"c\" to continue:");
			contin=input.next();

			// use Gauss elimination method
			System.out.println("\nuse Gauss reduction to solve it:");
			System.out.println(erGroup.gaussElimination());
			
			System.out.println("\ninput \"c\" to continue:");
			contin=input.next();

			// use Crout elimination method
			System.out.println("\n\nuse Crout reduction to solve it:");
			System.out.println(erGroup.croutElimination());
			
			System.out.println("\ninput \"c\" to continue:");
			contin=input.next();

			// use Cholesky method
			System.out.println("\n\nuse Chelosky method to solve it:");
			System.out.println(erGroup.cholesky());
			
			System.out.println("\ninput \"c\" to continue:");
			contin=input.next();

			// use main element method
			System.out.println("\n\nuse main element method to solve it:");
			System.out.println(erGroup.mainElement());
			
			System.out.println("\ninput \"c\" to continue:");
			contin=input.next();

			// use Jacobi iterative method
			errAllowed = 0;
			xIni = new double[n + 1];
			System.out
					.println("\n\nuse jacobi iterative method to solve it now.input the error allowed:");
			errAllowed = input.nextDouble();
			System.out.println("\nfrom x1 to x" + n
					+ ",input the initialized value(use space to seperate)");
			for (int i = 1; i < n + 1; i++)
				xIni[i] = input.nextDouble();
			System.out.println(erGroup.jacobiIterative(errAllowed, xIni));
			
			System.out.println("\ninput \"c\" to continue:");
			contin=input.next();

			// use Gauss-Seidel iterative method
			errAllowed = 0;
			xIni = new double[n + 1];
			System.out
					.println("\n\nuse Guass-Seidel iterative method to solve it now."
							+ "input the error allowed:");
			errAllowed = input.nextDouble();
			System.out.println("\nfrom x1 to x" + n
					+ ",input the initialized value(use space to seperate):");
			for (int i = 1; i < n + 1; i++)
				xIni[i] = input.nextDouble();
			System.out.println(erGroup.gaussSeidelIterative(errAllowed, xIni));
			
			System.out.println("\ninput \"c\" to continue:");
			contin=input.next();

			// use relaxation iterative method
			relaxationParameter = 1;
			System.out
					.println("\n\nuse relaxation iterative method to solve it now."
							+ "input the error allowed:");
			errAllowed = input.nextDouble();
			System.out.println("\nfrom x1 to x" + n
					+ ",input the initialized value(use space to seperate):");
			for (int i = 1; i < n + 1; i++)
				xIni[i] = input.nextDouble();
			System.out.println("input the relaxation parameter:");
			relaxationParameter = input.nextDouble();
			System.out.println(erGroup.relaxationIterative(errAllowed, xIni,
					relaxationParameter));

			System.out
					.println("\n         input another linear equation group?  y/n:");
			another = input.next();

		} while (another.equalsIgnoreCase("y"));
	}

}

⌨️ 快捷键说明

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