Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <fstream>
- #include <vector>
- void method_vr(const std::string &input_file_path, const std::string &output_file_path) {
- double w = 1.2;
- int N_max = 10000;
- int S = 0;
- double eps;
- double eps_max = 0;
- double eps_cur = 0;
- int n;
- std::ifstream input_file(input_file_path);
- input_file>> eps;
- input_file >> n;
- std::vector<std::vector<double>> a(n, std::vector<double>(n));
- std::vector<double> b(n);
- std::vector<double> x(n);
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < n; ++j) {
- input_file >> a[i][j];
- }
- }
- for (int j = 0; j < n; ++j) {
- input_file >> b[j];
- }
- for (int j = 0; j < n; ++j) {
- input_file >> x[j];
- }
- input_file.close();
- double x_old;
- double x_new;
- bool f = false;
- while (!f) {
- for (int i = 0; i < n; ++i) {
- x_old = x[i];
- x_new = (1 - w) * x[i]*a[i][i] + w * b[i];
- for (int j = 0; j < n; ++j) {
- if (j != i) {
- x_new -= w * a[i][j] * x[j];
- }
- }
- x_new /= a[i][i];
- eps_cur = fabs(x_old - x_new);
- if (i == 1) {
- eps_max = eps_cur;
- } else if (eps_cur > eps_max) {
- eps_max = eps_cur;
- }
- x[i] = x_new;
- }
- S += 1;
- if ((eps_max <= eps) || (S >= N_max)) {
- f = true;
- }
- }
- std::ofstream output_file(output_file_path);
- output_file << "eps_max: " << eps_max << '\n';
- for (int i = 0; i < n; ++i) {
- output_file << x[i] << ' ';
- }
- output_file << '\n';
- output_file << "count of steps: " << S << '\n';
- output_file.close();
- }
- int main() {
- std::string input_file_path = "input.txt";
- std::string output_file_path = "output.txt";
- method_vr(input_file_path, output_file_path);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement