《c编的高斯赛德尔迭代法解线性方程组的程序.doc》由会员分享,可在线阅读,更多相关《c编的高斯赛德尔迭代法解线性方程组的程序.doc(4页珍藏版)》请在三一办公上搜索。
1、c编的高斯赛德尔迭代法解线性方程组的程序c编的高斯赛德尔迭代法解线性方程组的程序2010-12-15 20:19#include math.h#include stdio.h double norm(double*x,double*y,int n)int i=0;double s=0;for(i=0;i n;i+)s=s+fabs(xi-yi)*fabs(xi-yi);return sqrt(s);void Jacobi(double A,double b,double x0,double x,int n1)int n=100;/最大迭代次数double ee=0.0001,s=0;/精度in
2、t i,j,k;for(j=0;j n1;j+)xj=x0j;for(i=1;i=n;i+)for(j=0;j n1;j+)s=0;for(k=0;k n1;k+)if(k j)s=s+Aj*n1+k*xk;else if(k j)s=s+Aj*n1+k*x0k;xj=(bj-s)/Aj*n1+j;if(norm(x,x0,n1)ee)break;for(j=0;j n1;j+)x0j=xj;int main()double a33=8,-1,1,2,10,-1,1,1,-5;double b3=1,4,3;double x03=0,0,0.0;double x3=0,0,0;int n1=3
3、,i;Jacobi(a,b,x0,x,n1);for(i=0;i n1;i+)printf(x(%d)=%6.4fn,i+1,xi);system(pause);return(0);#include math.h#include stdio.h double norm(double*x,double*y,int n)int i=0;double s=0;for(i=0;i n;i+)s=s+fabs(xi-yi)*fabs(xi-yi);return sqrt(s);void Jacobi(double A,double b,double x0,double x,int n1)int n=10
4、0;/最大迭代次数double ee=0.0001,s=0;/精度int i,j,k;for(i=1;i=n;i+)for(j=0;j n1;j+)xj=x0j;for(j=0;j n1;j+)s=0;for(k=0;k n1;k+)if(k!=j)s=s+Aj*n1+k*x0k;x0j=(bj-s)/Aj*n1+j;if(norm(x,x0,n1)ee)break;int main()double a33=8,-1,1,2,10,-1,1,1,-5;double b3=1,4,3;double x03=0,0,0.0;double x3=0,0,0;int n1=3,i;Jacobi(a,b,x0,x,n1);for(i=0;i n1;i+)printf(x(%d)=%6.4fn,i+1,xi);system(pause);return(0);