Anden198

Jacobi Method (Visual Studio Edition)

Feb 27th, 2018
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. double eps = 0.001; ///< желаемая точность
  8.  
  9. int _tmain(int argc, _TCHAR* argv[])
  10. {
  11.     int n;
  12.     cout<< "Enter the number of equations:"<<endl;
  13.     cin>>n;
  14.  
  15.     const int N = n;
  16.  
  17.     double** A = new double* [n];
  18.     for (int count = 0; count < n; count++)
  19.     {
  20.         A[count] = new double [n];
  21.     }
  22.     //double A[N][N];
  23.     //double F[N];
  24.     double *F = new double [n];
  25.     cout << "Enter the matrix of coefficients:" << endl;
  26.     for(int i=0; i<N; i++){
  27.         for(int j=0; j<N; j++){
  28.             cin>>A[i][j];
  29.         }
  30.     }
  31.     cout<< "Enter the vector b:"<<endl;
  32.     for(int i=0; i<N; i++){
  33.         cin>>F[i];
  34.     }
  35.     //double X[N];
  36.     double *X = new double [n];
  37.     for(int i=0;i<N;i++){
  38.         X[i]=1.0;
  39.     }
  40.     cout<<" You want to set the accuracy? (default 0.001)"<< endl;
  41.     cout<<"1 - Yes; \n 2- No;"<<endl;
  42.     int choice;
  43.     cin>>choice;
  44.     switch (choice)
  45.     {
  46.         case 1: cin>>eps; break;
  47.         case 2: break;
  48.     }
  49.     double* TempX = new double[N];
  50.     double norm; // норма, определяемая как наибольшая разность компонент столбца иксов соседних итераций.
  51.  
  52.     do {
  53.         for (int i = 0; i < N; i++) {
  54.             TempX[i] = F[i];
  55.             for (int g = 0; g < N; g++) {
  56.                 if (i != g)
  57.                     TempX[i] -= A[i][g] * X[g];
  58.             }
  59.             TempX[i] /= A[i][i];
  60.         }
  61.         norm = fabs(X[0] - TempX[0]);
  62.         for (int h = 0; h < N; h++) {
  63.             if (fabs(X[h] - TempX[h]) > norm)
  64.                 norm = fabs(X[h] - TempX[h]);
  65.             X[h] = TempX[h];
  66.         }
  67.     } while (norm > eps);
  68.     cout<<"X(";
  69.     for(int i=0; i<N; i++){
  70.         cout<<X[i]<<";";
  71.     }
  72.     cout<< ")";
  73.     delete[] TempX;
  74.     return 0;
  75. }
Add Comment
Please, Sign In to add comment