-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gaussian.java
71 lines (68 loc) · 2.33 KB
/
Gaussian.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package gaussian;
import java.util.Scanner;
public class Gaussian {
public static void solve(double[][] A, double[] B){
int N = B.length;
for (int k = 0; k < N; k++)
{
for (int i = k + 1; i < N; i++){
double factor = A[i][k] / A[k][k];
B[i] -= factor * B[k];
for (int j = k; j < N; j++){
A[i][j] -= factor * A[k][j];
}
}
}
printRowEchelonForm(A, B);
/** back substitution **/
double[] solution = new double[N];
for (int i = N - 1; i >= 0; i--) {
double sum = 0.0;
for (int j = i + 1; j < N; j++) {
sum += A[i][j] * solution[j];
}
solution[i] = (B[i] - sum) / A[i][i];
}
/** Print solution **/
printSolution(solution);
}
/** function to print in row form **/
public static void printRowEchelonForm(double[][] A, double[] B){
int N = B.length;
System.out.println("\nRow Echelon form : ");
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
System.out.printf("%.3f ", A[i][j]);
}
System.out.printf("| %.3f\n", B[i]);
}
System.out.println();
}
/** function to print solution **/
public static void printSolution(double[] sol){
int N = sol.length;
System.out.println("\nSolution : ");
for (int i = 0; i < N; i++) {
System.out.printf("%.2f ", sol[i]);
}
System.out.println();
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("\nEnter number of equations:");
int N = scan.nextInt();
double[] B = new double[N];
double[][] A = new double[N][N];
System.out.println("\nEnter "+ N +" equations coefficients values:");
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
A[i][j] = scan.nextDouble();
}
}
System.out.println("\nEnter "+ N +" solutions");
for (int i = 0; i < N; i++){
B[i] = scan.nextDouble();
}
solve(A,B);
}
}