Мои записи
[CPP] Метод Якоби решения СЛАУ
== Magic Technologic Inc.==
Volume 0x0c, Algoritms 0x07, 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 < numberOfEquation; j ++ ) { tempSolution[i] = tempSolution[i] + coefficients[i][j] * solution[j];} tempSolution[i] = tempSolution[i] + rightPart[i];} fault = 0.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)
|
| Просмотров: 38208 | Комментарии: 3
| Рейтинг: 5.0/1 |
|
| Всего комментариев: 3 | |
|
3
sotvoish
(20.07.2012 05:21)
0
I am concerned about the circumstances in Syria!
|
2
ToplootaLapd
(27.06.2012 20:01)
0
I'm waiting for the incontrovertible of Euro 2012. All spoke about Ukraine - a lie!
|
1
sypeteelt
(05.04.2012 18:59)
0
Добрый вечер! Информацию с описанием моейработы были действительно завершена, приступим к реализации уйти .
|
|
|
|
Добавлять комментарии могут только зарегистрированные пользователи. [ Регистрация | Вход ]
|
Статистика
Онлайн всего: 1 Гостей: 1 Пользователей: 0

|