|
Tenglamalar sistemasini Gauss usulida eshish
|
bet | 89/135 | Sana | 22.05.2024 | Hajmi | 15,08 Mb. | | #250347 |
Bog'liq Fizik jarayonlarni kompyuterda modellashtirish2. Tenglamalar sistemasini Gauss usulida eshish.
Masalaga ko’ra tenglamalar sistemasini Gauss usulida yechish uchun noma’lumlar oldidagi koeffitsientlardan hosil bo’lgan massiv diagonali quyi qismini 0 larga aylanitirishimiz kerak. Buni men qism dastur sifatida yozdim. Bunda esa yuqorida qayd qilingan operatorlar bilan birga FOR takrorlanish operatorini ham qo’lladim. Bu operator ma’lum o’zgaruvchi ma’lum qiymatga yetguncha tarkorlanishlarni bajaradigan operator hisoblanadi. Bunga misolni siz asosiy dasturda ko’rishingiz mumkin.
1 #include
2 using namespace std;
3 // Tenglamalar tizimini yechish
4 void sysout(double **a, double *y, int n)
5 {
6 for (int i = 0; i < n; i++)
7 {
8 for (int j = 0; j < n; j++)
9 {
10 cout << a[i][j] << "*x" << j;
11 if (j < n - 1)
12 cout << " + ";
13 }
14 cout << " = " << y[i] << endl;
15 }
16 return;
17 }
18 double * gauss(double **a, double *y, int n)
19 {
20 double *x, max;
21 int k, index;
22 const double eps = 0.00001; // aniqlik
23 x = new double[n];
24 k = 0;
25 while (k < n)
26 {
27 // a[i][k] maximal bilan satrni qidirish
28 max = abs(a[k][k]);
29 index = k;
30 for (int i = k + 1; i < n; i++)
31 {
32 if (abs(a[i][k]) > max)
33 {
34 max = abs(a[i][k]);
35 index = i;
36 }
37 }
38 // Satrlarni o’rnini almashtirish if (max < eps)
39 {
40 // nol bo’lmagan diagonal elementlar yo’q
41 cout << " Nolli ustun sababli yechimni olib bo’lmaydi ";
42 cout << index << " матрицы A" << endl;
43 return 0;
44 }
45 for (int j = 0; j < n; j++)
46 {
47 double temp = a[k][j];
48 a[k][j] = a[index][j];
49 a[index][j] = temp;
50 }
51 double temp = y[k];
52 y[k] = y[index];
53 y[index] = temp;
54 // Tenglamalarni normallashtirish
55 for (int i = k; i < n; i++)
56 {
57 double temp = a[i][k];
58 if (abs(temp) < eps) continue;
59 // nol koeffitsienti uchun tashlab ketish
60 for (int j = 0; j < n; j++)
61 a[i][j] = a[i][j] / temp;
62 y[i] = y[i] / temp;
63 if (i == k) continue;
64 // tenglamani o’zini o’zidan ayirmaslik
65 for (int j = 0; j < n; j++)
66 a[i][j] = a[i][j] - a[k][j];
67 y[i] = y[i] - y[k];
68 }
69 k++;
70 }
71 // teskari postanovka
72 for (k = n - 1; k >= 0; k--)
73 {
74 x[k] = y[k];
75 for (int i = 0; i < k; i++)
76 y[i] = y[i] - a[i][k] * x[k];
77 }
78 return x;
79 }
80 int main()
81 {
82 double **a, *y, *x;
83 int n;
84 system("chcp 1251");
85 system("cls");
86 cout << "Tenlamalar sonini kiriting: ";
87 cin >> n;
88 a = new double*[n];
89 y = new double[n];
90 for (int i = 0; i < n; i++)
91 {
92 a[i] = new double[n];
93 for (int j = 0; j < n; j++)
94 {
95 cout << "a[" << i << "][" << j << "]= ";
96 cin >> a[i][j];
97 }
98 }
99 for (int i = 0; i < n; i++)
100 {
101 cout << "y[" << i << "]= ";
102 cin >> y[i];
103 }
104 sysout(a, y, n);
105 x = gauss(a, y, n);
106 for (int i = 0; i < n; i++)
107 cout << "x[" << i << "]=" << x[i] << endl;
108 cin.get(); cin.get();
109 return 0;
110 }
|
| |