Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // laba3.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <cstdio>
- #include "math.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- int lines, columns, epsilon, tau;
- double A[lines][columns];
- double f[lines];
- double x[lines];
- cout << "Write lines: \n";
- cin >> lines;
- cout << "Write columns: \n";
- cin >> columns;
- cout << "Write epsilon: \n";
- cin >> epsilon;
- cout << "Write tau recomended 0.01: \n";
- cin >> tau;
- cout << "Go matrix A: \n";
- for (int i = 0; i < lines; i++)
- for (int j = 0; j < columns; j++)
- cin >> A[i][j];
- cout << "Go vector f: \n";
- for (int i = 0; i < lines; i++)
- cin >> f[i];
- cout << "Go vector x: \n";
- for(int i = 0; i < lines; i++)
- cin >> x[i];
- double B[lines][columns] = { 0.0 }; // инициализация новой матрицы (которая будет после умножения на тау)
- double C[lines] = { 0.0 }; // инициализация нового вектора (после умножения на тау)
- double xNext[lines] = { 0.0 }; // вспомогательный вектор иксов (для хранения значений, вычисленных при итерации)
- // находим матрицу В
- for (int i = 0; i < lines; i++)
- for (int j = 0; j < columns; j++)
- {
- if (i == j)
- B[i][j] = 1 - tau * A[i][j];
- else B[i][j] = -tau * A[i][j];
- }
- // вычисление нормы матрицы
- double norm = 0;
- double temp;
- for (int i = 0; i < lines; i++)
- {
- temp = 0.0;
- for (int j = 0; j < columns; j++)
- temp += fabs(B[i][j]);
- if (temp > norm)
- norm = temp;
- }
- // находим вектор С
- for (int i = 0; i < lines; i++)
- C[i] = tau * f[i];
- // вычисление нормы вектора C
- double fnorm = 0;
- for (int i = 0; i < lines; i++)
- if (abs(C[i]) > fnorm)
- fnorm = fabs(C[i]);
- // вычисление числа шагов для заданной точности
- int n;
- n = (int)(1 + (log(epsilon) + log(1 - norm) - log(fnorm)) / log(norm));
- // вычисление корней с заданной точностью
- int k = 0;
- while (k < n)
- {
- for (int i = 0; i < lines; i++)
- {
- for (int j = 0; j < columns; j++)
- xNext[i] += B[i][j] * x[j];
- xNext[i] += C[i];
- }
- for (int i = 0; i < lines; i++)
- {
- x[i] = xNext[i];
- xNext[i] = 0.0;
- }
- k++;
- }
- // вывод решения
- for (int i = 0; i < lines; i++)
- cout << "x" << i + 1 << " = " << x[i] << '\n';
- cout << '\n';
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement