Cloud
03-12-2006, 09:00 AM
I must add to may program GS algorithm for soloving linear equation (i already hav gauss elimination and LU algorithm).
I make function (but it not works, please corect me :( :( :
where:
n -dimensin of main matrix
tab - main matrix
itmax - max number of iteration
prec- precisin
start - initial for xold and x vector
void GS_solve(type **tab,type *b, type *x, const int &n,double prec, int itmax, type start){
double suma,m;
double error=1.;
type *xold=new type[n];
for(int i=0;i<n;++i){
xold[i]=start;
x[i]=start;
}
for(int it=1;it<itmax;++it){
if(error>prec){
for(int i=0;i<n;++i){
suma=0;
for(int j=0;j<i;++j){
suma+=tab[i][j]*x[j];
}
for(int j=i+1;j<n;++j){
suma+=tab[i][j]*xold[j];
}
x[i]=(b[i]-suma)/tab[i][i];
}
}else break;
error=0.;
for(int k=0;k<n;++k){
m=fabs(x[k]-xold[k]);
if(error<m) error=m;
xold[k]=x[k];
}
}
delete [] xo;
}
PS.Sorry for my bad englisch
I make function (but it not works, please corect me :( :( :
where:
n -dimensin of main matrix
tab - main matrix
itmax - max number of iteration
prec- precisin
start - initial for xold and x vector
void GS_solve(type **tab,type *b, type *x, const int &n,double prec, int itmax, type start){
double suma,m;
double error=1.;
type *xold=new type[n];
for(int i=0;i<n;++i){
xold[i]=start;
x[i]=start;
}
for(int it=1;it<itmax;++it){
if(error>prec){
for(int i=0;i<n;++i){
suma=0;
for(int j=0;j<i;++j){
suma+=tab[i][j]*x[j];
}
for(int j=i+1;j<n;++j){
suma+=tab[i][j]*xold[j];
}
x[i]=(b[i]-suma)/tab[i][i];
}
}else break;
error=0.;
for(int k=0;k<n;++k){
m=fabs(x[k]-xold[k]);
if(error<m) error=m;
xold[k]=x[k];
}
}
delete [] xo;
}
PS.Sorry for my bad englisch