Masalani yechish algaritm

|
Masalani yechish algaritm
|
bet | 2/2 | Sana | 20.05.2024 | Hajmi | 1,37 Mb. | | #246151 |
Bog'liq MAMARAIMOV ELYORMasalani yechish algaritm:
Integral javobi
To’gri to’rtburchaklar usuli
Dasturi
#include
#include
using namespace std;
double f(double x) {
double f = pow(5,x)/(7*x-1);
return f;
}
double rectangleMethod(double a, double b, int n) {
double step = (b - a) / n;
double sum = 0.0;
for (int i = 0; i < n; ++i) {
double x = a + i * step;
sum += f(x);
}
return sum * step;
}
int main() {
double a, b;
int n;
cout << "Intervallarni kiriting pastki va yuqori: ";
cin >> a >>b;
cout << "Bo'laklar sonini kiriting (n): ";
cin >> n;
double result = rectangleMethod(a, b, n);
cout << "Integralning qiymati: " << result << endl;
return 0;}
NATIJA
n
|
10
|
20
|
30
|
50
|
100
|
1000
|
10000
|
100000
|
F(x)
Integral qiymati
67.33007
|
50.82
|
58.65
|
61.44
|
63.75
|
65.52
|
67.1481
|
67.31
|
67.32
|
Trapetsiya usuli
Dastur kodi:
#include
#include
using namespace std;
double f(double x) {
double f = pow(5,x)/(7*x-1);
return f;
}
double trapezoidalMethod(double a, double b, int n) {
double step = (b - a) / n;
double sum = 0.5 * (f(a) + f(b));
for (int i = 1; i < n; ++i) {
double x = a + i * step;
sum += f(x);
}
return sum * step;
}
int main() {
double a, b;
int n;
cout << "Intervallarni kiriting (a va b): ";
cin >> a >> b;
cout << "Bo'laklar sonini kiriting (n): ";
cin >> n;
double result = trapezoidalMethod(a, b, n);
cout << "Integralning qiymati: " << result << endl;
return 0;
}
Natija:
n
|
10
|
20
|
30
|
50
|
100
|
1000
|
10000
|
100000
|
F(x)
Integral qiymati=
67.33007
|
69.037
|
67.7583
|
67.52
|
67.3987
|
67.3472
|
67.3302
|
67.3301
|
67.33
|
XULOSA:
Ushbu dastur foydalanuvchiga matritsani kiritish, kiritilgan matritsani ekranga chiqarish va har bir ustundagi juft elementlar yig'indisidan tashkil topgan B massivni hosil qilish imkonini beradi. Dastur nafaqat algoritmik fikrlash va massivlar bilan ishlashni o'rganishga yordam beradi, balki foydalanuvchiga kiritilgan ma'lumotlarni qayta ishlash va natijalarni vizual tarzda ko'rish imkoniyatini ham taqdim etadi. Bu dastur orqali foydalanuvchi matritsa elementlarini kiritish va juft elementlar yig'indisidan tashkil topgan B massivini hisoblash bo'yicha amaliy ko'nikmalar hosil qiladi.To'g'ri to'rtburchaklar va trapetsiyalar usullari matematik funksiyalar integralini hisoblashda samarali vosita bo'lib, analitik yondashuvlar qo'llanilmaydigan hollarda muhim ahamiyatga ega. Bu dasturlar talabalarga va dasturchilarga integralni numerik usulda hisoblashni tushunishga yordam beradi.
Shaxsiy topshiriq
N=14
n1={14/3}+1, n2={14/5}+1, n3={14/7}+1
n1=3, n2=5, n3=1
15x1+10x2+5x3 35n1+35n2+20n3
10x1+4x2+12x3 32n1+18n2+28n3
4x1+15x2+10x3 18n1+24n2+35n3
L=1800x1+2000x2+1500x3 max
x1,x2,x3 0
15x1+10x2+5x3 300
10x1+4x2+12x3 214
4x1+15x2+10x3 209
L=1800x1+2000x2+1500x3 max
x1,x2,x3 0
15y1+10y2+4y3 1800
10y1+4y2+15y3 2000
5y1+12y2+10y3 1500
Z=180y1+156y2+154y3 min
15x1+10x2+5x3 300
10x1+4x2+12x3 214
4x1+15x2+10x3 209
L=1800x1+2000x2+1500x3+x4+x5+x6 max
dastur
#include
#include
#include
using namespace std;
vector> A = {{15, 10, 5}, {10, 4, 12}, {4, 15, 10}};
vector b = {300,214,209};
vector c = {1800, 2000, 1500};
vector solveSimplex(const vector>& A, const vector& b, const vector& c) {
int m = A.size(); // Number of equations
int n = c.size(); // Number of variables
// Initialize the tableau
vector> tableau(m + 1, vector(n + m + 1));
vector basis(m);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
tableau[i][j] = A[i][j];
}
tableau[i][n + i] = 1;
basis[i] = n + i;
tableau[i][n + m] = b[i];
}
for (int j = 0; j < n; ++j) {
tableau[m][j] = -c[j];
}
while (true) {
int q = 0;
for (int j = 1; j < n + m; ++j) {
if (tableau[m][j] < tableau[m][q]) {
q = j;
}
}
if (tableau[m][q] >= 0) {
break;
}
int p = -1;
double minRatio = numeric_limits::infinity();
for (int i = 0; i < m; ++i) {
if (tableau[i][q] <= 0) {
continue;
}
double ratio = tableau[i][n + m] / tableau[i][q];
if (ratio < minRatio) {
minRatio = ratio;
p = i;
}
}
if (p == -1) {
throw logic_error("Unbounded solution");
}
for (int i = 0; i <= m; ++i) {
if (i != p) {
double alpha = tableau[i][q] / tableau[p][q];
for (int j = 0; j <= n + m; ++j) {
tableau[i][j] -= alpha * tableau[p][j];
}
}
}
basis[p] = q;
}
vector result(n);
for (int i = 0; i < m; ++i) {
if (basis[i] < n) {
result[basis[i]] = tableau[i][n + m] / tableau[i][basis[i]];
}
}
return result;
}
int main() {
vector result = solveSimplex(A, b, c);
cout << "Result: ";
for (double val : result) {
cout << val << " ";
}
cout << endl;
double maxValue = 0;
for (int i = 0; i < c.size(); ++i) {
maxValue += c[i] * result[i];
}
cout << "Maximum value: " << maxValue << endl;
return 0;
}
natija
Xulosa:
Berilgan masalaning simpleks usulda yechilishi chiziqli dasturlash muammolarini optimal hal qilishda simpleks usulining samaradorligini yana bir bor tasdiqladi. Ushbu usul o'zining aniqligi va tezkorligi bilan ajralib turadi. Excel yordamida yechim chiqarish jarayonini avtomatlashtirish va natijalarni tezroq olish imkonini beradi. Bu ish nafaqat chiziqli dasturlash nazariyasini mustahkamlashda, balki amaliy muammolarni hal qilishda ham foydali bo'ldi. Simpleks usuli yordamida muammolarni tizimli va izchil hal qilish mumkinligi yana bir bor tasdiqlandi.
Ushbu mustaqil ish davomida o'rgangan simpleks usulining qadamlarini qo'llash orqali har qanday chiziqli dasturlash muammosini samarali hal qilish mumkinligi aniqlandi. Bu bilimlar kelajakda boshqa murakkab muammolarni yechishda ham foydali bo'ladi.
|
| |