3 x_{1} + 7 x_{2} = 78 8 x_{1} + 5 x_{2} = 45 |
And:
5 x_{1} + 6 x_{2} - 3 x_{3} = 23 3 x_{1} - 4 x_{2} + 8 x_{3} = 25 -6 x_{1} + x_{2} - 2 x_{3} = 19 |
You can use this program to solve any systems of simultaneous equations...
2 x_{1} + 4 x_{2} = 16 3 x_{1} + 2 x_{2} = 12 |
2 x_{1} + 4 x_{2} = 16 3 x_{1} + 2 x_{2} = 12 |
Example: multiply first equation by 2 and second equation by 3
0.5 × (2 x_{1} + 4 x_{2}) = 0.5 × 16 ==> x_{1} + 2 x_{2} = 8 3 x_{1} + 2 x_{2} = 12 |
Example: subtract three time the first equation to the second equation
x_{1} + 2 x_{2} = 8 (1) 3 x_{1} + 2 x_{2} = 12 (2) (2) ===> 3 x_{1} + 2 x_{2} = 12 3 x (1) ===> 3 x_{1} + 6 x_{2} = 24 - --------------------------- 0 x_{1} - 4 x_{2} = -12 |
The "trivial" system of simultaneous equations is the following system:
x_{1} + 0 × x_{2} = ... 0 × x_{1} + x_{2} = ... |
It's called "trivial" because solving this system requires no effort at all.
2 x_{1} + 4 x_{2} = 16 3 x_{1} + 2 x_{2} = 12 |
(a) Multiply first equation by 0.5 (or divide by 2) - intermediate result:
1 x_{1} + 2 x_{2} = 8 3 x_{1} + 2 x_{2} = 12 |
(b) Subtract three time equation 1 from equation 2 - intermediate result:
1 x_{1} + 2 x_{2} = 8 0 x_{1} - 4 x_{2} = -12 |
(a) Multiply second equation by -4 - intermediate result:
1 x_{1} + 2 x_{2} = 8 0 x_{1} + 1 x_{2} = 3 |
(b) Subtract 2 times equation 2 from equation 1 - intermediate result:
1 x_{1} + 0 x_{2} = 2 0 x_{1} + 1 x_{2} = 3 |
Trivial system obtained. Solution:
x_{1} = 2 x_{2} = 3 |
2 x_{1} + 4 x_{2} + 6 x_{3} = 14 x_{1} + 4 x_{2} + 3 x_{3} = 3 3 x_{1} + 5 x_{2} + 2 x_{3} = 2 |
2 x_{1} + 4 x_{2} + 6 x_{3} = 14 x_{1} + 4 x_{2} + 3 x_{3} = 3 3 x_{1} + 5 x_{2} + 2 x_{3} = 2 |
1 x_{1} + 2 x_{2} + 3 x_{3} = 7 x_{1} + 4 x_{2} + 3 x_{3} = 3 3 x_{1} + 5 x_{2} + 2 x_{3} = 2 |
1 x_{1} + 2 x_{2} + 3 x_{3} = 7 --+ -----+ _{ }_{ } | -1 | -3 x_{1} + 4 x_{2} + 3 x_{3} = 3 <-+ | | 3 x_{1} + 5 x_{2} + 2 x_{3} = 2 <------------+ |
Result:
1 x_{1} + 2 x_{2} + 3 x_{3} = 7 _{ } + 2 x_{2} + _{ } = -4 _{ } - x_{2} - 7 x_{3} = -19 |
x_{1} + 2 x_{2} + 3 x_{3} = 7 _{ } + 2 x_{2} + _{ } = -4 _{ } - x_{2} - 7 x_{3} = -19 |
x_{1} + 2 x_{2} + 3 x_{3} = 7 _{ } + 1 x_{2} + _{ } = -2 _{ } - x_{2} - 7 x_{3} = -19 |
x_{1} + _{ } + 3 x_{3} = 11 _{ } + 1 x_{2} + _{ } = -2 _{ } _{ } - 7 x_{3} = -21 |
x_{1} + _{ } + 3 x_{3} = 11 _{ } + x_{2} + _{ } = -2 _{ } _{ } - 7 x_{3} = -21 |
x_{1} + _{ } + 3 x_{3} = 11 _{ } + x_{2} + _{ } = -2 _{ } _{ } + 1 x_{3} = 3 |
x_{1} + _{ } + _{ } = 2 _{ } + x_{2} + _{ } = -2 _{ } _{ } + 1 x_{3} = 3 |
x_{1} = 2 x_{2} = -2 x_{3} = 3 |
Initial system of equations: 2 x_{1} + 4 x_{2} + 6 x_{3} = 14 x_{1} + 4 x_{2} + 3 x_{3} = 3 3 x_{1} + 5 x_{2} + 2 x_{3} = 2 Will be eventually reduced to the trivial system: 1 x_{1} + 0 x_{2} + 0 x_{3} = 2 0 x_{1} + 1 x_{2} + 0 x_{3} = -2 1 x_{1} + 0 x_{2} + 1 x_{3} = 3 |
Clearly, when you obtained the trivial system, the solution for x_{1}, x_{2} and x_{3} can be "read" off the right hand side !
double[][] A; // The Coefficient "matrix" double[] b; // The right hand side "vector" int n; // number of unknowns (and size of A and b) |
System of equations: 2 x_{1} + 4 x_{2} + 6 x_{3} = 14 x_{1} + 4 x_{2} + 3 x_{3} = 3 3 x_{1} + 5 x_{2} + 2 x_{3} = 2 |
n = 3; A = new double[n][n]; 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 row i from 0 to n-1 { (1) Normalize row i: pivot = A[i][i]; divide coefficients in row i by pivot; (2) Sweep other rows using row i: for row k from 0 to n-1, except row i { factor = A[k][i]; Subtract "factor" times row i from row k } } |
Read in n, A and b; int i, j, k; double pivot, factor; for ( i = 0; i < n; i++ ) { // (1) Normalize row i: pivot = A[i][i]; for (j = 0; j < n; j++) A[i][j] = A[i][j] / pivot; // divide coeff. in row i by pivot; b[i] = b[i] / pivot; // (2) Sweep other rows using row i: (do NOT sweep 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]; // Subtract "factor" times row i from row k b[k] = b[k] - factor*b[i]; } } } |
or