Мои записи
[CPP] Метод Зейделя решения СЛАУ
== Magic Technologic Inc.==
Volume 0x0c, Algoritms 0x05, Phile #0x0a of 0x0f
|=-----------------------------------------------------------------=| |=----------------=[ Метод Зейделя решения СЛАУ ]=----------------=| |=-----------------------------------------------------------------=| |=----------------=[ Copyright (c) Flame Of Soul ]=----------------=| |=---------------=[ [E-mail]: _allbasse@yandex.ru ]=---------------=| |=-----------------------------------------------------------------=| |=-----------------------------=[ CPP ]=---------------------------=| |=-----------------------------------------------------------------=| #include "stdio.h" #include "iostream.h" #include "math.h" bool getDiagonal( double **coefficients, double *rightPart, int currColumn, int numberOfEquation ) { bool result = false; int i, row = currColumn; double tempItem; if ( currColumn == numberOfEquation - 1 ) { result = coefficients[currColumn][currColumn] != 0;} else { while ( !result && row < numberOfEquation ) { if ( coefficients[row][currColumn] != 0 ) { if ( row != currColumn ) { for ( i = 0; i < numberOfEquation; i++ ) { tempItem = coefficients[currColumn][i]; coefficients[currColumn][i] = coefficients[row][i]; coefficients[row][i] = tempItem;} tempItem = rightPart[currColumn]; rightPart[currColumn] = rightPart[row]; rightPart[row] = tempItem;} result = getDiagonal( coefficients, rightPart, currColumn + 1, numberOfEquation ); if (result) {break;}}row ++;}} return result;} int iteration( double **coefficients, double *rightPart, int numberOfEquation, double *solution, double precision ) { bool result; int i, j, k, step = 1; double temp; double* tempSolution; tempSolution = new double[numberOfEquation]; result = getDiagonal( coefficients, rightPart, 0, numberOfEquation ); if ( result ) { double fault = precision + 1; for ( i = 0; i < numberOfEquation; i ++ ) { for ( j = 0; j < numberOfEquation; j ++ ) { if ( i != j ) { coefficients[i][j] = - coefficients[i][j] / coefficients[i][i];}} rightPart[i] = rightPart[i] / coefficients[i][i]; coefficients[i][i] = 0;} for ( i = 0; i < numberOfEquation; i ++ ) { solution[i] = rightPart[i];} while ( fault > precision && step <= 1000 ) { for ( j = 0; j < numberOfEquation; j ++ ) { tempSolution[j] = 0;} for ( i = 0; i < numberOfEquation; i ++ ) { for ( j = 0; j < i; j ++ ) { tempSolution[i] = tempSolution[i] + coefficients[i][j] * tempSolution[j];} for ( j = i; j < numberOfEquation; j ++ ) { tempSolution[i] = tempSolution[i] + coefficients[i][j] * solution[j];} tempSolution[i] = tempSolution[i] + rightPart[i];} fault = 0; for ( j = 0; j < numberOfEquation; j ++ ) { fault = fault + (solution[j] - tempSolution[j])* (solution[j] - tempSolution[j]);} fault = sqrt( fault ); for ( j = 0; j < numberOfEquation; j ++ ) { solution[j] = tempSolution[j];} step++;}} else {result = 1001;} return step;}
void main() { int i, j; int size; double **coefficients, *rightPart, *solution, precision; cout << "Simple iteration method.\nEnter system dimension: "; cin >> size; coefficients = new double*[size]; for ( i = 0; i < size; i++ ) { coefficients[i] = new double[size];} rightPart = new double[size]; solution = new double[size]; for ( i = 0; i < size; i ++ ){ cout << "Enter " << i + 1 << " row: "; for ( j = 0; j < size; j ++ ){ cin >> coefficients[i][j];}} cout << "Enter right part: "; for ( j = 0; j < size; j ++ ){ cin >> rightPart[j];} cout << "Enter precision: "; cin >> precision; int steps = iteration( coefficients, rightPart, size, solution, precision ); if ( steps > 1000 ) { cout << "Solution for this matrix of coefficients not exist or not found";} else { cout << "Solution is:\n"; for ( j = 0; j < size; j ++ ){ cout << solution[j] << "\n";} cout << "Number of step: " << steps;} cout << "\nPress \"Enter\" to continue..." << endl; getchar();} |=-----------------------------=[ CPP ]=---------------------------=| |=-----------------------------------------------------------------=|
|
Категория: Численные методы | Добавил: flame (23.05.2009)
|
Просмотров: 759
| Рейтинг: 0.0/0 |
|
Добавлять комментарии могут только зарегистрированные пользователи. [ Регистрация | Вход ]
|
Статистика
Онлайн всего: 1 Гостей: 1 Пользователей: 0
![](/.s/t/959/12.gif)
|