资源描述:
《【精品】数值分析与算法》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、数值分析与算法实验报告班级:软件一班学号:20112711姓名:钱巧娅作业2.1Rgauss函数流程图Main函数流程图STARTfor作业2.1源代码#include#include#includeintrgauss(intn,doublea[][3],doubleb[]){intl,k,i,j;doubled,t;1=1;for(k=0;k<=n-2;k++){d=0.0;d=a[k][k];for(j=k+1;j<=n-l;j++){a[k]
2、
3、j]=a[k][j]/d;}b[k]=b[k]/d;for(i=k+1;i<=n-l;i++){for(j=k+l;j<=n・l;j++){a[i][j]=a[i][j]-a[i][k]*a[k]rj];)b[i]=b[i]-a[i][k]*b[k];)}d=a[(n-l)][n-l];b[n-l]=b[n-l]/d;for(i=n-2;i>=0;i—){t=0.0;for(j=i+1;j<=n・1;j++)t=t+a[i][j]*brj];b[i]=b[i]-t;}return(l);}main(){
4、inti,j;intrgauss(intn,doubleUl3],doubleb[J);staticdoubleal[][3]={2,-l,3,4,2,5,1,2,0};staticdoublebl[]={1,4,7};staticdoublea2[][3]={3,-1,2,1,1,1,2,1,-1};staticdoubleb2[]={-3,-4,-3);if(rgauss(3,al,bl)!=0)printfCXDXn");for(i=0;i<=2;i++)printf(Hx(%d)=%.4f"
5、,(i+l),b1Li]);printf("(2)M);if(rgauss(3,a2,b2)!=0)for(i=0;i<=2;i++)printf(Hx(%d)=%.4f",(i+l),b2[i]);}MC:Docu»ents结果截图:andSettingsAdBinistratorlyDocu>entsC-Free^H^<1>x=-1.0000x<3>=-6.0000<2>x<1>=-1.0000x<2>=-2.0000x<3)=-1.0000请按任意键继
6、续・•・rgauss函数流程图Main函数流程图2.2源代码:#include#include#includeintrgauss(intn,doublea[][4],doubleb[]){int*js,l,k,is,i,j;doubled,t;js=(int*)malloc(n*sizeof(int));1=1;for(k=0;k<=n-2;k++){d=0.0;for(i=k;i<=n-l;i++)for(j=k:j<=n-l;j++){t=fab
7、s(a[i]fjl);if(t>d){d=t;js[k]=j;is=i;}}if(d+l.O==l.O)1=0;else{if(js[k]!=k)for(i=0;i<=n-l;i++){t=a[i]LkJ;a[iJ[k]=ali][js[k]];a[i]
8、jslkj]=t;}if(is!=k){for(j=k;j<=n-l;j++){t=a[k][jJ;a[k]
9、j]=alis]
10、j];a[is][jJ=t;}t=b[k];b[k]=b[is];b[is]=t;}}if(l==0){free(js);p
11、rintfC'failXn0);return(0);}d=a[k][k];for(j=k+1;j<=n-l;j++){a[k][j]=a[k][j]/d;}b[k]=b[k]/d;for(i=k+1;i<=n-l;i++){for(j=k+l;j<=n-l;j++){a[i]U]=a[i][j]-a[i][k]*a[k]U];}b[i]=b[i]-a[i][k]*b[k];)}d=a[(n-l)][n-l];if(fabs(d)+1.0==1.0){frcc(js);printf(Hfail");r
12、eturn(0);}b[n-lj=b[n-l]/d;for(i=n-2;i>=0;i—){t=0.0;for(j=i4-1;j<=n-l;j++)t=t+a[i][j]*b
13、j];b[i]=b[i]-t;}js[n-l]=n-l;for(k=n-l;k>=0;k—)if(js[k]!=k){t=b[k];blk]=b[js[k]];bljs[k]]=t;}free(js);return(l);main(){inti;intrgauss(in