This set of equations is often referred to as a system of equations.
Wikipedia page: click here
3 x1 + 7 x2 = 78 8 x1 + 5 x2 = 45 |
Example 2: 3 simultaneous equations (with 3 unknowns x1, x2 and x3 )
5 x1 + 6 x2 - 3 x3 = 23 3 x1 - 4 x2 + 8 x3 = 25 -6 x1 + x2 - 2 x3 = 19 |
|
Example:
+- -+ | 5 6 -3 | A = | 3 -4 8 | | -6 1 -2 | +- -+ |
+- -+ | a11 a12 a13 | A = | a21 a22 a23 | | a31 a32 a33 | +- -+ |
|
Example: column vector
+- -+ | 2 | x = | 5 | | -1 | +- -+ |
+- -+ | x1 | x = | x2 | | x3 | +- -+ |
+- -+ +- -+ | a11 a12 a13 | | x1 | A×x = | a21 a22 a23 | × | x2 | | a31 a32 a33 | | x3 | +- -+ +- -+ +- -+ def | a11x1 + a12x2 + a13x3 | = | a21x1 + a22x2 + a23x3 | | a31x1 + a32x2 + a33x3 | +- -+ |
The product A×x is a vector !
Example:
5 x1 + 6 x2 - 3 x3 = 23 3 x1 - 4 x2 + 8 x3 = 25 -6 x1 + x2 - 2 x3 = 19 |
Wikipedia page: click here
|
2 x1 + 4 x2 = 16 3 x1 + 2 x2 = 12 |
The solution is:
x1 = 2 x1 = 3 Because: 2 × 2 + 4 × 3 = 4 + 12 = 16 3 × 2 + 2 × 3 = 6 + 6 = 12 |
|
2 x1 + 4 x2 = 16 3 x1 + 2 x2 = 12 |
|
|
|
|
It's called "trivial" because solving this system requires no effort at all...
The solution is:
x1 = value1 x2 = value2 |
2 x1 + 4 x2 = 16 3 x1 + 2 x2 = 12 |
|
Step 1B: make all other coefficients in the first column equal to ZERO (by adding a multiple of the first row to the other rows)
|
|
Step 2B: make all other coefficients in the second column equal to ZERO (by adding a multiple of the second row to the other rows)
|
Resulting trivial system of simultaneous equations:
x1 = 2 x2 = 3 |
Which also give the solution...
|
|
Simultaneous equations: 2 x1 + 4 x2 = 16 3 x1 + 2 x2 = 12 |
In matrix/vector form: +- -+ +- -+ +- -+ | 2 4 | | x1 | = | 16 | | 3 2 | | x2 | | 12 | +- -+ +- -+ +- -+ |
The element 2 in the matrix is coefficient a11
Simultaneous equations: x1 + 2 x2 = 8 -4 x2 = -12 |
In matrix/vector form: +- -+ +- -+ +- -+ | 1 2 | | x1 | = | 8 | | 0 -4 | | x2 | | -12 | +- -+ +- -+ +- -+ |
The element −4 in the matrix is coefficient a22
|
|
2 x1 + 4 x2 + 6 x3 = 14 x1 + 4 x2 + 3 x3 = 3 3 x1 + 5 x2 + 2 x3 = 2 |
We do not need to write out the variable x1, x2 and x3
We can represent these simulatneous equation by the following shorter form:
+- -+ | 2 4 6 | 14 | | 1 4 3 | 3 | | 3 5 2 | 2 | +- -+ |
Note:
|
|
x1 = 2 x2 = -2 x3 = 3 |
double[][] A; // The Coefficient "matrix" double[] b; // The right hand side "vector" int n; // number of unknowns (and size of A and b) |
Simultaneous equations: 2 x1 + 4 x2 + 6 x3 = 14 x1 + 4 x2 + 3 x3 = 3 3 x1 + 5 x2 + 2 x3 = 2 |
Representation: n = 3; double[][] A = new double[n][n]; double[] b = new double[n]; A[0][0] = 2; A[0][1] = 4; A[0][2] = 6; b[0] = 14; A[1][0] = 1; A[1][1] = 4; A[1][2] = 3; b[0] = 3; A[2][0] = 3; A[2][1] = 5; A[2][2] = 2; b[0] = 2; |
|
Read in n, A and b; for ( each row i from 0 to n-1 ) do { (Step i A): Select pivot pivot = A[i][i]; (Step i B): Normalize row i for ( each element in row i ) divide element by the pivot; (Step i C): Sweep column i; for row k from 0 to n-1, except row i do { factor = A[k][i]; Subtract "factor" × (row i) from (row k) } } |
|
|
import java.util.Scanner; public class Gauss1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); double[][] A; // The coefficients double[] b; // The RHS double pivot, factor; int n; // Number of variables int i, j, k; System.out.print("Number of unknows: "); n = in.nextInt(); // Read in problem size A = new double[n][n]; // Reserve space for coefficient matrix b = new double[n]; // Reserve space for RHS vector /* -------------------------------------- Read in the rows, one row at a time -------------------------------------- */ for (i = 0; i < n; i++) { // Read in row i's coefficients for (j = 0; j < n; j++) { System.out.print("A["+i+"]["+j+"] = "); A[i][j] = in.nextDouble(); } // Read in row i's RHS System.out.print("b["+i+"] = "); b[i] = in.nextDouble(); } /* ------------------------------------ The Gaussian elimination method ------------------------------------ */ for (i = 0; i < n; i++) { // Process row i /* --------------------- (A) Select pivot --------------------- */ pivot = A[i][i]; /* --------------------- (B) Normalize row i --------------------- */ for (j = 0; j < n; j++) A[i][j] = A[i][j]/pivot; b[i] = b[i]/pivot; /* --------------------- (C) Sweep using row i --------------------- */ for (k = 0; k < n; k++) { if ( k != i ) { factor = A[k][i]; for (j = 0; j < n; j++) A[k][j] = A[k][j] - factor*A[i][j]; b[k] = b[k] - factor*b[i]; } } } System.out.println("======================"); System.out.println("Solution:"); for (i = 0; i < n; i++) System.out.println("x" + (i+1) + " = " + b[i]); } } |
How to run the program:
|
|
|
public class MathTools { public static double[] GaussianElim(double[][] A, double[] b) { double pivot, factor; int i, j, k; int n = b.length; // # unknowns /* ----------------------------- Gaussian elimination method ----------------------------- */ for (i = 0; i < n; i++) { // Process row i /* --------------------- (A) Select pivot --------------------- */ pivot = A[i][i]; /* --------------------- (B) Normalize row i --------------------- */ for (j = 0; j < n; j++) A[i][j] = A[i][j]/pivot; b[i] = b[i]/pivot; /* --------------------- (C) Sweep using row i --------------------- */ for (k = 0; k < n; k++) { if ( k != i ) { factor = A[k][i]; for (j = 0; j < n; j++) A[k][j] = A[k][j] - factor*A[i][j]; b[k] = b[k] - factor*b[i]; } } } return(b); // The answer is in double[] b } } |
public class Gauss2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); double[][] A; double[] b, x; double pivot, factor; int n; // Number of variables int i, j, k; System.out.print("Number of unknows: "); n = in.nextInt(); // Read in problem size A = new double[n][n]; // Reserve space for coefficient matrix b = new double[n]; // Reserve space for RHS vector // Read in the rows, one row at a time for (i = 0; i < n; i++) { // Read in row i's coefficients for (j = 0; j < n; j++) { System.out.print("A["+i+"]["+j+"] = "); A[i][j] = in.nextDouble(); } // Read in row i's RHS System.out.print("b["+i+"] = "); b[i] = in.nextDouble(); } x = MathTools.GaussianElim(A, b); System.out.println("Solution:"); for (i = 0; i < n; i++) System.out.println("x[" + (i+1) + "] = " + x[i]); } } |
How to run the program:
|
Here's a sysem of 7 equations input file: click here
How to use the input:
|
|
|