数值分析作业(C语言编程实现)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdio.h#includemath.hdoublef(doublex){doubleans;ans=exp(x);returnans;}voidmain(){doublea=1,b=3,error=0.0001,t[20][20],h,c;inti,j,k,m,n;h=b-a;t[0][0]=h*(f(a)+f(b))/2;k=1;while(1){t[0][k]=0;m=1;for(j=0;jk-1;j++)m=m*2;for(i=1;i=m;i++)t[0][k]=t[0][k]+h*f(a+(i-0.5)*h);t[0][k]=(t[0][k]+t[0][k-1])/2;for(j=1;j=k;j++){c=1;for(n=0;nj;n++)c=c*4;t[j][k-j]=(c*t[j-1][k-j+1]-t[j-1][k-j])/(c-1);}if(fabs(t[k][0]-t[k-1][0])error){printf(\n积分结果I≈%lf\n,t[k][0]);break;}else{h=h/2;k++;}}}#includestdio.h#includemath.hdoublef(doublet){doubleans;ans=pow(cos(t),1.0/3);returnans;}voidmain(){doublex=0,eslong=0.000001,x0;intN=20,i;printf(\n近似初值x0=%lf\n,x);for(i=0;iN;i++){x0=x;x=f(x);printf(x%d=%lf\n,i+1,x);if(fabs(x-x0)eslong)break;}if(fabs(x-x0)eslong)printf(得到近似结果为x≈%lf\n\n,x,i);elseprintf(迭代失败\n);}#includestdio.h#includemath.hdoublea=0,b=1,x,y=0,h=0.1,k1,k2,k3,k4;inti,N;doublef(doublet,doubles){doubleans;ans=1+t*t;returnans;}voidmain(){N=(b-a)/h;x=a;printf(\n初值为(x0,y0)=(%.8f,%.8f)\n,x,y);for(i=0;iN;i++){k1=f(x,y);k2=f(x+h/2,y+h*k1/2);k3=f(x+h/2,y+h*k2/2);k4=f(x+h,y+h*k3);y=y+h*(k1+2*(k2+k3)+k4)/6;x=x+h;printf(第%d次输出结果为(x%d,y%d)=(%.8f,%.8f)\n,i+1,i+1,i+1,x,y);}}#includestdio.hvoidmain(){doubledatax[4]={1.2,2.9,4.6,5.8},datay[10]={14.84,33.71,58.36,79.24},l[3],x=1.5,y;inti,j;y=0;for(i=0;i=3;i++){l[i]=1;for(j=0;ji;j++)l[i]=(x-datax[j])/(datax[i]-datax[j])*l[i];for(j=i+1;j=3;j++)l[i]=(x-datax[j])/(datax[i]-datax[j])*l[i];y=y+datay[i]*l[i];}printf(\nf(x)在x=%f处的近似值为:y=%f\n,x,y);}#includestdio.hvoidmain(){doubledatay[9]={11.7,14.87,21.44,31.39,44.73,61.46,81.57,105.11,131.91};intm=2,i,j,k;doublep,data[9][4],a[3][4],datax[9]={1.2,2.3,3.4,4.5,5.6,6.7,7.8,8.9,10.0};for(i=0;i9;i++)for(j=1;j2*m+1;j++){data[i][j]=1;for(k=0;kj;k++)data[i][j]=data[i][j]*datax[i];}for(i=0;im+1;i++){for(j=0;jm+1;j++){a[i][j]=0;for(k=0;k9;k++)a[i][j]=a[i][j]+data[k][i+j];}}a[0][0]=9;a[0][m+1]=0;for(i=0;i9;i++)a[0][m+1]=a[0][m+1]+datay[i];for(i=1;im+1;i++){a[i][m+1]=0;for(j=0;j9;j++){p=datay[j];for(k=0;ki;k++)p=p*datax[j];a[i][m+1]=a[i][m+1]+p;}}//生成m+1行,m+2列增广矩阵//for(i=0;im+1;i++)//显示方程组//for(j=0;jm+2;j++){if(j!=m+1){printf((%f)a%d,a[i][j],j);if(j!=m)printf(+);}elseprintf(=%f\n,a[i][j]);}for(i=0;im;i++)//高斯消去法//{if(a[i][i]!=0){for(j=i+1;jm+1;j++){a[j][i]=a[j][i]/a[i][i];for(k=i+1;km+2;k++)a[j][k]=a[j][k]-a[i][k]*a[j][i];}}elsebreak;}if(a[m][m]!=0&&i==m){a[m][m+1]=a[m][m+1]/a[m][m];for(i=2;i=m+1;i++){for(j=1;ji;j++)a[m+1-i][m+1]=a[m+1-i][m+1]-a[m+1-i][m+1-j]*a[m+1-j][m+1];a[m+1-i][m+1]=a[m+1-i][m+1]/a[m+1-i][m+1-i];}printf(方程组的解为:\n);for(j=0;jm+1;j++)printf(a%d=%f\n,j,a[j][m+1]);printf(拟合多项式为:\n);printf(P%d(x)=(%f)+(%f)x+(%f)x^2\n,m,a[0][m+1],a[1][m+1],a[2][m+1]);}elseprintf(数据有误!\n);}}列主元素法#includestdio.h#includemath.hvoidmain(){doublea[3][4]={1,-2,-1,3,-2,10,-3,15,-1,-2,5,10},mov,comp;inti,j,k,nrow;for(i=0;i2;i++){comp=fabs(a[i][i]);for(k=i;k3;k++)//比较绝对值大小并进行主元列交换//if(fabs(a[k][i])=comp){nrow=k;comp=fabs(a[k][i]);}for(j=0;j=3;j++){mov=a[i][j];a[i][j]=a[nrow][j];a[nrow][j]=mov;}printf(方程第%d行互换位置后如下\n,i+1);for(j=0;j3;j++)printf((%f)x1+(%f)x2+(%f)x3=%f\n,a[j][0],a[j][1],a[j][2],a[j][3]);if(a[i][i]!=0){for(j=i+1;j3;j++){a[j][i]=a[j][i]/a[i][i];for(k=i+1;k=3;k++)a[j][k]=a[j][k]-a[i][k]*a[j][i];a[j][i]=0;}printf(方程经%d次消元如下\n,i+1);for(j=0;j3;j++)printf((%f)x1+(%f)x2+(%f)x3=%f\n,a[j][0],a[j][1],a[j][2],a[j][3]);}elsebreak;}if(a[2][2]!=0&&i==2){printf(方程化简得\n);for(i=0;i3;i++)printf((%f)x1+(%f)x2+(%f)x3=%f\n,a[i][0],a[i][1],a[i][2],a[i][3]);a[2][3]=a[2][3]/a[2][2];for(i=2;i=3;i++){for(j=1;ji;j++)a[3-i][3]=a[3-i][3]-a[3-i][3-j]*a[3-j][3];a[3-i][3]=a[3-i][3]/a[3-i][3-i];}printf(方程组的解为:\n);for(j=0;j3;j++)printf(x%d=%f\n,j+1,a[j][3]);}elseprintf(数据有误!\n);}Jacobi迭代法#includestdio.h#includemath.hvoidmain(){doublea[3][7]={{1,-2,-1,3},{-2,10,-3,15},{-1,-2,5,10}},error=0.000001,norm;intN=423,i,j,k;a[0][4]=0,a[1][4]=0,a[2][4]=0;for(i=0;i3;i++)//把a矩阵转化为b矩阵//{a[i][6]=a[i][i];for(j=0;j3;j++){a[i][j]=-a[i][j]/a[i][6];}a[i][3]=a[i][3]/a[i][6];a[i][i]=0;}printf(化为b矩阵如下\n);for(i=0;i3;i++){printf(%f%f%f%f\n,a[i][0],a[i][1],a[i][2],a[i][3]);}for(i=1;iN;i++){for(j=0;j3;j++){a[j][5]=0;for(k=0;k3;k++){a[j][5]=a[k][4]*a[j][k]+a[j][5];}a[j][5]=a[j][5]+a[j][3];}norm=0;for(k=0;k3;k++)norm=norm+fabs(a[k][4]-a[k][5]);if(normerror)break;elsefor(k=0;k3;k++)a[k][4]=a[k][5];}if(normerror){printf(计算结果为\n);for(i=0;i3;i++){printf(x%d=%.3f\n,i+1,a[i][5]);}}elseprintf(迭代失败\n);}现场考试题目1#includestdio.h#includemath.hdoublef(doublex){doubleans;ans=exp(x);return(ans);}voidmain(){doublea=-1,b=1,error=0.0001,m=1,h,T0,T,F;intk;h=(b-a)/2;T0=h*(f(a)+f(b));while(1){F=0;for(k=1;k=pow(2.0,m-1);k++)F=F+f(a+(2*k-1)*h);T=T0/2+h*F;if(fabs(T-T0)error)break;m++;h=h/2;T0=T;}printf(积分结果为I≈%f\n,T);}题目2#includestdio.hdoublef(doublet,doubles){doubleans;ans=1+t*t;return(ans);}voidmain(){doublea=0,b=1,h=0.2,x0=0,y0=0,x,k1,k2,k3,y;intN,n;N=(b-a)/h;for(n=1;n=N;n++){x=x0+h;k1=f(x0,y0);k2=f(x0+h/2,y0+h/2*k1);k3=f(x0+h,y0-h*k1+2*h*k2);y=y0+h/6*(k1+4*k2+k3);

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功