From Wikipedia:
The convergence properties of the Gauss–Seidel method
are dependent on the matrix A. Namely, the procedure
is known to converge if either:1. A is symmetric positive-definite, or 2. A is strictly or irreducibly diagonally dominant.
Is there algorithm for transforming matrix to meet the convergence criteria or must I do it manually?
I am implementing Gauss-Seidel method in C++:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int n;
double eps = 0.001;
//yygnanma sherti
bool converge(double *xk, double *xkp)
{
double norm = 0;
for (int i = 0; i < n; i++)
{
norm += (xk[i] - xkp[i])*(xk[i] - xkp[i]);
}
if(sqrt(norm) >= eps)
return false;
return true;
}
int main()
{
//olcheg
printf("Olchegi giriz: ");
scanf("%d", &n);
//koeffisientler uchin
double a[n][n];
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
double tmp;
printf("a[%d][%d] = ", i+1, j+1);
scanf("%lf", &tmp);
a[i][j] = tmp;
}
}
//azat agzalar uchin
double b[n];
for(int i=0; i<n; i++)
{
double tmp;
printf("b[%d] = ", i+1);
scanf("%lf", &tmp);
b[i] = tmp;
}
double x[n];
double p[n];
for(int i=0; i<n; i++)
{
x[i] = 0;
p[i] = 0;
}
do
{
for (int i = 0; i < n; i++)
p[i] = x[i];
for (int i = 0; i < n; i++)
{
double var = 0;
for (int j = 0; j < i; j++)
var += (a[i][j] * x[j]);
for (int j = i; j < n; j++)
var += (a[i][j] * p[j]);
x[i] = (b[i] - var) / a[i][i];
}
}while (!converge(x, p));
return 0;
}
Now I want to implement matrix transformation algorithm (if there is one) to meet convergence criteria.
Best Answer
I have found solution for my question and implemented it in C++: