Flame of Soul
Главная | Регистрация | Вход

Мои записи


Главная » Статьи » Алгоритмы » Численные методы

[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)
Просмотров: 37348 | Комментарии: 14 | Рейтинг: 5.0/1 |
Всего комментариев: 141 2 »
14  
http://www.sarvajal.com - viagra

13  
However, there is a wide range of inflatable kayaks available today that are far from the clumsy inner tube-like boats one usually thinks of. http://www.cheapmonclerjacketsx.com/#62991 - moncler outlet uk - http://www.cheapmonclerjacketsx.com/#77361 - moncler shop , http://www.cheapmonclerjacketsx.com/#88271

12  
http://gfkdjghfkgjjkhj.com - gfkdjghfkgjjkh

11  
The very purpose of cd envelopes customization is that the customer can capably modify and transform the cd envelope printing design according to his or her requirements. http://www.cheapmonclercoatsx.com/#56713 - moncler jackets men - http://www.cheapmonclercoatsx.com/#41947 - authentic moncler jackets , http://www.cheapmonclercoatsx.com/#77465

10  
Now guys, here's some food for thought. http://www.cheaplouisvuittonbagsx.com/#18782 - louis vuitton handbags replica - http://www.cheaplouisvuittonbagsx.com/#46749 - authentic louis vuitton belts , http://www.cheaplouisvuittonbagsx.com/#22183

9  
More pieces always involves a better than usual level of fit. http://www.cheaplouisvuittonbagsx.com/#77294 - cheap louis vuitton handbags - http://www.cheaplouisvuittonbagsx.com/#28348 - authentic louis vuitton belts , http://www.cheaplouisvuittonbagsx.com/#18238

8  
Stay as dry as you can. http://www.cheapmonclercoatsx.com/#81519 - moncler outlet usa - http://www.cheapmonclercoatsx.com/#43969 - moncler usa official website , http://www.cheapmonclercoatsx.com/#66151

7  
When it comes to god and prayers, they do prefer to wear something that is well-defined. http://www.cheapmonclercoatsx.com/#72881 - moncler jackets for kids - http://www.cheapmonclercoatsx.com/#57268 - moncler sale coat , http://www.cheapmonclercoatsx.com/#85955

6  
Corporate people are consistently stuck in freezing offices. http://www.cheapmonclercoatsx.com/#56625 - moncler usa website - http://www.cheapmonclercoatsx.com/#98538 - moncler sale , http://www.cheapmonclercoatsx.com/#52461

5  
Avodart can be administered in coalesce with tamsulosin or <a href= "http://www.dutasterideavodart.com" >buy avodart</a> as monotherapy. Eat the sedate with or without food. Teeth of the happening <a href= "http://www.dutasteridebuyavodart.com" >buy avodart</a> that the unfailing effect of treatment Avodart may decease right away, the <a href= "http://www.avodartbuydutasteride.com" >cheap avodart</a> drug should be continued representing at least 6 months.

1-10 11-14
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]

Форма входа

Поиск

Статистика


Онлайн всего: 1
Гостей: 1
Пользователей: 0