1计算方法与实习实验报告学院:学号:姓名:完成日期:2实习题一4、设2211NnjSj,已知其精确值为。1)编制按从大到小的顺序计算Sn的程序;2)编制按从小到大的顺序计算Sn的程序;3)按两种顺序分别计算S1000,S10000,S30000,并指出有效位数。实验代码C语言程序如下:#includeiostream#includecmathusingnamespacestd;intmain(){floatSn=0;intN;cinN;for(floatj=2;j=N;j++){Sn=1/(j*j-1)+Sn;}cout从小到大计算的结果为Snendl;Sn=0;for(j=N;j=2;j--){Sn=1/(j*j-1)+Sn;}cout从大到小计算的结果为Snendl;return0;}运行窗口3实习题二1、用牛顿法求下列方程的根:1)20xxe实验代码C语言程序代码如下:#includeiostream#includecmath#defineN100#defineeps1e-6#defineeta1e-8usingnamespacestd;floatNewton(floatf(float),floatfl(float),floatx0){floatx1,d;intk=0;4do{x1=x0-f(x0)/fl(x0);if(k++N||fabs(fl(x1))eps){cout发散endl;break;}d=fabs(x1)1?x1-x0:(x1-x0)/x1;x0=x1;coutx=x0endl;}while(fabs(d)eps&&fabs(f(x1))eta);returnx1;}floatf(floatx){returnx+log10(x)-2;}floatfl(floatx){return1+1/x;}voidmain(){floatx0,y0;cinx0;y0=Newton(f,fl,x0);cout方程的根为y0endl;}运行窗口5实习题三1、用列主元消去法解方程组:1)12434xxx123421xxxx1234333xxxx1234234xxxx实验代码C语言程序代码如下:#includeiostream#includecmathusingnamespacestd;voidColPivot(float*c,intn,floatx[]){inti,j,t,k;floatp;for(i=0;i=n-2;i++){k=i;for(j=i+1;j=n-1;j++)if(fabs(*(c+j*(n+1)+i))(fabs(*(c+k*(n+1)+i))))k=j;if(k!=j)for(j=i;j=n;j++){p=*(c+i*(n+1)+j);*(c+i*(n+1)+j)=*(c+k*(n+1)+j);*(c+k*(n+1)+j)=p;}for(j=i+1;j=n-1;j++){p=(*(c+j*(n+1)+i))/(*(c+i*(n+1)+i));for(t=i;t=n;t++)*(c+j*(n+1)+t)-=p*(*(c+i*(n+1)+t));}}for(i=n-1;i=0;i--){for(j=n-1;j=i+1;j--)(*(c+i*(n+1)+n))-=x[j]*(*(c+i*(n+1)+j));x[i]=*(c+i*(n+1)+n)/(*(c+i*(n+1)+i));}}intmain(){voidColPivot(float*,int,float[]);inti;6floatx[4];floatc[4][5]={1,1,0,3,4,2,1,-1,1,1,3,-1,-1,3,-3,-1,2,3,-1,4};ColPivot(c[0],4,x);for(i=0;i=3;i++)printf([x%d]=%f\n,i,x[i]);return0;}运行窗口4、编写用追赶法解三对角线性方程组的程序,并解下列方程组:2)Axb,其中A10x10=-411-411-41......1-411-4b=-27-15…-15实验代码C语言程序如下:#includeiostream#includecmathusingnamespacestd;7voidColPivot(float*c,intn,floatx[]){inti,j,t,k;floatp;for(i=0;i=n-2;i++){k=i;for(j=i+1;j=n-1;j++)if(fabs(*(c+j*(n+1)+i))(fabs(*(c+k*(n+1)+i))))k=j;if(k!=j)for(j=i;j=n;j++){p=*(c+i*(n+1)+j);*(c+i*(n+1)+j)=*(c+k*(n+1)+j);*(c+k*(n+1)+j)=p;}for(j=i+1;j=n-1;j++){p=(*(c+j*(n+1)+i))/(*(c+i*(n+1)+i));for(t=i;t=n;t++)*(c+j*(n+1)+t)-=p*(*(c+i*(n+1)+t));}}for(i=n-1;i=0;i--){for(j=n-1;j=i+1;j--)(*(c+i*(n+1)+n))-=x[j]*(*(c+i*(n+1)+j));x[i]=*(c+i*(n+1)+n)/(*(c+i*(n+1)+i));}}intmain(){voidColPivot(float*,int,float[]);inti;floatx[10];floatc[10][11]={-4,1,0,0,0,0,0,0,0,0,-27,1,-4,1,0,0,0,0,0,0,0,-15,0,1,-4,1,0,0,0,0,0,0,-15,0,0,1,-4,1,0,0,0,0,0,-15,0,0,0,1,-4,1,0,0,0,0,-15,0,0,0,0,1,-4,1,0,0,0,-15,0,0,0,0,0,1,-4,1,0,0,-15,0,0,0,0,0,0,1,-4,1,0,-15,0,0,0,0,0,0,0,1,-4,1,-15,0,0,0,0,0,0,0,0,1,-4,-15};ColPivot(c[0],10,x);for(i=0;i=9;i++)printf([x%d]=%f\n,i,x[i]);return0;}8运行窗口实习题四2、按下列数据Xi0.300.420.500.580.660.72Yi1.044031.084621.118031.156031.198171.23223作5次插值,并求X1=0.46,X2=0.55,X3=0.60时的函数近似值。实验代码C语言程序如下:#includeiostream#includecstdiousingnamespacestd;#defineN5voidDifference(floatx[],floaty[],intn){float*f=newfloat[n+1];intk,i;for(k=1;k=n;k++){f[0]=y[k];for(i=0;ik;i++)f[i+1]=(f[i]-y[i])/(x[k]-x[i]);y[k]=f[k];}deletef;return;}intmain(){inti;floata,b,c,varx=0.46,vary=0.55,varz=0.60;floatx[N+1]={0.30,0.42,0.50,0.58,0.66,0.72};floaty[N+1]={1.04403,1.08462,1.11803,1.15603,1.19817,1.23223};9Difference(x,y,N);a=y[N];b=y[N];c=y[N];for(i=N-1;i=0;i--)a=a*(varx-x[i])+y[i];for(i=N-1;i=0;i--)b=b*(vary-x[i])+y[i];for(i=N-1;i=0;i--)c=c*(varz-x[i])+y[i];printf(Nn(%f)=%f\n,varx,a);printf(Nn(%f)=%f\n,vary,b);printf(Nn(%f)=%f\n,varz,c);return0;}运行窗口实习题六1、用复化梯形公式和复化辛卜生公式计算积分I1(f)=202xcos1dx。观察n为多少时,所得近似值具有6位有效数字。实验代码C语言程序如下:#includeiostream#includecmath#includeiomanipusingnamespacestd;intmain(){doublef1=0,f2=0,p=0,q=0,e=0.000001,a,b,h,x1,x2,x3;inti,j=1,n1=1,n2=1;cout计算结果如下:endl;cout以下为用复化梯形公式的计算结果endl;coutsetw(15)f1setw(15)前后项差值endl;for(i=0;;i++){10a=0;b=3.14159/2;h=(b-a)/n1;for(j=1;j=n1;j++){x1=a+(j-1)*h;x2=a+j*h;f1+=h/2*(sqrt(1+cos(x1)*cos(x1))+sqrt(1+cos(x2)*cos(x2)));if(fabs(f1-p)0.000001)break;}coutsetw(15)f1setw(15)fabs(f1-p)endl;if(fabs(f1-p)0.000001)break;p=f1;n1++;f1=0;}coutsetprecision(7)用复化梯形公式计算得f1=f1,迭代次数为:n=n1endlendl;cout以下为用复化辛卜生公式的计算结果endl;coutsetw(15)f1setw(15)前后项差值endl;for(i=0;;i++){a=0;b=3.14159/2;h=(b-a)/n2;for(j=1;j=n2;j++){x1=a+(j-1)*h;x2=a+j*h;x3=a+(j-0.5)*h;f2+=h/6*(sqrt(1+cos(x1)*cos(x1))+4*sqrt(1+cos(x3)*cos(x3))+sqrt(1+cos(x2)*cos(x2)));if(fabs(f2-q)0.000001)break;}coutsetw(15)f2setw(15)fabs(f2-q)endl;if(fabs(f2-q)0.000001)break;q=f2;n2++;f2=0;}coutsetprecision(7)用复化辛卜生公式计算得f1=f2,迭代次数为:n=n2endl;return0;}运行窗口11实习题七2、用四阶龙格-库塔公式(取h=0.1)解下列微分方程组初值问题:(1)21'122120yyy)0.10(x21'252yyy0)0()0(21yy实验代码C语言程序如下:#includestdio.hvoidRunge_Kutta(float(*f1)(floatx,floaty1,floaty2),float(*f2)(floatx,floaty1,floaty2),floata,floatb,floaty0,intN){floatx=a,K1,K2,K3,K4,L1,L2,L3,L4,y1=y0,y2=y0;floath=(b-a)/N;inti;printf(x[0]=%f\ty1[0]=%f\ty2[0]=%f\n,x,y1,y2);for(i=1;i=N;i++){K1=(*f1)(x,y1,y2);L1=(*f2