Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Шпоры по МПиПА / Численные методы / Метод прогонки / Исходник / SLAU
.cpp#include "stdio.h"
#include "iostream.h"
// было ли найдено решение, если да - итог в параметре solution
bool progonka( double *aCoefficients, double *bCoefficients, double *cCoefficients, double *dCoefficients, int numberOfEquation, double *solution ) {
bool result;
int i, k;
// прямой ход метода прогонки - нахождение вспомогательных групп коэффициентов P и Q
double *pCoefficients, *qCoefficients;
pCoefficients = new double[numberOfEquation];
qCoefficients = new double[numberOfEquation];
if ( bCoefficients[0] == 0 ) {// во избежания деления на ноль
return false;
}
// определение первых кэффициентов массивoв P и Q
pCoefficients[0] = - cCoefficients[0] / bCoefficients[0];
qCoefficients[0] = dCoefficients[0] / bCoefficients[0];
// пределение сновной части коэффициентов массивoв P и Q, через уже найденные
double temp;
for ( i = 1; i < numberOfEquation - 1; i ++ ){
temp = bCoefficients[i] + aCoefficients[i]*pCoefficients[i-1];
if ( temp == 0 ) {
return false;
}
pCoefficients[i] = - cCoefficients[i] / temp;
qCoefficients[i] = ( dCoefficients[i] - aCoefficients[i]*qCoefficients[i-1] ) / temp;
}
// определение последних коэффициентов массивoв P и Q
temp = bCoefficients[numberOfEquation - 1] + aCoefficients[numberOfEquation - 1]*pCoefficients[numberOfEquation - 2];
if ( temp == 0 ) {
return false;
}
pCoefficients[numberOfEquation - 1] = 0;
qCoefficients[numberOfEquation - 1] = ( dCoefficients[numberOfEquation - 1] - aCoefficients[numberOfEquation - 1]*qCoefficients[numberOfEquation - 2] ) / temp;
// обратный ход метода прогонки - собственно писк решения
solution[numberOfEquation - 1] = qCoefficients[numberOfEquation - 1];
for ( i = numberOfEquation - 2; i >= 0; i -- ){
solution[i] = pCoefficients[i]*solution[i+1] + qCoefficients[i];
}
return result;
}
void main() {
int i, j;
int size;
// маассивы элементов с 3 диагоналей и массив правых частей
double *aCoefficients, *bCoefficients, *cCoefficients, *dCoefficients, *solution;
cout << "Metod progonki.\nEnter system dimension: ";
cin >> size;
if ( size < 1 ) {
cout << "size must be >= 2.";
}
else {
aCoefficients = new double[size];
bCoefficients = new double[size];
cCoefficients = new double[size];
dCoefficients = new double[size];
solution = new double[size];
// заполнение массивов
aCoefficients[0] = cCoefficients[size-1] = 0;
cout << "Enter " << 1 << " row: ( 2 elements )";
cin >> bCoefficients[0];
cin >> cCoefficients[0];
for ( i = 1; i < size - 1; i ++ ){
cout << "Enter " << i + 1 << " row: ( 3 elements )";
cin >> aCoefficients[i];
cin >> bCoefficients[i];
cin >> cCoefficients[i];
}
cout << "Enter " << size << " row: ( 2 elements )";
cin >> aCoefficients[size - 1];
cin >> bCoefficients[size - 1];
cout << "Enter right part: ";
for ( j = 0; j < size; j ++ ){
cin >> dCoefficients[j];
}
// если метод прогонки смог сработать - выписать результат
if ( !progonka( aCoefficients, bCoefficients, cCoefficients, dCoefficients, size, solution ) ) {
cout << "Solution for this matrix of coefficients not exist";
}
else {
cout << "Solution is:\n";
for ( j = 0; j < size; j ++ ){
cout << solution[j] << "\n";
}
}
}
cout << "\nPress \"Enter\" to continue..." << endl;
getchar();
}