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

📄 trecia.java

📁 Solving linear equations using iteration. Seidels and Biggest incline methods
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
							else {
								x[iteration + 1][row] = x[iteration + 1][row] - matrixFull[row][column] * x[iteration][column];
							}
						}
						else {
							x[iteration + 1][row] = x[iteration + 1][row] - matrixFull[row][column] * x[iteration][column];
						}
					}
				}

				if (((iteration + 1) < 10) && (row == 0)) {
					System.out.print(" ");
				}
				round = Round(x[iteration][row], deg);
				doubleToString = "" + round;
				System.out.print(doubleToString);
				for (buffer = 0; buffer < ((2 * deg) - doubleToString.length() - 7); buffer++) {
					System.out.print(" ");
				}
			}
			
			System.out.println();
			
			maximum = 0;
			for (row = 0; row < N; row++) {
				difference = Math.abs(x[iteration + 1][row] - x[iteration][row]);
				if (difference < 0) {
					difference = (-1) * difference;
				}
				if (maximum < difference) {
					maximum = difference;
				}
			}
			
			iteration++;
		} while (maximum > PRECISION);

		System.out.println();
		System.out.println("###############################################################################");
		System.out.println("\n\n");
	}

	public static void BiggestGradientMethod() {
		double[] x, newX, P, Z, R, newZ, newP;
		double t, b;
		int iteration = 1;
		int row = 0;
		double round = 0.0;
		int buffer = 0;
		String doubleToString = "";
		int deg = 11;

		P = new double[N];
		Z = new double[N];
		R = new double[N];
		x = new double[N];
		newX = new double[N];
		newZ = new double[N];
		newP = new double[N];

		System.out.println("\n\n\n\n\n\n");
		System.out.println("###############################################################################");
		System.out.println("                          DIDZIAUSIO NUOLYDZIO METODAS");
		System.out.println("###############################################################################");

		for (row = 0; row < N; row++) {
			x[row] = 0;
		}

		for (row = 0; row < N; row++){
			//P = Ax-f
			P[row] = (matrixA[row][0] * x[0]  + matrixA[row][1] * x[1]  + matrixA[row][2] * x[2]  + matrixA[row][3] * x[3]    - matrixB[row]);
			//Z=Ax-f
			Z[row] = (matrixA[row][0] * x[0]  + matrixA[row][1] * x[1]  + matrixA[row][2] * x[2]  + matrixA[row][3] * x[3]    - matrixB[row]);
		}

		for (row = 0; row < 4; row++){
			//R =AZ
			R[row] = (matrixA[row][0] * Z[0]  + matrixA[row][1] * Z[1]  + matrixA[row][2] * Z[2]  + matrixA[row][3] * Z[3]);
		}

		t = ((Z[0] * P[0]  + Z[1] * P[1] + Z[2]  * P[2]  + Z[3] * P[3]) / (R[0] * Z[0]  + R[1] * Z[1]  + R[2] * Z[2]  + R[3] * Z[3]));

		for (row = 0; row < N; row++) {
			//x=x-tZ
			newX[row] = x[row] - (t * Z[row]);
			//Z=Z-tR
			newZ[row] = Z[row] - (t * R[row]);
		}

		System.out.println("\n");
		System.out.println("Sprendinio artiniu lentele:");
		System.out.print(iteration + " iter.:  ");

		for (row = 0; row < N; row++) {
			round = Round(newX[row], deg);
			doubleToString = "" + round;
			
			if ((iteration < 10) && (row == 0)) {
				System.out.print(" ");
			}
			
			System.out.print(doubleToString);
			
			for (buffer = 0; buffer < ((2 * deg) - doubleToString.length() - 7); buffer++) {
				System.out.print(" ");
			}
		}

		System.out.println();

		while ((Math.abs(newZ[0] * newZ[0]  + newZ[1] * newZ[1]  + newZ[2] * newZ[2]  + newZ[3] * newZ[3])) >= (PRECISION * PRECISION)) {
			x[0] = newX[0];
			x[1] = newX[1];
			x[2] = newX[2];
			x[3] = newX[3];
			P[0] = newP[0];
			P[1] = newP[1];
			P[2] = newP[2];
			P[3] = newP[3];
			Z[0] = newZ[0];
			Z[1] = newZ[1];
			Z[2] = newZ[2];
			Z[3] = newZ[3];
			
			iteration++;

			for (row = 0; row < N; row++){
				R[row] = (matrixA[row][0] * Z[0]  + matrixA[row][1] * Z[1]  + matrixA[row][2] * Z[2]  + matrixA[row][3] * Z[3]);
			}

			t = ((Z[0] * Z[0]  + Z[1] * Z[1]  + Z[2] * Z[2]  + Z[3] * Z[3]) / (R[0] * Z[0]  + R[1] * Z[1]  + R[2] * Z[2]  + R[3] * Z[3]));

			for (row = 0; row < N; row++) {
				newX[row] = x[row] - (t * Z[row]);
				newZ[row] = Z[row] - (t * R[row]);
			}

			System.out.print(iteration + " iter.:  ");

			for (row = 0; row < N; row++) {
				round = Round(newX[row], deg);
				doubleToString = "" + round;
				if ((iteration < 10) && (row == 0)) {
					System.out.print(" ");
				}

				System.out.print(doubleToString);

				for (buffer = 0; buffer < ((2 * deg) - doubleToString.length() - 7); buffer++) {
					System.out.print(" ");
				}
			}
			
			System.out.println();
		}

		System.out.println();
		System.out.println("###############################################################################");
		System.out.println("\n\n");
	}

	public static void UnitedGradientMethod() {
		double[] x, newX, P, Z, R, newZ, newP;
		double t, b;
		int iteration = 1;
		int row = 0;
		double round = 0.0;
		int buffer = 0;
		String doubleToString = "";
		int deg = 11;

		P = new double[N];
		Z = new double[N];
		R = new double[N];
		x = new double[N];
		newX = new double[N];
		newZ = new double[N];
		newP = new double[N];

		System.out.println("\n\n\n\n\n\n");
		System.out.println("###############################################################################");
		System.out.println("                          JUNGTINIU GRADIENTU METODAS");
		System.out.println("###############################################################################");

		for (row = 0; row < N; row++) {
			x[row] = 0;
		}

		for (row = 0; row < N; row++){
			//P = Ax-f
			P[row] = (matrixA[row][0] * x[0]  + matrixA[row][1] * x[1]  + matrixA[row][2] * x[2]  + matrixA[row][3] * x[3]    - matrixB[row]);
			//Z=Ax-f
			Z[row] = (matrixA[row][0] * x[0]  + matrixA[row][1] * x[1]  + matrixA[row][2] * x[2]  + matrixA[row][3] * x[3]    - matrixB[row]);
		}

		for (row = 0; row < 4; row++){
			//R =AP
			R[row] = (matrixA[row][0] * P[0]  + matrixA[row][1] * P[1]  + matrixA[row][2] * P[2]  + matrixA[row][3] * P[3]);
		}

		t = ((Z[0] * P[0]  + Z[1] * P[1] + Z[2]  * P[2]  + Z[3] * P[3]) / (R[0] * P[0]  + R[1] * P[1]  + R[2] * P[2]  + R[3] * P[3]));

		for (row = 0; row < N; row++) {
			//x=x-tP
			newX[row] = x[row] - (t * P[row]);
			//Z=Z-tR
			newZ[row] = Z[row] - (t * R[row]);
		}

		System.out.println("\n");
		System.out.println("Sprendinio artiniu lentele:");
		System.out.print(iteration + " iter.:  ");

		for (row = 0; row < N; row++) {
			round = Round(newX[row], deg);
			doubleToString = "" + round;
			
			if ((iteration < 10) && (row == 0)) {
				System.out.print(" ");
			}
			
			System.out.print(doubleToString);
			
			for (buffer = 0; buffer < ((2 * deg) - doubleToString.length() - 7); buffer++) {
				System.out.print(" ");
			}
		}

		System.out.println();

		while ((Math.abs(newZ[0] * newZ[0]  + newZ[1] * newZ[1]  + newZ[2] * newZ[2]  + newZ[3] * newZ[3])) >= (PRECISION * PRECISION)) {
			//Beta = (nZ,nZ)/(Z,Z)
			b = ((newZ[0] * newZ[0]  + newZ[1] * newZ[1]  + newZ[2] * newZ[2]  + newZ[3] * newZ[3]) / (Z[0] * Z[0]  + Z[1] * Z[1]  + Z[2] * Z[2]  + Z[3] * Z[3]));

			for (row = 0; row < 4; row++) {
				newP[row] = newZ[row] + (b * P[row]);
			}

			x[0] = newX[0];
			x[1] = newX[1];
			x[2] = newX[2];
			x[3] = newX[3];
			P[0] = newP[0];
			P[1] = newP[1];
			P[2] = newP[2];
			P[3] = newP[3];
			Z[0] = newZ[0];
			Z[1] = newZ[1];
			Z[2] = newZ[2];
			Z[3] = newZ[3];
			
			iteration++;

			for (row = 0; row < N; row++){
				R[row] = (matrixA[row][0] * P[0]  + matrixA[row][1] * P[1]  + matrixA[row][2] * P[2]  + matrixA[row][3] * P[3]);
			}

			t = ((Z[0] * P[0]  + Z[1] * P[1]  + Z[2] * P[2]  + Z[3] * P[3]) / (R[0] * P[0]  + R[1] * P[1]  + R[2] * P[2]  + R[3] * P[3]));

			for (row = 0; row < N; row++) {
				newX[row] = x[row] - (t * P[row]);
				newZ[row] = Z[row] - (t * R[row]);
			}

			System.out.print(iteration + " iter.:  ");

			for (row = 0; row < N; row++) {
				round = Round(newX[row], deg);
				doubleToString = "" + round;
				
				if ((iteration < 10) && (row == 0)) {
					System.out.print(" ");
				}
				
				System.out.print(doubleToString);
				
				for (buffer = 0; buffer < ((2 * deg) - doubleToString.length() - 7); buffer++) {
					System.out.print(" ");
				}
			}
			
			System.out.println();
		}

		System.out.println();
		System.out.println("###############################################################################");
		System.out.println("\n\n");
	}

	public static int IntegerRead() {
        String tmp = "";
		int skaiciukas = 0;

		try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            tmp = br.readLine();
		} catch (IOException exc) {
            System.out.println("Skaitymo klaida: " + exc);
            System.exit(0);
		}

		try {
            skaiciukas = Integer.parseInt(tmp);
        } catch (NumberFormatException exc) {
            System.out.println("Blogas skaitmuo. Eilute " + tmp + " yra ignoruota!");
		}

		return skaiciukas;
    }

	public static void main(String[] args) {
		int meniu;

		dataInitialize();
		StartUpData();

    	System.out.println("Pasirinkite metodo varianta:");
    	System.out.println("1 - Jakobio metodas");
    	System.out.println("2 - Zeidelio metodas");
    	System.out.println("3 - Didziausio nuolydzio metodas");
    	System.out.println("4 - Jungtiniu gradientu metodas");
    	System.out.println("5 - Pradiniai duomenys");
    	System.out.println("0 - Iseiti");
    	System.out.print("Numeris: ");
    	meniu = IntegerRead();

		while (meniu != 0) {
	        if (meniu == 1){
	            JacobiMethod();
	            System.out.println("Pasirinkite metodo varianta:");
		    	System.out.println("1 - Jakobio metodas");
		    	System.out.println("2 - Zeidelio metodas");
		    	System.out.println("3 - Didziausio nuolydzio metodas");
		    	System.out.println("4 - Jungtiniu gradientu metodas");
		    	System.out.println("5 - Pradiniai duomenys");
		    	System.out.println("0 - Iseiti");
		    	System.out.print("Numeris: ");
        		meniu = IntegerRead();
	        }
	        else if (meniu == 2) {
	        	ZeidelMethod();
	            System.out.println("Pasirinkite metodo varianta:");
		    	System.out.println("1 - Jakobio metodas");
		    	System.out.println("2 - Zeidelio metodas");
		    	System.out.println("3 - Didziausio nuolydzio metodas");
		    	System.out.println("4 - Jungtiniu gradientu metodas");
		    	System.out.println("5 - Pradiniai duomenys");
		    	System.out.println("0 - Iseiti");
		    	System.out.print("Numeris: ");
        		meniu = IntegerRead();
	        }
	        else if (meniu == 3) {
	        	BiggestGradientMethod();
	            System.out.println("Pasirinkite metodo varianta:");
		    	System.out.println("1 - Jakobio metodas");
		    	System.out.println("2 - Zeidelio metodas");
		    	System.out.println("3 - Didziausio nuolydzio metodas");
		    	System.out.println("4 - Jungtiniu gradientu metodas");
		    	System.out.println("5 - Pradiniai duomenys");
		    	System.out.println("0 - Iseiti");
		    	System.out.print("Numeris: ");
        		meniu = IntegerRead();
	        }
	        else if (meniu == 4) {
	        	UnitedGradientMethod();
	        	System.out.println("Pasirinkite metodo varianta:");
		    	System.out.println("1 - Jakobio metodas");
		    	System.out.println("2 - Zeidelio metodas");
		    	System.out.println("3 - Didziausio nuolydzio metodas");
		    	System.out.println("4 - Jungtiniu gradientu metodas");
		    	System.out.println("5 - Pradiniai duomenys");
		    	System.out.println("0 - Iseiti");
		    	System.out.print("Numeris: ");
        		meniu = IntegerRead();
	        }
	        else if (meniu == 5) {
	        	StartUpData();
	        	System.out.println("Pasirinkite metodo varianta:");
		    	System.out.println("1 - Jakobio metodas");
		    	System.out.println("2 - Zeidelio metodas");
		    	System.out.println("3 - Didziausio nuolydzio metodas");
		    	System.out.println("4 - Jungtiniu gradientu metodas");
		    	System.out.println("5 - Pradiniai duomenys");
		    	System.out.println("0 - Iseiti");
		    	System.out.print("Numeris: ");
        		meniu = IntegerRead();
	        }
	        else {
	            System.out.println("Ivedete bloga skaiciu.");
	            System.out.println("Pasirinkite metodo varianta:");
		    	System.out.println("1 - Jakobio metodas");
		    	System.out.println("2 - Zeidelio metodas");
		    	System.out.println("3 - Didziausio nuolydzio metodas");
		    	System.out.println("4 - Jungtiniu gradientu metodas");
		    	System.out.println("5 - Pradiniai duomenys");
		    	System.out.println("0 - Iseiti");
		    	System.out.print("Numeris: ");
        		meniu = IntegerRead();
	        }
	    }
	}

}

⌨️ 快捷键说明

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