Sie sind auf Seite 1von 3

GausSeidel.

java

1 package Ejercicios;
2 import java.text.DecimalFormat;
3 import java.util.Scanner;
4
5 public class GausSeidel {
6
7 public static void main(String[] args) {
8 Scanner leer = new Scanner(System.in);
9 String pregunta,medida = null;
10 double ErrorTotal=0,itera=0;
11 int orden;
12
13 DecimalFormat f = new DecimalFormat("####.######");
14
15 System.out.println("Ingrese la pregunta: ");
16 pregunta = leer.nextLine();
17 System.out.println(" \n");
18
19 System.out.println("Ingrese la cantidad de incognitas: ");
20 orden = leer.nextInt();
21 double Matriz[][] = new double[orden][orden+1];
22 double Vant[] = new double[orden];
23 double Vact[]= new double[orden];
24 String concepto[] = new String[orden];
25
26 System.out.println("**ingresa datos extra***");
27 System.out.print("Ingresa el error: ");
28 double error = leer.nextDouble();
29 System.out.print("ingresa el numero de calculos: ");
30 int calculo= leer.nextInt();
31
32 for (int i = 0; i < orden; i++) {
33 leer.nextLine();
34 System.out.println("ingrese el concepto"+(i+1)+" :");
35 concepto[i]=leer.nextLine();
36 System.out.println("Ingrese valor anterior: ");
37 Vant[i]=leer.nextDouble();
38 Vact[i]=0;
39
40 }
41 leer.nextLine();
42 System.out.print("Ingrese la unidad: ");
43 medida = leer.nextLine();
44
45 //System.out.println("***Capturando matriz***");
46 for (int i = 0; i < orden; i++) {
47 for (int j = 0; j < orden+1; j++) {
48 System.out.println("Ingrese dato en la posicion A[" + (i + 1)+"]["
+(j+1)+ "]");
49 double dato =0;
50 dato= leer.nextDouble();
51 Matriz[i][j]=dato;
52 }
53 }
54
55 System.out.println(String.format("\tInstituto Tecnologico de Culiacán"));
56 System.out.println(String.format(" \tIngenieria en Sistemas Computacionales"));
57 System.out.println();
58 System.out.println("NOMBRE: Oscar Enrique Bojorquez Martinez");
59 System.out.println("MATERIA: Metodos Númericos");
60 System.out.println("MAESTRO: Lopez Velazquez Jacobo Natanael");
61 System.out.println("HORARIO: 11:00-12:00");

Page 1
GausSeidel.java

62 System.out.println("Tema: Metodo de Gauss Seidel");


63
64
65 System.out.println("Problema: "+pregunta);
66 System.out.println(" \t| No \t| \t X1 \t| \t X2 \t| \t X3 \t| \t
X4 \t| \t\t Error Total | ");
67
68 System.out.print("\t| "+itera+" \t|");
69 for (int i = 0; i < orden; i++) {
70 System.out.print(f.format(Vant[i])+"\t");
71 System.out.print("\t\t| ");
72
73 }
74 System.out.println(ErrorTotal+"\t\t\t");
75
76
77 //Proceso DE GausSeidel
78
79 do {
80 for (int i=0; i<orden;i++) {
81 double suma=0;
82 double Coef=Matriz[i][i];
83 suma=+Matriz[i][orden];
84
85 for (int j = 0; j < orden; j++) {
86 if(j!=i) {
87 if(j<i){
88 suma=suma+(Matriz[i][j]*-1)*Vact[j];
89
90 } else {
91 suma=suma+(Matriz[i][j]*-1)*Vant[j];
92 }
93 }
94
95 }
96 Vact[i] =(suma/Coef);
97 }
98
99 //Calculando Error Absoluto
100 ErrorTotal=0;
101 for (int i = 0; i < orden; i++) {
102 ErrorTotal=ErrorTotal+(Math.abs(Math.abs(Vact[i])-Math.abs(Vant[i])));
103 }
104 itera++;
105 System.out.print("\t| "+itera+" \t|");
106
107 for (int i = 0; i < orden; i++) {
108 System.out.print(f.format(Vact[i]));
109 System.out.print(" \t|");
110 Vant[i]=Vact[i];
111 }
112 System.out.println(f.format(ErrorTotal));
113 System.out.println("\t\t|");
114
115 if(ErrorTotal<=error) {
116 System.out.println("_______________________________________________________
____________________________________________________________________");
117 System.out.println("Resultados:");
118 for (int i = 0; i < orden; i++) {
119 System.out.print("\n"+concepto[i]+" = "+" \t "+f.format(Vact[i])+"
"+medida);
120 System.out.println();

Page 2
GausSeidel.java

121 }
122 }else {
123 if(itera<calculo) {
124
125 } else {
126 System.out.println("_______________________________________________________
_____________________________________________________________________");
127 System.out.println("Resultados: ");
128 System.out.println("Lo siento, no se encontro un resultado apropiado en los
calculos permitidos.");
129
130 }
131 }
132
133 }while(ErrorTotal>error || calculo<=itera);
134
135
136 }
137
138
139
140
141
142
143
144
145 }
146
147
148
149
150
151

Page 3

Das könnte Ihnen auch gefallen