|
楼主 |
发表于 2004-5-15 07:41:45
|
显示全部楼层
< center" align=center><B>赛德尔迭代法解线性方程组</B><B> seidel.c<p></p></B></P>< 23pt; mso-line-height-rule: exactly">#include<math.h><p></p></P>< 23pt; mso-line-height-rule: exactly">#include<stdio.h><p></p></P><P 23pt; mso-line-height-rule: exactly"> <p></p></P><P 23pt; mso-line-height-rule: exactly">int seidel(double *a[],double b[],double x[],double eps,int n)<p></p></P><P 23pt; mso-line-height-rule: exactly">{<p></p></P><P 23pt; mso-line-height-rule: exactly">int i,j,k=1;<p></p></P><P 23pt; mso-line-height-rule: exactly">double s,t,dx,maxdx;<p></p></P><P 23pt; mso-line-height-rule: exactly">for(;;)<p></p></P><P 23pt; mso-line-height-rule: exactly"> {<p></p></P><P 23pt; mso-line-height-rule: exactly"> maxdx=0.0;<p></p></P><P 23pt; mso-line-height-rule: exactly"> for(i=0;i<n;i++)<p></p></P><P 23pt; mso-line-height-rule: exactly"> {<p></p></P><P 23pt; mso-line-height-rule: exactly"> t=x; s=b;<p></p></P><P 23pt; mso-line-height-rule: exactly"> for(j=0;j<n;j++) s+=(a[j]*x[j]);<p></p></P><P 23pt; mso-line-height-rule: exactly"> x=s;<p></p></P><P 23pt; mso-line-height-rule: exactly"> dx=fabs(s-t);<p></p></P><P 23pt; mso-line-height-rule: exactly"> if(dx>maxdx)maxdx=dx;<p></p></P><P 23pt; mso-line-height-rule: exactly"> }<p></p></P><P 23pt; mso-line-height-rule: exactly"> k++;<p></p></P><P 23pt; mso-line-height-rule: exactly"> printf("\nk=%d ",k);<p></p></P><P 23pt; mso-line-height-rule: exactly"> if(k>200)return(0);<p></p></P><P 23pt; mso-line-height-rule: exactly"> if(maxdx<eps)break;<p></p></P><P 23pt; mso-line-height-rule: exactly"> }<p></p></P><P 23pt; mso-line-height-rule: exactly">return(1);<p></p></P><P 23pt; mso-line-height-rule: exactly">}<p></p></P><P 23pt; mso-line-height-rule: exactly">main()<p></p></P><P 23pt; mso-line-height-rule: exactly">{<p></p></P><P 23pt; mso-line-height-rule: exactly">int i,n=3;<p></p></P><P 23pt; mso-line-height-rule: exactly">double a[3][3]={0,0.2,0.1,0.2,0,0.1,0.2,0.4,0};<p></p></P><P 23pt; mso-line-height-rule: exactly">double x[3]={1,1,1},b[3]={0.3,1.5,2.0},eps=1.0e-15;<p></p></P><P 23pt; mso-line-height-rule: exactly">double *pa[3];<p></p></P><P 23pt; mso-line-height-rule: exactly">for(i=0;i<n;i++)pa=a;<p></p></P><P 23pt; mso-line-height-rule: exactly">if(seidel(pa,b,x,eps,n))<p></p></P><P 23pt; mso-line-height-rule: exactly"> {<p></p></P><P 23pt; mso-line-height-rule: exactly"> for(i=0;i<n;i++)<p></p></P><P 23pt; mso-line-height-rule: exactly"> printf("\nx[%d] = %g",i,x);<p></p></P><P 23pt; mso-line-height-rule: exactly"> }<p></p></P><P 23pt; mso-line-height-rule: exactly">else printf("\nFiled !");<p></p></P><P 23pt; mso-line-height-rule: exactly">}<p></p></P><P 23pt; mso-line-height-rule: exactly"> <p></p></P> |
|