Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <math.h>
- #include <vector>
- using namespace std;
- const int N = 4;
- const int M = 5;
- void SimpleIteration() {
- double q = 0, result[N], x0[N];
- double matrix[N][M] = {
- { 9, 0, 1, -3, -5 }, //[3] - [0]
- { 3, 19, 11, 8, 149 }, //[0]
- { -1, -12, 20, 8, -1 }, //[3] * -1 + [2]
- { 5, 0, 11, 18, 107 } //[1] + [2] - [3] - [0]
- };
- for (int i = 0; i < N; i++) {
- double a = matrix[i][i];
- matrix[i][M - 1] /= a;
- for (int j = 0; j < N; j++) {
- if (j != i) {
- matrix[i][j] /= a * -1;
- } else {
- matrix[i][j] = 0;
- }
- }
- }
- for (int i = 0; i < N; i++) {
- double a = 0;
- for (int j = 0; j < N; j++) {
- a += abs(matrix[i][j]);
- }
- if (q < a) {
- q = a;
- }
- }
- for (int i = 0; i < N; i++) {
- x0[i] = matrix[i][M - 1];
- }
- printf("-------------------------------------\n");
- printf("|%7s |%7s |%7s |%7s |\n", "x1", "x2", "x3", "x4");
- printf("-------------------------------------\n");
- float t = abs((1 - q) * 1e-2 / q);
- while (true) {
- printf("|");
- for (int i = 0; i < N; i++) {
- result[i] = matrix[i][M - 1];
- for (int j = 0; j < N; j++) {
- result[i] += matrix[i][j] * x0[j];
- }
- printf("%7.3f |", result[i]);
- }
- printf("\n-------------------------------------\n");
- double a = abs(result[0] - x0[0]);
- for (int i = 1; i < N; i++) {
- if (abs(result[i] - x0[i]) > a) {
- a = abs(result[i] - x0[i]);
- }
- }
- if (a <= t) break;
- for (int i = 0; i < N; i++) {
- x0[i] = result[i];
- }
- }
- }
- void main() {
- printf("\nSimple iteration method\n");
- SimpleIteration();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement