Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <cmath>
- #include <iostream>
- using namespace std;
- double eps = 0.001; ///< желаемая точность
- int _tmain(int argc, _TCHAR* argv[])
- {
- int n;
- cout<< "Enter the number of equations:"<<endl;
- cin>>n;
- const int N = n;
- double** A = new double* [n];
- for (int count = 0; count < n; count++)
- {
- A[count] = new double [n];
- }
- //double A[N][N];
- //double F[N];
- double *F = new double [n];
- cout << "Enter the matrix of coefficients:" << endl;
- for(int i=0; i<N; i++){
- for(int j=0; j<N; j++){
- cin>>A[i][j];
- }
- }
- cout<< "Enter the vector b:"<<endl;
- for(int i=0; i<N; i++){
- cin>>F[i];
- }
- //double X[N];
- double *X = new double [n];
- for(int i=0;i<N;i++){
- X[i]=1.0;
- }
- cout<<" You want to set the accuracy? (default 0.001)"<< endl;
- cout<<"1 - Yes; \n 2- No;"<<endl;
- int choice;
- cin>>choice;
- switch (choice)
- {
- case 1: cin>>eps; break;
- case 2: break;
- }
- double* TempX = new double[N];
- double norm; // норма, определяемая как наибольшая разность компонент столбца иксов соседних итераций.
- do {
- for (int i = 0; i < N; i++) {
- TempX[i] = F[i];
- for (int g = 0; g < N; g++) {
- if (i != g)
- TempX[i] -= A[i][g] * X[g];
- }
- TempX[i] /= A[i][i];
- }
- norm = fabs(X[0] - TempX[0]);
- for (int h = 0; h < N; h++) {
- if (fabs(X[h] - TempX[h]) > norm)
- norm = fabs(X[h] - TempX[h]);
- X[h] = TempX[h];
- }
- } while (norm > eps);
- cout<<"X(";
- for(int i=0; i<N; i++){
- cout<<X[i]<<";";
- }
- cout<< ")";
- delete[] TempX;
- return 0;
- }
Add Comment
Please, Sign In to add comment