Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- void print(float** MTRX_ORIG, int N);
- void print_iterat(float* ARR, int N);
- void print_mtrx(float** MTRX, int N);
- float** create(int N);
- float** create_rand(int N);
- bool conver(float** MTRX, int N);
- float* simple_iterat_method(float** MTRX, float* ARR, float E, int N);
- float* m_Seidel(float** MTRX, float* ARR, float E, int N);
- int main()
- {
- int n, c;
- float** mtrx_orig;
- cout << "Enter the number of equations n = ";
- cin >> n;
- cout << endl << "Would you like to create the matrix? Random(0)/By hand(1)" << endl;
- cin >> c;
- if (c == 0)
- mtrx_orig = create_rand(n);
- else
- mtrx_orig = create(n);
- cout << endl << "Your matrix:" << endl;
- print(mtrx_orig, n);
- float* arr = new float[n];
- float** mtrx = new float* [n];
- for (int i = 0; i < n; i++)
- {
- mtrx[i] = new float[n];
- }
- for (int i = 0; i < n; i++)
- for (int j = 0; j <= n; j++)
- {
- if (j != n)
- mtrx[i][j] = mtrx_orig[i][j];
- else arr[i] = mtrx_orig[i][n];
- }
- cout << endl << "Your array: " << endl;
- for (int i = 0; i < n; i++)
- cout << setw(10) << round(arr[i] * 1000) / 1000;
- cout << endl << "Your matrix: " << endl;
- print_mtrx(mtrx, n);
- /*Проверка на сходимость*/
- bool con = conver(mtrx, n);
- if (!con)
- {
- cout << "A function has not been tested for convergence!" << endl;
- return 0;
- }
- cout << "The function passed the convergence test!" << endl;
- float e;
- cout << "Entrer the Epsilant E = ";
- cin >> e;
- cout << endl << "Choose a solution method: The method of simple iterations(0)/ Seidel's method(1)" << endl;
- cin >> c;
- float* answer = new float[n];
- if (c == 0)
- {
- answer = simple_iterat_method(mtrx, arr, e, n);
- }
- else answer = m_Seidel(mtrx, arr, e, n);
- if (!answer) return 0;
- cout << "Answer:" << endl;
- print_iterat(answer, n);
- return 0;
- }
- void print(float** MTRX_ORIG, int N)
- {
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j <= N; j++)
- {
- if (j < N)
- cout << setw(10) << round(MTRX_ORIG[i][j] * 1000) / 1000;
- else
- cout << setw(10) << "|" << round(MTRX_ORIG[i][j] * 1000) / 1000;
- }
- cout << endl;
- }
- cout << endl;
- }
- void print_iterat(float* ARR, int N)
- {
- for (int i = 0; i < N; i++)
- {
- cout << "x" << i + 1 << " = " << setw(5) << round(ARR[i] * 1000) / 1000;
- cout << endl;
- }
- }
- void print_mtrx(float** MTRX, int N)
- {
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N; j++)
- cout << setw(10) << round(MTRX[i][j] * 1000) / 1000;
- cout << endl;
- }
- cout << endl;
- }
- float** create(int N)
- {
- float** MTRX_ORIG = new float* [N];
- for (int i = 0; i < N; i++)
- {
- MTRX_ORIG[i] = new float[N + 1];
- }
- cout << endl << "Enter the element matrix:" << endl;
- for (int i = 0; i < N; i++)
- for (int j = 0; j <= N; j++)
- {
- cin >> MTRX_ORIG[i][j];
- }
- return MTRX_ORIG;
- }
- float** create_rand(int N)
- {
- float** MTRX_ORIG = new float* [N];
- for (int i = 0; i < N; i++)
- {
- MTRX_ORIG[i] = new float[N + 1];
- }
- for (int i = 0; i < N; i++)
- for (int j = 0; j <= N; j++)
- {
- MTRX_ORIG[i][j] = rand() % 100;
- }
- return MTRX_ORIG;
- }
- bool conver(float** MTRX, int N)
- {
- float sum = 0;
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N; j++)
- {
- sum += abs(MTRX[i][j]);
- }
- if (abs(MTRX[i][i]) <= sum - abs(MTRX[i][i]))
- return false;
- sum = 0;
- }
- return true;
- }
- float* simple_iterat_method(float** MTRX, float* ARR,float E, int N)
- {
- float* ANSWER = new float[N];
- for (int i = 0; i < N; i++)
- ANSWER[i] = 0;
- float* X = new float[N];
- bool c = true;
- int k = 0;
- while (c)
- {
- for (int i = 0; i < N; i++)
- {
- X[i] = ARR[i] / MTRX[i][i];
- for (int j = 0; j < N; j++)
- {
- if (i != j)
- X[i] -= MTRX[i][j] * ANSWER[i] / MTRX[i][i];
- }
- }
- for (int i = 0; i < N; i++)
- {
- if (E > abs(X[i] - ANSWER[i]))
- {
- c = false;
- break;
- }
- }
- for (int i = 0; i < N; i++)
- ANSWER[i] = X[i];
- k++;
- cout << endl << "Iteration " << k << endl;
- print_iterat(ANSWER, N);
- cout << endl;
- }
- return ANSWER;
- }
- float* m_Seidel(float** MTRX, float* ARR, float E, int N)
- {
- float* ANSWER = new float[N];
- for (int i = 0; i < N; i++)
- ANSWER[i] = 0;
- float* X = new float[N];
- bool c = true;
- int k = 0;
- while (c)
- {
- for (int i = 0; i < N; i++)
- {
- X[i] = ARR[i] / MTRX[i][i];
- for (int j = 0; j < N; j++)
- {
- if (i != j)
- {
- if (j < i)
- X[i] -= MTRX[i][j] * X[i - 1] / MTRX[i][i];
- else X[i] -= MTRX[i][j] * ANSWER[i] / MTRX[i][i];
- }
- }
- }
- for (int i = 0; i < N; i++)
- {
- if (E > abs(X[i] - ANSWER[i]))
- {
- c = false;
- break;
- }
- }
- for (int i = 0; i < N; i++)
- ANSWER[i] = X[i];
- k++;
- cout << endl << "Iteration " << k << endl;
- print_iterat(ANSWER, N);
- cout << endl;
- }
- return ANSWER;
- }
Add Comment
Please, Sign In to add comment