#include <iostream> #include <math.h> #include <string> #include <fstream> using namespace std; double n = 5.0; double epsilon = 0.0001; double mli = 30.0; double data[] = { 10, 1, 1, 1, 2, 15, 3, 20, 4, 2, 1, 30, 5, 1, 40, 9, 5, 60, 3, 0, 1, 10, 1, 15, 6, 2, 1, 1, 20, 30 }; //double data[] = { 10, -1, 1, 1, 2, 13, -3, 20, 4, 2, 1, 24, 5, -1, 40, 9, 5, 58, 3, 0, -1, 10, 1, 13, 6, -2, 1, 1, 20, 26 }; double A[5][5]; double Alfa[5][5]; double wektor[5]; double Beta[5]; double x_nowy[5], x_stary[5]; int x=0; void funkcja_b() { std::cout << "Macierz alfa"<<endl; //tworzymy alfa for (int i = 0; i < n; i++) { for (int j = 0; j < n;j++) { if (i == j){ Alfa[i][j] = 0; } else{ if (A[i][i] != 0){ Alfa[i][j] = -(A[i][j] / A[i][i]); } } } } //tworzymy beta for (int i = 0; i < n; i++){ Beta[i] = wektor[i] / A[i][i]; } } void funkcja_c() { double norma2 = 0.0; int licznik = 0; for (int i = 0; i < n; i++){ x_nowy[i] = Beta[i]; } do{ for (int i = 0; i < n; i++) { x_stary[i] = x_nowy[i]; } licznik++; for (int i = 0; i <= n; i++){ for (int j = 0; j <= i - 1;j++){ x_nowy[i] += Alfa[i][j] * x_nowy[j]; } for (int j = i + 1; j < n; j++){ x_nowy[i] += Alfa[i][j] * x_stary[j]; //cout << x_nowy<<endl; } x_nowy[i] += Beta[i]; } norma2 = 0; for (int i = 0; i < n; i++){ norma2 += abs(x_nowy[i]-x_stary[i]); //cout << norma2<<endl; } norma2 = norma2/n; } while (norma2 > epsilon && licznik < mli); cout << endl <<"Licznik iteracji " << licznik << endl << " norma2 " << norma2 << endl; } int main() { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i][j] = data[x]; x++; } wektor[i] = data[x]; x++; } funkcja_b(); cout << "Macierz alfa" << endl; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ cout << Alfa[i][j] << "\t"; } cout << endl; } cout << endl<< "Wektor beta" << endl; for (int i = 0; i < n; i++){ cout << Beta[i] << endl; } funkcja_c(); return 0; }