Last active
June 24, 2017 01:41
-
-
Save georgelima/5f0f68f17b6a86de5ce1766e48478e7f to your computer and use it in GitHub Desktop.
Jacobi method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Jacobi { | |
public static void main(String args[]) { | |
double []solucao = new double[10]; | |
double [][]matriz = new double[10][10]; | |
double []resultados = new double[10]; | |
double diagonalDominante = 0; | |
double soma; | |
int cont = 0; | |
Scanner input = new Scanner(System.in); | |
System.out.print("Informe o número de equações: "); | |
int numEquacoes = input.nextInt(); | |
System.out.print("Informe a quantidade de iterações: "); | |
int iteracoes = input.nextInt(); | |
for (int i = 0; i < numEquacoes; i++) { | |
for (int j = 0; j < numEquacoes; j++) { | |
System.out.print("Matriz[" + i + "][" + j + "] = "); | |
matriz[i][j] = input.nextDouble(); | |
} | |
} | |
System.out.println("Informe os resultados: "); | |
for (int j = 0; j < numEquacoes; j++) { | |
System.out.print("Vetor[" + j + "] = "); | |
resultados[j] = input.nextDouble(); | |
} | |
for (int i = 0; i < numEquacoes; i++) { | |
soma = 0; | |
for (int j = 0; j < numEquacoes; j++) { | |
if (i == j) { | |
diagonalDominante = matriz[i][j]; | |
} else { | |
soma += matriz[i][j]; | |
} | |
} | |
if (diagonalDominante > soma) { | |
cont++; | |
} | |
} | |
if (cont == numEquacoes) { | |
Jacobi gauss = new Jacobi(); | |
gauss.Jacobi(numEquacoes, iteracoes, solucao, matriz, resultados); | |
} else { | |
System.out.println("A matriz não é diagonalmente dominante!"); | |
} | |
} | |
public void Jacobi(int numEquacoes, int iteracoes, double solucao[], double matriz[][], double resultados[]) { | |
double []c = new double[10]; | |
for (int k = 0; k < iteracoes; k++) { | |
for (int i = 0; i < numEquacoes; i++) { | |
c[i] = resultados[i]; | |
for (int j = 0; j < numEquacoes; j++) { | |
if (i != j) { | |
c[i] = c[i] - matriz[i][j] * solucao[j]; | |
} | |
} | |
} | |
for (int i = 0; i < 10; i++) { | |
solucao[i] = c[i]/matriz[i][i]; | |
} | |
} | |
System.out.println("A solução é: "); | |
for (int i = 0; i < numEquacoes; i++) { | |
System.out.println("x(" + i + ") = " + solucao[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment