|
Tekshirdi : Babajanov Mo’min
|
Sana | 14.05.2024 | Hajmi | 237,63 Kb. | | #230476 |
Bog'liq jasural3
O’zbekiston Respublikasi Raqamli Texnologiyalar Vazirligi Muhammad al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari Universiteti
Algoritmlarni loyihalash fani bo’yicha
Amaliy ish
Bajardi : Shukrullayev Jasurbek
Tekshirdi : Babajanov Mo’min
Toshkent – 2024
3-Amaliy ish
Berilgan tenglamalar sistemasi Oddiy iteratsiya, Zeydel iteratsiya, Relaqsatsiya iteratsiya usuli bilan yeching. Bunda har bir iteratsiya qadamini hisoblang. Natijalarni jadval ko’rinishida keltiring. Yaqinlashish xatoliklarini har qadamda ko’rsating. Shu uchta usul uchun dastur tuzing va natijalarni solishtiring.
15 – variant
#include
#include
#include
using namespace std;
void printEquations(const vectordouble>>& equations) {
for (const auto& equation : equations) {
for (const auto& coef : equation) {
cout << coef << " ";
}
cout << endl;
}
}
vector<double> simpleIteration(const vectordouble>>& equations, const vector<double>& initialGuess, double epsilon, int maxIterations) {
vector<double> result(initialGuess.size(), 0.0);
vector<double> nextResult(initialGuess.size(), 0.0);
for (int iter = 0; iter < maxIterations; ++iter) {
for (size_t i = 0; i < equations.size(); ++i) {
double sum = 0.0;
for (size_t j = 0; j < equations[i].size() - 1; ++j) {
if (j != i) {
sum += equations[i][j] * nextResult[j];
}
}
nextResult[i] = (equations[i].back() - sum) / equations[i][i];
}
double maxDiff = 0.0;
for (size_t i = 0; i < result.size(); ++i) {
double diff = abs(result[i] - nextResult[i]);
if (diff > maxDiff) {
maxDiff = diff;
}
result[i] = nextResult[i];
}
if (maxDiff < epsilon) {
return result;
}
}
return result;
}
vector<double> gaussSeidel(const vectordouble>>& equations, const vector<double>& initialGuess, double epsilon, int maxIterations) {
vector<double> result(initialGuess.size(), 0.0);
vector<double> nextResult(initialGuess.size(), 0.0);
for (int iter = 0; iter < maxIterations; ++iter) {
for (size_t i = 0; i < equations.size(); ++i) {
double sum = 0.0;
for (size_t j = 0; j < equations[i].size() - 1; ++j) {
if (j != i) {
sum += equations[i][j] * (j < i ? nextResult[j] : result[j]);
}
}
nextResult[i] = (equations[i].back() - sum) / equations[i][i];
}
double maxDiff = 0.0;
for (size_t i = 0; i < result.size(); ++i) {
double diff = abs(result[i] - nextResult[i]);
if (diff > maxDiff) {
maxDiff = diff;
}
result[i] = nextResult[i];
}
if (maxDiff < epsilon) {
return result;
}
}
return result;
}
vector<double> relaxation(const vectordouble>>& equations, const vector<double>& initialGuess, double omega, double epsilon, int maxIterations) {
vector<double> result(initialGuess.size(), 0.0);
vector<double> nextResult(initialGuess.size(), 0.0);
for (int iter = 0; iter < maxIterations; ++iter) {
for (size_t i = 0; i < equations.size(); ++i) {
double sum = 0.0;
for (size_t j = 0; j < equations[i].size() - 1; ++j) {
if (j != i) {
sum += equations[i][j] * nextResult[j];
}
}
nextResult[i] = result[i] + omega * ((equations[i].back() - sum) / equations[i][i] - result[i]);
}
double maxDiff = 0.0;
for (size_t i = 0; i < result.size(); ++i) {
double diff = abs(result[i] - nextResult[i]);
if (diff > maxDiff) {
maxDiff = diff;
}
result[i] = nextResult[i];
}
if (maxDiff < epsilon) {
return result;
}
}
return result;
}
int main() {
vectordouble>> equations = {
{7.8, 5.3, 4.8, 1.8},
{3.3, 1.1, 1.8, 2.3},
{4.5, 3.3, 2.8, 3.4}
};
cout << endl;
vector<double> initialGuess = {0.0, 0.0, 0.0};
cout << "Tenglamalar tizimi:" << endl;
printEquations(equations);
cout << endl;
cout << "Oddiy iteratsiya natijasi:" << endl;
vector<double> simpleIterResult = simpleIteration(equations, initialGuess, 0.0001, 100);
for (size_t i = 0; i < simpleIterResult.size(); ++i) {
cout << "x_" << i + 1 << " = " << simpleIterResult[i] << endl;
}
cout << endl;
cout << "Zeydel iteratsiya natijasi:" << endl;
vector<double> gaussSeidelResult = gaussSeidel(equations, initialGuess, 0.0001, 100);
for (size_t i = 0; i < gaussSeidelResult.size(); ++i) {
cout << "x_" << i + 1 << " = " << gaussSeidelResult[i] << endl;
}
cout << endl;
cout << "Relaksatsiya iteratsiya natijasi:" << endl;
vector<double> relaxationResult = relaxation(equations, initialGuess, 1.2, 0.0001, 100);
for (size_t i = 0; i < relaxationResult.size(); ++i) {
cout << "x_" << i + 1 << " = " << relaxationResult[i] << endl;
}
cout << endl;
return 0;
}
|
| |