3-laboratoriya ishi «Matritsalar bilan ishlash va chiziqli tenglamalar sistemasini yechish»
Download 413.84 Kb.
|
AL 3-laboratoriya ishi
- Bu sahifa navigatsiya:
- Chiziqli tenglamalar sistemasini Kramer usuli yordamida yechish dasturi
- TOPSHIRIQ
- Вариант №2
#include using namespace std; // Вывод системы уравнений void sysout(double **a, double *y, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << a[i][j] << "*x" << j; if (j < n - 1) cout << " + "; } cout << " = " << y[i] << endl; } return; } double * gauss(double **a, double *y, int n) { double *x, max; int k, index; const double eps = 0.00001; // точность x = new double[n]; k = 0; while (k < n) { // Поиск строки с максимальным a[i][k] max = abs(a[k][k]); index = k; for (int i = k + 1; i < n; i++) { if (abs(a[i][k]) > max) { max = abs(a[i][k]); index = i; } } // Перестановка строк if (max < eps) { // нет ненулевых диагональных элементов cout << "Решение получить невозможно из-за нулевого столбца "; cout << index << " матрицы A" << endl; return 0; } for (int j = 0; j < n; j++) { double temp = a[k][j]; a[k][j] = a[index][j]; a[index][j] = temp; } double temp = y[k]; y[k] = y[index]; y[index] = temp; // Нормализация уравнений for (int i = k; i < n; i++) { double temp = a[i][k]; if (abs(temp) < eps) continue; // для нулевого коэффициента пропустить for (int j = 0; j < n; j++) a[i][j] = a[i][j] / temp; y[i] = y[i] / temp; if (i == k) continue; // уравнение не вычитать само из себя for (int j = 0; j < n; j++) a[i][j] = a[i][j] - a[k][j]; y[i] = y[i] - y[k]; } k++; } // обратная подстановка for (k = n - 1; k >= 0; k--) { x[k] = y[k]; for (int i = 0; i < k; i++) y[i] = y[i] - a[i][k] * x[k]; } return x; } int main() { double **a, *y, *x; int n; system("chcp 1251"); system("cls"); cout << "Введите количество уравнений: "; cin >> n; a = new double*[n]; y = new double[n]; for (int i = 0; i < n; i++) { a[i] = new double[n]; for (int j = 0; j < n; j++) { cout << "a[" << i << "][" << j << "]= "; cin >> a[i][j]; } } for (int i = 0; i < n; i++) { cout << "y[" << i << "]= "; cin >> y[i]; } sysout(a, y, n); x = gauss(a, y, n); for (int i = 0; i < n; i++) cout << "x[" << i << "]=" << x[i] << endl; cin.get(); cin.get(); return 0; } Chiziqli tenglamalar sistemasini Kramer usuli yordamida yechish dasturi #include using namespace std; int determinant(int matrix[3][3]); int determinantX1(int coefMatrix[3][3], int constTermsMatrix[3]); int determinantX2(int coefMatrix[3][3], int constTermsMatrix[3]); int determinantX3(int coefMatrix[3][3], int constTermsMatrix[3]); int main() { int i, j; int coefficientsMatrix3x3[3][3]; int constantTermsMatrix3x1[3]; cout << "Vvedite koefficienty i sbobodnye chleny " << endl; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { cout << "a[ " << i << "," << j << "]= "; cin >> coefficientsMatrix3x3[i][j]; } cout << "b,[ " << i << "]= "; cin >> constantTermsMatrix3x1[i]; } int det = determinant(coefficientsMatrix3x3); int detX1 = determinantX1(coefficientsMatrix3x3, constantTermsMatrix3x1); int detX2 = determinantX2(coefficientsMatrix3x3, constantTermsMatrix3x1); int detX3 = determinantX3(coefficientsMatrix3x3, constantTermsMatrix3x1); if (det != 0) { cout << "X1 = " << (float)detX1/(float)det << endl; cout << "X2 = " << (float)detX2/(float)det << endl; cout << "X3 = " << (float)detX3/(float)det << endl; } else cout << "Sistema ne imejet reshenij " << endl << endl; return 0; } int determinant(int matrix[3][3]) { int a11 = matrix[0][0]; int a12 = matrix[0][1]; int a13 = matrix[0][2]; int a21 = matrix[1][0]; int a22 = matrix[1][1]; int a23 = matrix[1][2]; int a31 = matrix[2][0]; int a32 = matrix[2][1]; int a33 = matrix[2][2]; return (a11 * a22 * a33) + (a12 * a23 * a31) + (a13 * a21 * a32) - (a13 * a22 * a31) - (a11 * a23 * a32) - (a12 * a21 * a33); } int determinantX1(int coefMatrix[3][3], int constTermsMatrix[3]) { int a12 = coefMatrix[0][1]; int a13 = coefMatrix[0][2]; int a22 = coefMatrix[1][1]; int a23 = coefMatrix[1][2]; int a32 = coefMatrix[2][1]; int a33 = coefMatrix[2][2]; int c1 = constTermsMatrix[0]; int c2 = constTermsMatrix[1]; int c3 = constTermsMatrix[2]; return (c1 * a22 * a33) + (a12 * a23 * c3) + (a13 * c2 * a32) - (a13 * a22 * c3) - (c1 * a23 * a32) - (a12 * c2 * a33); } int determinantX2(int coefMatrix[3][3], int constTermsMatrix[3]) { int a11 = coefMatrix[0][0]; int a13 = coefMatrix[0][2]; int a21 = coefMatrix[1][0]; int a23 = coefMatrix[1][2]; int a31 = coefMatrix[2][0]; int a33 = coefMatrix[2][2]; int c1 = constTermsMatrix[0]; int c2 = constTermsMatrix[1]; int c3 = constTermsMatrix[2]; return (a11 * c2 * a33) + (c1 * a23 * a31) + (a13 * a21 * c3) - (a13 * c2 * a31) - (a11 * a23 * c3) - (c1 * a21 * a33); } int determinantX3(int coefMatrix[3][3], int constTermsMatrix[3]) { int a11 = coefMatrix[0][0]; int a12 = coefMatrix[0][1]; int a21 = coefMatrix[1][0]; int a22 = coefMatrix[1][1]; int a31 = coefMatrix[2][0]; int a32 = coefMatrix[2][1]; int c1 = constTermsMatrix[0]; int c2 = constTermsMatrix[1]; int c3 = constTermsMatrix[2]; return (a11 * a22 * c3) + (a12 * c2 * a31) + (c1 * a21 * a32) - (c1 * a22 * a31) - (a11 * c2 * a32) - (a12 * a21 * c3); } TOPSHIRIQ Chiziqli algebraik tenglamalar sistemasini Gauss va Kramer usullari yordamida yechish algoritmi va dasturini tuzing Вариант №1 1) 2) Вариант №2 1) 2) Вариант №3 1) 2) Вариант №4 1) 2) Вариант №5 1) 2) Вариант №6 1) 2) Download 413.84 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling