G-S Methom.cpp
资源名称:数值分析课程设计.zip [点击查看]
上传用户:zhdd911129
上传日期:2007-05-11
资源大小:722k
文件大小:1k
源码类别:
matlab例程
开发平台:
Matlab
- int ReachTOL(double x1[],double x2[],double tol,int n)
- {
- int i;
- double error=0;
- for (i=0;i<n;i++)
- {
- error+=(x1[i]-x2[i])*(x1[i]-x2[i]);
- }
- if(error<tol)
- return 1;
- else
- return 0;
- }
- void GSI(double a[][N],double b[],double x0[],double tol,int max,int n)
- {
- double x1[N];
- int k,i,j;
- for (i=0;i<n;i++) //Initialazation
- {
- x1[i]=0;
- }
- for (k=0;k<max;k++)
- {
- for (i=0;i<n;i++)
- {
- for (j=0;j<n;j++)
- {
- if(j!=i)
- {
- x1[i]+=a[i][j]*x0[j];
- }
- }
- x1[i]=((-1)*x1[i]+b[i])/a[i][i];
- if(i<n-1)
- x0[i]=x1[i];
- }
- if(ReachTOL(x0,x1,tol,n))
- {
- for (i=0;i<n;i++)
- {
- x0[i]=x1[i];
- }
- return;
- }
- x00[n-1]=x1[n-1];
- for (i=0;i<n;i++)
- {
- x1[i]=0;
- }
- }
- for (i=0;i<n;i++)
- {
- x0[i]=x1[i];
- }
- return;
- }