Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- double[][] xs = new double[][]{
- new double[]{5.452, 0.401, 0.758, 0.123, -0.886},
- new double[]{0.785, 2.654, 0.687, 0.203, -0.356},
- new double[]{0.402, 0.244, 4.456, 0.552, -0.342},
- new double[]{0.210, 0.514, 0.206, 4.568, -0.452}
- };
- double eps;
- double[][] rs = new double[100][4];
- double[][] deltas = new double[100][4];
- double[] newX = new double[4];
- double max = 0;
- int iteration = 0, max_pos = 0, rd =0;
- Scanner sc = new Scanner(System.in);
- eps = sc.nextDouble();
- rd = rounded(eps);
- for (int i = 0; i < 4; i++) {
- double deli = -1 * xs[i][i];
- for (int j = 0; j < 5; j++) {
- BigDecimal rounded = new BigDecimal(xs[i][j] / deli).setScale(rd+1,
- RoundingMode.HALF_UP);
- xs[i][j] = Double.parseDouble(String.valueOf(rounded));
- }
- }
- for (int i = 0; i < 100; i++) {
- iteration++;
- if (i == 0) {
- for (int j = 0; j < 4; j++) {
- rs[i][j] = xs[j][4];
- double tmp = rs[i][j];
- if (tmp < 0) {
- tmp *= -1;
- }
- if (tmp > max) {
- max = tmp;
- for (int k = 0; k < 4; k++) {
- if (k == j) {
- deltas[i][k] = rs[i][j];
- max_pos = k;
- } else {
- deltas[i][k] = 0;
- }
- }
- }
- }
- } else {
- if (i == 7) {
- System.out.println();
- }
- for (int j = 0; j < 4; j++) {
- if (j != max_pos) {
- double tt = rs[i - 1][j] + xs[j][max_pos] * deltas[i - 1][max_pos];
- BigDecimal rounded = new BigDecimal(tt).setScale(rd+1, RoundingMode.HALF_UP);
- rs[i][j] = Double.parseDouble(String.valueOf(rounded));
- }
- }
- for (int j = 0; j < 4; j++) {
- double tmp = rs[i][j];
- if (tmp < 0) {
- tmp *= -1;
- }
- if (tmp > max) {
- max = tmp;
- for (int k = 0; k < 4; k++) {
- if (k == j) {
- deltas[i][k] = rs[i][j];
- max_pos = k;
- } else {
- deltas[i][k] = 0;
- }
- }
- }
- }
- }
- max = 0;
- int check = 0;
- for (int j = 0; j < 4; j++) {
- double tmp = rs[i][j];
- if (tmp < 0) {
- tmp *= -1;
- }
- if (tmp <= eps) {
- check++;
- }
- }
- if (check == 4) {
- break;
- }
- }
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < iteration; j++) {
- newX[i] += deltas[j][i];
- }
- }
- System.out.println("Количество итераций: "+ iteration);
- System.out.println("deltaX(1-4) R(1-4)");
- for (int i = 0; i < iteration; i++) {
- for (int j = 0; j < 4; j++) {
- System.out.print(deltas[i][j] + "\t");
- }
- System.out.print("|");
- for (int j = 0; j < 4; j++) {
- System.out.print(rs[i][j] + "\t");
- }
- System.out.println();
- }
- for (int i = 1; i <= newX.length; i++){
- BigDecimal rounded = new BigDecimal(newX[i-1]).setScale(rd,
- RoundingMode.CEILING);
- System.out.println("x[" + i +"] = "+ rounded);
- }
- }
- public static int rounded(double a){
- int k = 0;
- while(a > 0 && a < 1){
- a *= 10;
- k++;
- }
- return k;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement