Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "header.h"
- void systemOfLinearEquations::GaussFormula()
- {
- for(int i = 0 ; i < numEquations ; ++i)
- {
- double maxElement = abs(supplementMatrix[i][i]);
- int maxRow = i;
- for(int j = i + 1 ; j < numEquations ; ++j)
- {
- if(abs(supplementMatrix[j][i]) > maxElement)
- {
- maxElement = abs(supplementMatrix[j][i]);
- maxRow = j;
- }
- }
- for(int j = i ; j < numEquations + 1 ; ++j)
- {
- double temp = supplementMatrix[maxRow][j];
- supplementMatrix[maxRow][j] = supplementMatrix[i][j];
- supplementMatrix[i][j] = temp;
- }
- for(int j = i + 1 ; j < numEquations; ++j)
- {
- double multiplier = -supplementMatrix[j][i] / supplementMatrix[i][i];
- for(int k = i ; k < numEquations + 1 ; ++k)
- {
- if (i == k)
- {
- supplementMatrix[j][k] = 0;
- }
- else
- {
- supplementMatrix[j][k] += multiplier * supplementMatrix[i][k];
- }
- }
- }
- }
- double* solution;
- allocateArray1D(solution, numEquations);
- for(int i = numEquations - 1 ; i >= 0 ; --i)
- {
- solution[i] = supplementMatrix[i][numEquations] / supplementMatrix[i][i];
- for(int k = i - 1 ; k >= 0 ; --k)
- {
- supplementMatrix[k][numEquations] -= supplementMatrix[k][i] * solution[i];
- }
- }
- systemOfLinearEquations::getDefaultSupplementMatrix();
- cout << endl;
- for(int i = 0 ; i < numEquations ; ++i)
- {
- cout << "x" << i + 1 << " = " << solution[i]<<endl;
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement